swift使用md5:
1:在XX-Bridging-Header.h 中添加头文件
#import
<CommonCrypto/CommonDigest.h>
2:md5的方法
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)
var
hash =
NSMutableString
()
for
i
in
0
..<digestLen {
hash.
appendFormat
(
“%02x”
, result[i])
}
result.
destroy
()
return
String
(format: hash
as
String
)
}