Skip to content

Instantly share code, notes, and snippets.

@t089
Last active August 10, 2018 09:36
Show Gist options
  • Save t089/3d3e703d0c54f0751828f9432bbdfb0b to your computer and use it in GitHub Desktop.
Save t089/3d3e703d0c54f0751828f9432bbdfb0b to your computer and use it in GitHub Desktop.
CommonCrypto
extension Array where Element == UInt8 {
var md5Hash: [UInt8] {
var hash = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
CC_MD5(self, UInt32(self.count), &hash)
return hash
}
var hexString : String {
return self.reduce(into: "", { $0 += String(format: "%02x", $1) })
}
}
let bytes = Array("Test".utf8)
let md5 = bytes.md5Hash.hexString
md5 == "0cbc6611f5540bd0809a388dc95a615b" // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment