Skip to content

Instantly share code, notes, and snippets.

@networkextension
Last active July 11, 2016 14:02
Show Gist options
  • Select an option

  • Save networkextension/2d1c43993df75dd5041541bddcfc6fa8 to your computer and use it in GitHub Desktop.

Select an option

Save networkextension/2d1c43993df75dd5041541bddcfc6fa8 to your computer and use it in GitHub Desktop.
evpBytesToKey equal openssl EVP_BytesToKey func
func evpBytesToKey(password:String, keyLen:Int) ->NSData {
let md5Len:Int = 16
let cnt = 1// (keyLen -1)/md5Len + 1
let m = NSMutableData.init(length: cnt*md5Len)!
let bytes = password.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
// memcpy((m?.mutableBytes)!, bytes.bytes , password.characters.count)
let md5 = bytes.md5
m.setData(md5)
// Repeatedly call md5 until bytes generated is enough.
// Each call to md5 uses data: prev md5 sum + password.
let d = NSMutableData.init(length: md5Len+bytes.length)!
//d := make([]byte, md5Len+len(password))
var start = 0
for _ in 0 ..< cnt {
start += md5Len
memcpy(d.mutableBytes,m.bytes , m.length)
memcpy(d.mutableBytes+md5Len, bytes.bytes, bytes.length)
let md5 = d.md5
m.appendData(md5)
}
return m
}
@networkextension
Copy link
Copy Markdown
Author

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