swift 3.0 中的 Sha 256 加密语法错误 [英] Sha 256 Encryption syntax error in swift 3.0

查看:39
本文介绍了swift 3.0 中的 Sha 256 加密语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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

}

我正在使用这个函数来加密字符串,它在 swift 2 中工作正常,现在它在 swift 3.0

I am using this function to encrypt strings it was working fine in swift 2, now its not working in swift 3.0

推荐答案

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), res?.mutableBytes.assumingMemoryBound(to: UInt8.self))
    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

}

替换了 CC_SHA256((data! as NSData).bytes, CC_LONG(data!.count), UnsafeMutablePointer(res!.mutableBytes))

Replaced CC_SHA256((data! as NSData).bytes, CC_LONG(data!.count), UnsafeMutablePointer(res!.mutableBytes))

这篇关于swift 3.0 中的 Sha 256 加密语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆