swift使用md5

  • Post author:
  • Post category:其他



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


)


}



版权声明:本文为TIANBING878原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。