美文网首页
Swift Md5加密

Swift Md5加密

作者: Str1ng | 来源:发表于2017-03-16 09:32 被阅读0次

    1.随便新建一个OC文件会提示新建桥接文件xxx-Bridging-Header.

    2.在桥接文件加入#import <CommonCrypto/CommonDigest.h>

    3.新增一个swift文件StringMd5.swift

    4.在StringMd5.swift加入

    extension String {    func md5() -> String {        let str = self.cString(using: String.Encoding.utf8)        let strLen = CUnsignedInt(self.lengthOfBytes(using: String.Encoding.utf8))        let digestLen = Int(CC_MD5_DIGEST_LENGTH)        let result = UnsafeMutablePointer.allocate(capacity: digestLen)

    CC_MD5(str!, strLen, result)

    let hash = NSMutableString()

    for i in 0 ..< digestLen {

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

    }

    result.deinitialize()

    return String(format: hash as String)

    }

    }

    5.调用方法为String类字符串.md5()

    相关文章

      网友评论

          本文标题:Swift Md5加密

          本文链接:https://www.haomeiwen.com/subject/zlsdnttx.html