]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blobdiff - ctypescrypto/pbkdf2.py
Merge pull request #5 from parneshraniga/master
[oss/ctypescrypto.git] / ctypescrypto / pbkdf2.py
index 0c2b077f175804048e268c918de3d180fff72313..bde567b5e361e119758ef0e7037a073ceb6bb519 100644 (file)
@@ -3,31 +3,34 @@ PKCS5 PBKDF2 function.
 
 """
 
-from ctypes import c_char_p,c_int, c_void_p, create_string_buffer
+from ctypes import c_char_p, c_int, c_void_p, create_string_buffer
 from ctypescrypto import libcrypto
 from ctypescrypto.digest import DigestType
+from ctypescrypto.exception import LibCryptoError
 
-def pbkdf2(password,salt,outlen,digesttype="sha1",iterations=2000):
-       """
-               Interface to PKCS5_PBKDF2_HMAC function
-               Parameters:
-                       
-                               @param password - password to derive key from
-                               @param salt - random salt to use for key derivation
-                               @param outlen - number of bytes to derive
-                               @param digesttype - name of digest to use to use (default sha1)
-                               @param iterations - number of iterations to use
+__all__ = ['pbkdf2']
 
-                               @returns outlen bytes of key material derived from password and salt
-       """
-       dt=DigestType(digesttype)
-       out=create_string_buffer(outlen)
-       res=libcrypto.PKCS5_PBKDF2_HMAC(password,len(password),salt,len(salt),
-               iterations,dt.digest,outlen,out)
-       if res<=0:
-               raise LibCryptoError("error computing PBKDF2")
-       return out.raw
+def pbkdf2(password, salt, outlen, digesttype="sha1", iterations=2000):
+    """
+    Interface to PKCS5_PBKDF2_HMAC function
+    Parameters:
 
-libcrypto.PKCS5_PBKDF2_HMAC.argtypes=(c_char_p,c_int,c_char_p,c_int,c_int,
-       c_void_p,c_int,c_char_p)
-libcrypto.PKCS5_PBKDF2_HMAC.restupe=c_int
+    @param password - password to derive key from
+    @param salt - random salt to use for key derivation
+    @param outlen - number of bytes to derive
+    @param digesttype - name of digest to use to use (default sha1)
+    @param iterations - number of iterations to use
+
+    @returns outlen bytes of key material derived from password and salt
+    """
+    dgst = DigestType(digesttype)
+    out = create_string_buffer(outlen)
+    res = libcrypto.PKCS5_PBKDF2_HMAC(password, len(password), salt, len(salt),
+                                      iterations, dgst.digest, outlen, out)
+    if res <= 0:
+        raise LibCryptoError("error computing PBKDF2")
+    return out.raw
+
+libcrypto.PKCS5_PBKDF2_HMAC.argtypes = (c_char_p, c_int, c_char_p, c_int, c_int,
+                                        c_void_p, c_int, c_char_p)
+libcrypto.PKCS5_PBKDF2_HMAC.restupe = c_int