【发布时间】:2016-10-07 15:42:21
【问题描述】:
func SHA256() -> String {
let data = self.data(using: String.Encoding.utf8)
let res = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH))
CC_SHA256((data! as NSData).bytes, CC_LONG(data!.count), UnsafeMutablePointer(res!.mutableBytes))
let hashedString = "\(res!)".replacingOccurrences(of: "", with: "").replacingOccurrences(of: " ", with: "")
let badchar: CharacterSet = CharacterSet(charactersIn: "\"<\",\">\"")
let cleanedstring: String = (hashedString.components(separatedBy: badchar) as NSArray).componentsJoined(by: "")
return cleanedstring
}
【问题讨论】:
-
或者这个是一个String to String函数:stackoverflow.com/questions/25761344/…,它可以很容易地适应SHA256。
-
SHA256,实际上所有的哈希函数都不是加密的,它们是单向哈希函数。