一丶說明
需要用到swift與oc的橋接文件;
利用橋接文件導(dǎo)入#import <CommonCrypto/CommonDigest.h>
二丶代碼
// MARK:- MD5加密
func md5String(str:String) -> String{
let cStr = str.cString(using: String.Encoding.utf8);
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
CC_MD5(cStr!,(CC_LONG)(strlen(cStr!)), buffer)
let md5String = NSMutableString();
for i in 0 ..< 16{
md5String.appendFormat("%02x", buffer[i])
}
free(buffer)
return md5String as String
}
三丶擴(kuò)展:
1.其他加密方式
https://github.com/krzyzanowskim/CryptoSwift
2.添加橋接文件:
參考
http://www.reibang.com/p/27b9ff63ab6f
3.product Name:
Build Settings