分两个步骤:
1.从字符串创建md5数据
2.将md5数据隐蔽为十六进制字符串
Swift 2.0:
func md5(string string: String) -> String { var digest = [UInt8](count: Int(CC_MD5_DIGEST_LENGTH), repeatedValue: 0) if let data = string.dataUsingEncoding(NSUTF8StringEncoding) { CC_MD5(data.bytes, CC_LONG(data.length), &digest) } var digestHex = "" for index in 0..<Int(CC_MD5_DIGEST_LENGTH) { digestHex += String(format: "%02x", digest[index]) } return digestHex}//Test:let digest = md5(string:"Hello")print("digest: (digest)")
输出:
摘要:8b1a9953c4611296a827abf8c47804d7
Swift 3.0:
func MD5(string: String) -> Data { let messageData = string.data(using:.utf8)! var digestData = Data(count: Int(CC_MD5_DIGEST_LENGTH)) _ = digestData.withUnsafeMutableBytes {digestBytes in messageData.withUnsafeBytes {messageBytes in CC_MD5(messageBytes, CC_LONG(messageData.count), digestBytes) } } return digestData}//Test:let md5Data = MD5(string:"Hello")let md5Hex = md5Data.map { String(format: "%02hhx", Swift 5.0:) }.joined()print("md5Hex: (md5Hex)")let md5base64 = md5Data.base64EnpredString()print("md5base64: (md5base64)")
输出:
md5Hex:8b1a9953c4611296a827abf8c47804d7
md5base64:ixqZU8RhEpaoJ6v4xHgE1w ==
import Foundationimport var CommonCrypto.CC_MD5_DIGEST_LENGTHimport func CommonCrypto.CC_MD5import typealias CommonCrypto.CC_LonGfunc MD5(string: String) -> Data { let length = Int(CC_MD5_DIGEST_LENGTH) let messageData = string.data(using:.utf8)! var digestData = Data(count: length) _ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in messageData.withUnsafeBytes { messageBytes -> UInt8 in if let messageBytesbaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress { let messageLength = CC_LONG(messageData.count) CC_MD5(messageBytesbaseAddress, messageLength, digestBytesBlindMemory) } return 0 } } return digestData }//Test:let md5Data = MD5(string:"Hello")let md5Hex = md5Data.map { String(format: "%02hhx",#import <CommonCrypto/CommonCrypto.h>) }.joined()print("md5Hex: (md5Hex)")let md5base64 = md5Data.base64EnpredString()print("md5base64: (md5base64)")
MD2,MD4,MD5,SHA1,SHA224,SHA256,SHA384,SHA512(Swift 3+)
输出:
md5Hex:8b1a9953c4611296a827abf8c47804d7
md5base64:ixqZU8RhEpaoJ6v4xHgE1w ==
注意:
#import <CommonCrypto/CommonCrypto.h>必须添加到Bridging-Header文件中
有关如何创建Bridging-Header的信息
通常,不应将MD5用于新工作,SHA256是当前的最佳实践。
不推荐使用的文档部分中的示例:name:作为字符串的哈希函数的名称 string:要散列的字符串 返回:哈希结果为Datafunc hash(name:String, string:String) -> Data? { let data = string.data(using:.utf8)! return hash(name:name, data:data)}
这些函数将使用八种密码哈希算法之一来哈希String或Data输入。
name参数将哈希函数名称指定为String
支持的函数是MD2,MD4,MD5,SHA1,SHA224,SHA256,SHA384和SHA512 a此示例需要通用加密
必须在项目中具有桥接头:
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)")
添加安全性.framework到项目。
此函数采用哈希名称和要哈希的String并返回一个Data:
clearString: clearData0123456cleardata: <636c6561 72446174 61303132 33343536>hashSHA256: <aabc766b 6b357564 e41f4f91 2d494bcc bfa16924 b574abbd ba9e3e9d a0c8920a>hashMD5: <4df665f7 b94aea69 695b0e7b baf9e9d6>
例子:
输出:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)