Skip to content

Instantly share code, notes, and snippets.

@Ridwy
Created January 5, 2017 01:03
Show Gist options
  • Save Ridwy/43371d3acc76d67a020a8a84dfb3bf9f to your computer and use it in GitHub Desktop.
Save Ridwy/43371d3acc76d67a020a8a84dfb3bf9f to your computer and use it in GitHub Desktop.
get MD5 in Swift 3
extension String {
var md5: String? {
guard let str = cString(using: .utf8) else { return nil }
let strLen = CC_LONG(lengthOfBytes(using: .utf8))
let digestLen = Int(CC_MD5_DIGEST_LENGTH)
let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
CC_MD5(str, strLen, result)
let hash = (0 ..< digestLen).map({String(format:"%02x", result[$0])}).joined()
result.deallocate(capacity: digestLen)
return hash as String
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment