Skip to content

Instantly share code, notes, and snippets.

@telliott99
Last active February 11, 2018 11:17
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 telliott99/b648b36813a70d84b0f1 to your computer and use it in GitHub Desktop.
Save telliott99/b648b36813a70d84b0f1 to your computer and use it in GitHub Desktop.
import Foundation
func doIt() {
let s = "The quick brown fox jumps over the lazy dog."
let n = Int(CC_MD5_DIGEST_LENGTH)
let len = CC_LONG(s.utf8.count)
let ctx = UnsafeMutablePointer<CC_MD5_CTX>.alloc(1)
var digest = Array<UInt8>(count:n, repeatedValue:0)
CC_MD5_Init(ctx)
CC_MD5_Update(ctx, s, len)
CC_MD5_Final(&digest, ctx)
ctx.dealloc(1)
let hL = digest.map { String(format:"%02x", $0) }
print(hL.reduce("", combine: +))
}
@acalism
Copy link

acalism commented Feb 11, 2018

There is no need to allocate CC_MD5_CTX.

var ctx = CC_MD5_CTX()  
var digest = Array<UInt8>.init(repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))  
CC_MD5_Init(&ctx)  
CC_MD5_Update(&ctx, s, CC_LONG(s.utf8.count))  
CC_MD5_Final(&digest, &ctx)  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment