Swift2.0 字符串分类 MD5加密

import UIKit

                class iosMD5: NSObject {

                }
                extension String{
                    func md5() ->String!{
                        let str = self.cStringUsingEncoding(NSUTF8StringEncoding)
                        let strLen = CUnsignedInt(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("%02x", result[i])
                        }
                        result.destroy()
                        return String(format: hash as String)
                    }
                }

使用

let  str =  "加密内容".md5() //加密
  • 看我那么可爱n(≧▽≦)n
  • 关注我的微薄 (梁同桌):http://weibo.com/tongrenyinsheng
  • 个人网站(同人音声) http://www.tongrenyinsheng.com
  • ios 个人写的app (同人音声)ASMR音乐

你可能感兴趣的:(Swift2.0 字符串分类 MD5加密)