博客> Swift MD5和Base64加密
Swift MD5和Base64加密
2017-11-21 00:05 评论:0 阅读:338 梧桐一叶
swift MD5 base64 加密

首先要在桥接文件中导入#import 一、MD5加密 extension String { //MD5加密 var md5:String{ let str = self.cStringUsingEncoding(NSUTF8StringEncoding) let strLen = CC_LONG(self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)) let digestLen = Int(CC_MD5_DIGEST_LENGTH) let result = UnsafeMutablePointer.alloc(digestLen);

    CC_MD5(str!, strLen, result);

    let hash = NSMutableString();
    for i in 0 ..< digestLen {
        hash.appendFormat("x", result[i]);
    }
    result.destroy()
    return String(format: hash as String)

}

}

二、base64加密 let str = "base641234567" let utf8Str = str.dataUsingEncoding(NSUTF8StringEncoding) let base64Str = utf8Password?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)) print("===(base64Str)")

收藏
1
sina weixin mail 回到顶部