6 from ctypes import c_char_p,c_int, c_void_p, create_string_buffer
7 from ctypescrypto import libcrypto
8 from ctypescrypto.digest import DigestType
10 def pbkdf2(password,salt,outlen,digesttype="sha1",iterations=2000):
12 Interface to PKCS5_PBKDF2_HMAC function
15 @param password - password to derive key from
16 @param salt - random salt to use for key derivation
17 @param outlen - number of bytes to derive
18 @param digesttype - name of digest to use to use (default sha1)
19 @param iterations - number of iterations to use
21 @returns outlen bytes of key material derived from password and salt
23 dt=DigestType(digesttype)
24 out=create_string_buffer(outlen)
25 res=libcrypto.PKCS5_PBKDF2_HMAC(password,len(password),salt,len(salt),
26 iterations,dt.digest,outlen,out)
28 raise LibCryptoError("error computing PBKDF2")
31 libcrypto.PKCS5_PBKDF2_HMAC.argtypes=(c_char_p,c_int,c_char_p,c_int,c_int,
32 c_void_p,c_int,c_char_p)
33 libcrypto.PKCS5_PBKDF2_HMAC.restupe=c_int