swift2 如何在swift 2 & 3中使用通用加密和/或计算sha 256

owfi6suc  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(182)

我正在尝试根据sha256将hash设置为密码值。我已经搜索过了,但是没有关于swift 2的信息。这个解决方案对我不起作用

func sha256(data:String) -> String {
        let data = self.dataUsingEncoding(NSUTF8StringEncoding)!
        var digest = [UInt8](count:Int(CC_SHA256_DIGEST_LENGTH), repeatedValue: 0)
        CC_SHA256(data.bytes, CC_LONG(data.length), &digest)
        let hexBytes = digest.map { String(format: "%02hhx", $0) }
        return hexBytes.joinWithSeparator("")
    }

它给出错误:使用未解析的标识符CC_SHA256_DIGEST_LENGTH

sczxawaw

sczxawaw1#

添加桥接标头并将导入添加到其中:
#import <CommonCrypto/CommonCrypto.h>
雨燕三:

func sha256(string: String) -> Data? {
    guard let messageData = string.data(using:String.Encoding.utf8) else { return nil }
    var digestData = Data(count: Int(CC_SHA256_DIGEST_LENGTH))

    _ = digestData.withUnsafeMutableBytes {digestBytes in
        messageData.withUnsafeBytes {messageBytes in
            CC_SHA256(messageBytes, CC_LONG(messageData.count), digestBytes)
        }
    }
    return digestData
}

//测试

let shaData = sha256(string:"Here is the test string")
let shaHex =  shaData!.map { String(format: "%02hhx", $0) }.joined()
print("shaHex: \(shaHex)")

十六进制数:
雨燕2:

func sha256(string string: String) -> NSData {
    let digest = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH))!
    if let data :NSData = string.dataUsingEncoding(NSUTF8StringEncoding) {
        CC_SHA256(data.bytes, CC_LONG(data.length),
            UnsafeMutablePointer<UInt8>(digest.mutableBytes))
    }
    return digest
}

//测试:

let digest = sha256(string:"Here is the test string")
print("digest: \(digest)")

输出量:
摘要:错误代码

sunseted文档部分的示例:

支持MD5、SHA1、SHA 224、SHA 256、SHA 384和SHA 512的HMAC(Swift 3+)

这些函数将使用八种加密哈希算法中的一种对字符串或数据输入进行哈希。
name参数将哈希函数名称指定为字符串支持的函数包括MD5、SHA1、SHA 224、SHA 256、SHA 384和SHA 512
此示例需要通用加密
有必要为项目提供一个桥接集管:
#import <CommonCrypto/CommonCrypto.h>
将Security.framework添加到项目中。
这些函数接受散列名称、要散列的消息、键并返回摘要:
第一个

}

第一个
//范例

let clearString = "clearData0123456"
let keyString   = "keyData8901234562"
let clearData   = clearString.data(using:.utf8)!
let keyData     = keyString.data(using:.utf8)!
print("clearString: \(clearString)")
print("keyString:   \(keyString)")
print("clearData: \(clearData as NSData)")
print("keyData:   \(keyData as NSData)")

let hmacData1 = hmac(hashName:"SHA1", message:clearData, key:keyData)
print("hmacData1: \(hmacData1! as NSData)")

let hmacData2 = hmac(hashName:"SHA1", message:clearString, key:keyString)
print("hmacData2: \(hmacData2! as NSData)")

let hmacData3 = hmac(hashName:"SHA1", message:clearString, key:keyData)
print("hmacData3: \(hmacData3! as NSData)")

输出量:

clearString: clearData0123456
keyString:   keyData8901234562
clearData: <636c6561 72446174 61303132 33343536>
keyData:   <6b657944 61746138 39303132 33343536 32>

hmacData1: <bb358f41 79b68c08 8e93191a da7dabbc 138f2ae6>
hmacData2: <bb358f41 79b68c08 8e93191a da7dabbc 138f2ae6>
hmacData3: <bb358f41 79b68c08 8e93191a da7dabbc 138f2ae6>

MD 2、MD 4、MD5、SHA1、SHA 224、SHA 256、SHA 384、SHA 512(SWIFT 3及更高版本)

这些函数将使用八种加密哈希算法中的一种对字符串或数据输入进行哈希。
name参数将散列函数名称指定为字符串
支持的函数包括MD 2、MD 4、MD5、SHA1、SHA 224、SHA 256、SHA 384和SHA 512 a此示例需要通用加密
有必要为项目提供一个桥接集管:
#import <CommonCrypto/CommonCrypto.h>
将Security.framework添加到项目中。
此函数接受散列名称和要散列的字符串,并返回数据:
第一个
示例:

let clearString = "clearData0123456"
let clearData   = clearString.data(using:.utf8)!
print("clearString: \(clearString)")
print("clearData: \(clearData as NSData)")

let hashSHA256 = hash(name:"SHA256", string:clearString)
print("hashSHA256: \(hashSHA256! as NSData)")

let hashMD5 = hash(name:"MD5", data:clearData)
print("hashMD5: \(hashMD5! as NSData)")

输出量:

clearString: clearData0123456
clearData: <636c6561 72446174 61303132 33343536>

hashSHA256: <aabc766b 6b357564 e41f4f91 2d494bcc bfa16924 b574abbd ba9e3e9d a0c8920a>
hashMD5: <4df665f7 b94aea69 695b0e7b baf9e9d6>

相关问题