我在 Swift 中有以下代码:
static func sha256(_ data: Data) -> Data?
{
guard let res = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH)) else { return nil }
CC_SHA256((data as NSData).bytes, CC_LONG(data.count), res.mutableBytes.assumingMemoryBound(to: UInt8.self))
return res as Data
}
我不确定在 Kotlin 中进行 Android 开发时如何翻译 CC_SHA256_DIGEST_LENGTH
和 CC_SHA256
。
执行此操作的正确方法是什么?
最佳答案
更通用的方法(按照 user2340612 的建议):
private fun hashString(input: String, algorithm: String): String {
return MessageDigest.getInstance(algorithm)
.digest(input.toByteArray())
.fold("", { str, it -> str + "%02x".format(it) })
}
感谢this guy .
关于android - Swift 中 CC_SHA256 的 Kotlin 等效项是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57582226/