Created
November 28, 2018 14:57
-
-
Save tkanzakic/e8e8e119f0fa4146de383b974df78f92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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