Swift_技巧(1)_加密_MD5加密
2017-02-28 本文已影响31人
丶纳凉
一丶说明
需要用到swift与oc的桥接文件;
利用桥接文件导入#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
}
三丶扩展:
1.其他加密方式
https://github.com/krzyzanowskim/CryptoSwift
2.添加桥接文件:
参考
http://www.jianshu.com/p/27b9ff63ab6f
3.product Name:
Build Settings
Paste_Image.png