Skip to content

Instantly share code, notes, and snippets.

@bleft
Created December 14, 2016 13:19
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bleft/364d64b36974097f6f8f62001676e108 to your computer and use it in GitHub Desktop.
Save bleft/364d64b36974097f6f8f62001676e108 to your computer and use it in GitHub Desktop.
get md5 hash from Data in swift
extension Data {
var md5 : String {
var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
_ = self.withUnsafeBytes { bytes in
CC_MD5(bytes, CC_LONG(self.count), &digest)
}
var digestHex = ""
for index in 0..<Int(CC_MD5_DIGEST_LENGTH) {
digestHex += String(format: "%02x", digest[index])
}
return digestHex
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment