swift使用md5

来源:互联网 发布:游戏编程入门教程 编辑:程序博客网 时间:2024/06/05 18:54

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 iin 0..<digestLen {

            hash.appendFormat("%02x", result[i])

        }

        result.destroy()

        returnString(format: hash asString)

    }


0 0