]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - ctypescrypto/pbkdf2.py
c2e01bbe95de7e8dc5a53d31801e40366cd40ec3
[oss/ctypescrypto.git] / ctypescrypto / pbkdf2.py
1 """
2 PKCS5 PBKDF2 function.
3
4 """
5
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
9
10 __all__ = ['pbkdf2']
11
12 def pbkdf2(password,salt,outlen,digesttype="sha1",iterations=2000):
13         """
14                 Interface to PKCS5_PBKDF2_HMAC function
15                 Parameters:
16                         
17                                 @param password - password to derive key from
18                                 @param salt - random salt to use for key derivation
19                                 @param outlen - number of bytes to derive
20                                 @param digesttype - name of digest to use to use (default sha1)
21                                 @param iterations - number of iterations to use
22
23                                 @returns outlen bytes of key material derived from password and salt
24         """
25         dt=DigestType(digesttype)
26         out=create_string_buffer(outlen)
27         res=libcrypto.PKCS5_PBKDF2_HMAC(password,len(password),salt,len(salt),
28                 iterations,dt.digest,outlen,out)
29         if res<=0:
30                 raise LibCryptoError("error computing PBKDF2")
31         return out.raw
32
33 libcrypto.PKCS5_PBKDF2_HMAC.argtypes=(c_char_p,c_int,c_char_p,c_int,c_int,
34         c_void_p,c_int,c_char_p)
35 libcrypto.PKCS5_PBKDF2_HMAC.restupe=c_int