如何使用ios swift将字符串转换为MD5哈希 [英] How to convert string to MD5 hash using ios swift
问题描述
我想转换用户输入或假设任何字符串如abc到MD5哈希。我想在ios swift中这样做。我已经提到了下面的链接,但解决方案对我不起作用,或者我很难实现它,因为我是快速编程的新手。有人可以通过明确的步骤来帮助我实现这一目标。提前致谢!
I want to convert user input or assume any string like "abc" to MD5 hash. I want to do this in ios swift. I have refered the below links but the solutions are not working for me or I am confused to implement it properly as i am new to swift programing. Could someone help me with clear steps to achieve this. Thanks in advance!
http://iosdeveloperzone.com/2014/10/03/using-commoncrypto-in-swift/
为了更清楚,我希望像在php中一样快速实现这一点。
To be more clear i want to achieve this in swift like what we do in php.
$str = "Hello";
echo md5($str);
输出:8b1a9953c4611296a827abf8c47804d7
Output: 8b1a9953c4611296a827abf8c47804d7
推荐答案
有两个步骤:
1.从字符串创建md5数据
2.将md5数据转换为十六进制字符串
There are two steps:
1. Create md5 data from a string
2. Covert the md5 data to a hex string
Swift 2.0
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
digest: 8b1a9953c4611296a827abf8c47804d7
Swift 3.0:
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 ==
md5Hex: 8b1a9953c4611296a827abf8c47804d7
md5Base64: ixqZU8RhEpaoJ6v4xHgE1w==
注意:
#import<必须将CommonCrypto / CommonCrypto.h>
添加到Bridging-Header文件
Notes:
#import <CommonCrypto/CommonCrypto.h>
must be added to a Bridging-Header file
有关如何创建桥接头的信息,请参阅此SO答案。
For how to create a Bridging-Header see this SO answer.
一般情况下,MD5不应用于新工作,SHA256是当前的最佳做法。
In general MD5 should not be used for new work, SHA256 is a current best practice.
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参数将哈希函数名称指定为字符串
支持函数是MD2,MD4,MD5,SHA1,SHA224,SHA256,SHA384和SHA512
a
这个例子需要Common Crypto
必须有一个桥接头到项目:< br>
#import< CommonCrypto / CommonCrypto.h>
将Security.framework添加到项目中。
The name parameter specifies the hash function name as a String
Supported functions are MD2, MD4, MD5, SHA1, SHA224, SHA256, SHA384 and SHA512
a
This example requires Common Crypto
It is necessary to have a bridging header to the project:
#import <CommonCrypto/CommonCrypto.h>
Add the Security.framework to the project.
此函数采用哈希名称和字符串进行哈希处理并返回a数据:
This function takes a hash name and String to be hashed and returns a Data:
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将字符串转换为MD5哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!