Swift MD5 加密字符串

前端之家收集整理的这篇文章主要介绍了Swift MD5 加密字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

包含桥接头文件


#import <CommonCrypto/CommonDigest.h>



extension String{ func md5() -> String! { let str = self.cStringUsingEncoding(NSUTF8StringEncoding) let strLen = CUnsignedInt(self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)) let digestLen = Int(CC_MD5_DIGEST_LENGTH) let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen) CC_MD5(str!,strLen,result) let hash = NSMutableString() for i in 0 ..< digestLen { hash.appendFormat("%02x",result[i]) } result.destroy() return String(format: hash as String) }}

猜你在找的Swift相关文章