Skip to content

Instantly share code, notes, and snippets.

@hacksoldier
Forked from finder39/Swift-MD5.swift
Created April 18, 2016 10:46
Show Gist options
  • Save hacksoldier/b8a6b57cdd53f0dc8d07bf3d16d46306 to your computer and use it in GitHub Desktop.
Save hacksoldier/b8a6b57cdd53f0dc8d07bf3d16d46306 to your computer and use it in GitHub Desktop.
Swift MD5
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<CUnsignedChar>.alloc(digestLen)
CC_MD5(str!, strLen, result)
var hash = NSMutableString()
for i in 0..<digestLen {
hash.appendFormat("%02x", result[i])
}
result.destroy()
return String(format: hash)
}
}
#import <CommonCrypto/CommonCrypto.h> // required for MD5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment