1,建立oc的橋接文件,可任意創(chuàng)建一個(gè)oc的文件,Xcode就會(huì)提示讓創(chuàng)建橋接文件,要檢查targets-build setting 的該位置,是否顯示的是橋接文件的名字

2,在橋接文件中導(dǎo)入頭文件 #import<CommonCrypto/CommonCrypto.h>
3,編寫代碼
public func MD5(str:String) ->String{? ? ? ??
? ? ? ? ? ?let cStr = str.cString(using: String.Encoding.utf8)? ? ? ?
? ? ? ? ? ?let digestLen = Int(CC_MD5_DIGEST_LENGTH)? ? ? ?
? ? ? ? ? ?let result = UnsafeMutablePointer.allocate(capacity: digestLen)
? ? ? ? ? ?CC_MD5(str, CC_LONG(strlen(cStr)), result)
? ? ? ? ? ?let retStr = NSMutableString()
? ? ? ? ? ?for i in 0..< digestLen {
? ? ? ? ? ? ? ? ? ?retStr.appendFormat("%02x", result[i])
? ? ? ? ? }
? ? ? ? ? result.deallocate(capacity: digestLen)
? ? ? ? ? return retStr as String
}
4,使用方法
let strMD5 = "ASDNsdol"
print("\(StringPublic().MD5(str: strMD5))")