|
1
|
#import <CommonCrypto/CommonDigest.h> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import Foundationextension String { var md5 : String{ let str = self.cStringUsingEncoding(NSUTF8StringEncoding) let strLen = CC_LONG(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) }} |
|
1
2
|
print("welcome to hangge.com".md5)//输出:6ad70eb3a4a50011af9c62cc0674803a |
原文:http://www.cnblogs.com/Free-Thinker/p/4858358.html