gpt4 book ai didi

ios - 如何使用 Swift 在 iOS 中将字符串转换为 MD5 哈希值?

转载 作者:IT王子 更新时间:2023-10-29 04:56:46 28 4
gpt4 key购买 nike

我想将“abc”之类的字符串转换为 MD5 散列。我想在 iOS 和 Swift 中执行此操作。我已尝试使用以下解决方案,但它们对我不起作用:

Importing CommonCrypto in a Swift framework

How to use CC_MD5 method in swift language.

http://iosdeveloperzone.com/2014/10/03/using-commoncrypto-in-swift/

更清楚地说,我想在 Swift 中实现类似于此 PHP 代码输出的输出:

$str = "Hello";

echo md5($str);

输出:8b1a9953c4611296a827abf8c47804d7

最佳答案

分为两步:
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)")

输出:

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", $0) }.joined()
print("md5Hex: \(md5Hex)")

let md5Base64 = md5Data.base64EncodedString()
print("md5Base64: \(md5Base64)")

输出:

md5Hex: 8b1a9953c4611296a827abf8c47804d7
md5Base64: ixqZU8RhEpaoJ6v4xHgE1w==

swift 5.0:

import Foundation
import var CommonCrypto.CC_MD5_DIGEST_LENGTH
import func CommonCrypto.CC_MD5
import typealias CommonCrypto.CC_LONG

func 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", $0) }.joined()
print("md5Hex: \(md5Hex)")

let md5Base64 = md5Data.base64EncodedString()
print("md5Base64: \(md5Base64)")

输出:

md5Hex: 8b1a9953c4611296a827abf8c47804d7
md5Base64: ixqZU8RhEpaoJ6v4xHgE1w==

注意事项:
#import <CommonCrypto/CommonCrypto.h> 必须添加到 Bridging-Header 文件中

有关如何创建桥接 header ,请参阅 this SO answer

一般而言,MD5 不应用于新工作,SHA256 是当前的最佳实践。

已弃用文档部分的示例:

MD2、MD4、MD5、SHA1、SHA224、SHA256、SHA384、SHA512(Swift 3+)

These functions will hash either String or Data input with one of eight cryptographic hash algorithms.

name参数指定哈希函数名称为String
支持的函数有 MD2、MD4、MD5、SHA1、SHA224、SHA256、SHA384 和 SHA512一个此示例需要 Common Crypto
必须要有项目的桥接头:
#import <CommonCrypto/CommonCrypto.h>
将 Security.framework 添加到项目中。



这个函数接受一个散列名称和要散列的字符串并返回一个数据:

name: A name of a hash function as a String  string: The String to be hashed  returns: the hashed result as Data  
func hash(name:String, string:String) -> Data? {
let data = string.data(using:.utf8)!
return hash(name:name, data:data)
}

例子:

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>

关于ios - 如何使用 Swift 在 iOS 中将字符串转换为 MD5 哈希值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32163848/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com