Created
January 5, 2017 01:03
-
-
Save Ridwy/43371d3acc76d67a020a8a84dfb3bf9f to your computer and use it in GitHub Desktop.
get MD5 in Swift 3
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
extension String { | |
var md5: String? { | |
guard let str = cString(using: .utf8) else { return nil } | |
let strLen = CC_LONG(lengthOfBytes(using: .utf8)) | |
let digestLen = Int(CC_MD5_DIGEST_LENGTH) | |
let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen) | |
CC_MD5(str, strLen, result) | |
let hash = (0 ..< digestLen).map({String(format:"%02x", result[$0])}).joined() | |
result.deallocate(capacity: digestLen) | |
return hash as String | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment