swift使用md5

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)

    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章