Skip to content

Instantly share code, notes, and snippets.

@tkanzakic
Created November 28, 2018 14:57
Show Gist options
  • Save tkanzakic/e8e8e119f0fa4146de383b974df78f92 to your computer and use it in GitHub Desktop.
Save tkanzakic/e8e8e119f0fa4146de383b974df78f92 to your computer and use it in GitHub Desktop.
import CommonCrypto
extension String {
var md5: String? {
guard let data = self.data(using: .utf8) else { return nil }
let hash = data.withUnsafeBytes { (bytes: UnsafePointer<Data>) -> [UInt8] in
var hash: [UInt8] = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
CC_MD5(bytes, CC_LONG(data.count), &hash)
return hash
}
return hash.map { String(format: "%02x", $0) }.joined()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment