1.随便新建一个OC文件会提示新建桥接文件xxx-Bridging-Header.
2.在桥接文件加入#import <CommonCrypto/CommonDigest.h>
3.新增一个swift文件StringMd5.swift
4.在StringMd5.swift加入
extension String { func md5() -> String { let str = self.cString(using: String.Encoding.utf8) let strLen = CUnsignedInt(self.lengthOfBytes(using: String.Encoding.utf8)) let digestLen = Int(CC_MD5_DIGEST_LENGTH) let result = UnsafeMutablePointer.allocate(capacity: digestLen)
CC_MD5(str!, strLen, result)
let hash = NSMutableString()
for i in 0 ..< digestLen {
hash.appendFormat("%02x", result[i])
}
result.deinitialize()
return String(format: hash as String)
}
}
5.调用方法为String类字符串.md5()