美文网首页
Swift Md5封装

Swift Md5封装

作者: 透亮心情 | 来源:发表于2017-11-11 15:19 被阅读12次

步骤1:必须添加oc桥接文件 在桥接文件中导入

#import <CommonCrypto/CommonCrypto.h>

步骤2:给String添加分类 这个代码是全部代码可以直接使用!

import Foundation

extension String {
    
    func md5() -> String {
        let cStr = self.cString(using: .utf8)
        
        
        var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
        
        CC_MD5(cStr, CC_LONG(strlen(cStr)), &digest)
        
        var output = String()
        
        for i in digest {
            
            output = output.appendingFormat("%02X", i)
        }
        
        return output;
    }
    
}

相关文章

网友评论

      本文标题:Swift Md5封装

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