Using MD5 in Swift

String+md5.swift

import Foundation

extension String {
    func MD5() -> String {
        let data = (self as NSString).dataUsingEncoding(NSUTF8StringEncoding)
        let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))
        let resultBytes = UnsafeMutablePointer(result.mutableBytes)
        CC_MD5(data.bytes, CC_LONG(data.length), resultBytes)

        let a = UnsafeBufferPointer(start: resultBytes, length: result.length)
        let hash = NSMutableString()

        for i in a {
            hash.appendFormat("%02x", i)
        }

        return hash
    }
}

Bridge.h

#import 

Leave a Reply

Your email address will not be published. Required fields are marked *