Skip to content

Instantly share code, notes, and snippets.

@jknthn
Created August 24, 2016 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jknthn/ba642d4f3d0abf2425a0293c19ca2494 to your computer and use it in GitHub Desktop.
Save jknthn/ba642d4f3d0abf2425a0293c19ca2494 to your computer and use it in GitHub Desktop.
var md5: String {
let str = self.cString(using: .utf8)
let strLen = CUnsignedInt(self.lengthOfBytes(using: .utf8))
let digestLen = Int(CC_MD5_DIGEST_LENGTH)
let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
CC_MD5(str!, strLen, result)
let hash = NSMutableString()
for i in 0..<digestLen {
hash.appendFormat("%02x", result[i])
}
result.deinitialize()
return String(format: hash as String)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment