本文介绍了如何在Swift 4中复制hash_hmac('sha256',$ key,$ secret_key)函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 在使用像CommonCrypto,CryptoSwift这样的库之后,我尝试了在Swift 4中生成相当于hash_hmac('sha256',$ key,$ secret_key)的php函数,但没有成功。我需要这些函数进行API认证,使用Alamofire库,这是一个很棒的库。因为我使用Swift 4,所以与其他Swift库的兼容性不太好。即使CryptoSwift具有Swift 4的最新版本(0.7.1),我仍然会遇到很多兼容错误,比如 解决方案 b $ b 具有MD5,SHA1,SHA224,SHA256,SHA384,SHA512的HMAC(Swift 3) name参数将散列函数名称指定为字符串支持的函数是MD5,SHA1,SHA224,SHA256,SHA384和SHA512 这个例子需要Common Crypto 必须有该项目的桥接头: #import< CommonCrypto / CommonCrypto.h> 将Security.framework添加到该项目。 这些函数需要一个散列名,消息被散列,一个键并返回一个摘要: hashName:作为字符串的散列函数的名称 message:作为数据的消息 key:作为数据的返回:摘要为数据 func hmac(hashName:String,message:Data,key:Data) - >数据? { let algos = [SHA1:(kCCHmacAlgSHA1,CC_SHA1_DIGEST_LENGTH),MD5:(kCCHmacAlgMD5,CC_MD5_DIGEST_LENGTH),SHA224:( kCCHmacAlgSHA224,CC_SHA224_DIGEST_LENGTH),SHA256:(kCCHmacAlgSHA256,CC_SHA256_DIGEST_LENGTH),SHA384:(kCCHmacAlgSHA384,CC_SHA384_DIGEST_LENGTH),SHA512:(kCCHmacAlgSHA512,CC_SHA512_DIGEST_LENGTH)] guard let(hashAlgorithm,length) = algos [hashName] else {return nil} var macData = Data(count:Int(length)) macData.withUnsafeMutableBytes {macBytes in message.withUnsafeBytes {messageBytes in key.withUnsafeBytes {keyBytes in CCHmac(CCHmacAlgorithm(hashAlgorithm), keyBytes,key.count, messageBytes,message.count, macBytes)} } } return macData } hashName:作为字符串的散列函数的名称消息:作为字符串的消息 key:作为字符串的键返回:作为数据的摘要 func hmac(hashName:String,message:String,key:String) - >数据? { let messageData = message.data(使用:.utf8)! let keyData = key.data(使用:.utf8)! 返回hmac(hashName:hashName,message:messageData,key:keyData)} hashName:作为字符串的散列函数的名称消息:消息作为字符串 key:键作为数据返回:摘要为数据 func hmac(hashName:String,message:String,key :Data) - >数据? { let messageData = message.data(使用:.utf8)! 返回hmac(hashName:hashName,message:messageData,key:key)} //示例 let clearString =clearData0123456让keyString =keyData8901234562让clearData = clearString.data(使用:.utf8)! let keyData = keyString.data(使用:.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 !)作为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> I've tried generating the hash_hmac('sha256', $key, $secret_key) php function equivalent in Swift 4 without success, after using libraries like CommonCrypto, CryptoSwift. I need these function for API authentication, using Alamofire library, which is a great library. Since i use Swift 4 the compatibility with other Swift libraries is not so good. Even with CryptoSwift which has the latest version(0.7.1) for Swift 4 i still get a lot of compatibility errors likesenter image description here 解决方案 Swift 3/4:HMAC with MD5, SHA1, SHA224, SHA256, SHA384, SHA512 (Swift 3)The name parameter specifies the hash function name as a StringSupported functions are MD5, SHA1, SHA224, SHA256, SHA384 and SHA512This example requires Common CryptoIt is necessary to have a bridging header to the project:#import <CommonCrypto/CommonCrypto.h>Add the Security.framework to the project.These functions takes a hash name, message to be hashed, a key and return a digest:hashName: name of a hash function as Stringmessage: message as Datakey: key as Datareturns: digest as Datafunc hmac(hashName:String, message:Data, key:Data) -> Data? { let algos = ["SHA1": (kCCHmacAlgSHA1, CC_SHA1_DIGEST_LENGTH), "MD5": (kCCHmacAlgMD5, CC_MD5_DIGEST_LENGTH), "SHA224": (kCCHmacAlgSHA224, CC_SHA224_DIGEST_LENGTH), "SHA256": (kCCHmacAlgSHA256, CC_SHA256_DIGEST_LENGTH), "SHA384": (kCCHmacAlgSHA384, CC_SHA384_DIGEST_LENGTH), "SHA512": (kCCHmacAlgSHA512, CC_SHA512_DIGEST_LENGTH)] guard let (hashAlgorithm, length) = algos[hashName] else { return nil } var macData = Data(count: Int(length)) macData.withUnsafeMutableBytes {macBytes in message.withUnsafeBytes {messageBytes in key.withUnsafeBytes {keyBytes in CCHmac(CCHmacAlgorithm(hashAlgorithm), keyBytes, key.count, messageBytes, message.count, macBytes) } } } return macData }hashName: name of a hash function as Stringmessage: message as Stringkey: key as Stringreturns: digest as Datafunc hmac(hashName:String, message:String, key:String) -> Data? { let messageData = message.data(using:.utf8)! let keyData = key.data(using:.utf8)! return hmac(hashName:hashName, message:messageData, key:keyData)}hashName: name of a hash function as Stringmessage: message as Stringkey: key as Datareturns: digest as Datafunc hmac(hashName:String, message:String, key:Data) -> Data? { let messageData = message.data(using:.utf8)! return hmac(hashName:hashName, message:messageData, key:key)}// Exampleslet 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)")Output:clearString: clearData0123456keyString: keyData8901234562clearData: <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> 这篇关于如何在Swift 4中复制hash_hmac('sha256',$ key,$ secret_key)函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 17:32