我需要将字符串编码为 UTF-16LE(稍后转换为 sha1),但我遇到了一些问题。这是我试过的:
let utf16array = Array("password".utf16)
print(utf16array)
// [112, 97, 115, 115, 119, 111, 114, 100]
但这正是我所期待的:
// [112, 0, 97, 0, 115, 0, 115, 0, 119, 0, 111, 0, 114, 0, 100, 0]
同样的事情使用 utf8array:
let utf8array = "password".utf8.map({ $0 as UInt8 })
// [112, 97, 115, 115, 119, 111, 114, 100]
所以,这就是我“修复”它所做的:
var bytesArray:[UInt16] = []
for byte in utf16array {
bytesArray.append(byte)
bytesArray.append(0)
}
print(bytesArray)
// [112, 0, 97, 0, 115, 0, 115, 0, 119, 0, 111, 0, 114, 0, 100, 0]
但我敢肯定这不是正确的方法。有什么建议吗?
最佳答案
您可以使用 UTF-16LE 数据表示
let password = "password€"
let data = password.data(using: .utf16LittleEndian)!
print(data as NSData)
// <70006100 73007300 77006f00 72006400 ac20>
这已经足以计算 SHA1 摘要(代码 来自 How to crypt string to sha1 with Swift? ):
var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA1($0, CC_LONG(data.count), &digest)
}
let hexEncodedDigest = digest.map { String(format: "%02hhx", $0) }.joined()
print(hexEncodedDigest)
// 177f0d080dfe533e102dd67d6321204813cf1b0c
但是如果你需要它作为一个字节数组那么
let bytesArray = data.map { $0 }
print(bytesArray)
// [112, 0, 97, 0, 115, 0, 115, 0, 119, 0, 111, 0, 114, 0, 100, 0, 172, 32]
会起作用。
(为了演示,我附加了一个非ASCII字符,
€ = U+20AC
变为 172, 32
。)
如果你想知道如何将 [UInt16]
数组转换为
[UInt8]
数组,这是你可以通过一些指针杂耍来实现的
(只有一个副本):
let utf16array = Array("password€".utf16)
print(utf16array)
// [112, 97, 115, 115, 119, 111, 114, 100, 8364]
let bytes = Array(utf16array.withUnsafeBufferPointer {
$0.baseAddress!.withMemoryRebound(to: UInt8.self, capacity: 2 * utf16array.count) {
UnsafeBufferPointer(start: $0, count: 2 * utf16array.count)
}
})
print(bytes)
// [112, 0, 97, 0, 115, 0, 115, 0, 119, 0, 111, 0, 114, 0, 100, 0, 172, 32]
关于 swift 3 : Encode String to UTF-16LE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40116258/