Skip to content

Instantly share code, notes, and snippets.

@ppth0608
Created July 28, 2019 07:10
Show Gist options
  • Save ppth0608/090e880fa6fba3b1d246929969d3d3aa to your computer and use it in GitHub Desktop.
Save ppth0608/090e880fa6fba3b1d246929969d3d3aa to your computer and use it in GitHub Desktop.
How to make `md5` hashed string.
import Foundation
import CommonCrypto
//Reference : https://stackoverflow.com/questions/55356220/get-string-md5-in-swift-5
extension String {
var md5: String {
let data = Data(self.utf8)
let hash = data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) -> [UInt8] in
var hash = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
CC_MD5(bytes.baseAddress, 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