Created
July 28, 2019 07:10
-
-
Save ppth0608/090e880fa6fba3b1d246929969d3d3aa to your computer and use it in GitHub Desktop.
How to make `md5` hashed string.
This file contains 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 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