X-Git-Url: https://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=ctypescrypto%2Fpbkdf2.py;h=2a2125af9f802b6532c3911d4f4cf7623aa0f29f;hb=79425e0192b55dded5b50b2d5216184480b7fc75;hp=85a99a5c097fc3cd5ee19cf55b33b03f8aa40ad4;hpb=954b6dc9e3312f8d8b49f20f8466e6d2a8342f35;p=oss%2Fctypescrypto.git diff --git a/ctypescrypto/pbkdf2.py b/ctypescrypto/pbkdf2.py index 85a99a5..2a2125a 100644 --- a/ctypescrypto/pbkdf2.py +++ b/ctypescrypto/pbkdf2.py @@ -3,33 +3,38 @@ PKCS5 PBKDF2 function. """ -from ctypes import c_char_p,c_int, c_void_p, create_string_buffer -from ctypescrypto import libcrypto +from ctypes import c_char_p, c_int, c_void_p, create_string_buffer +from ctypescrypto import libcrypto, chartype from ctypescrypto.digest import DigestType +from ctypescrypto.exception import LibCryptoError __all__ = ['pbkdf2'] -def pbkdf2(password,salt,outlen,digesttype="sha1",iterations=2000): +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 + Interface to PKCS5_PBKDF2_HMAC function + Parameters: - @returns outlen bytes of key material derived from password and salt + @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 """ - 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: + dgst = DigestType(digesttype) + out = create_string_buffer(outlen) + if isinstance(password,chartype): + pwd = password.encode("utf-8") + else: + pwd = password + res = libcrypto.PKCS5_PBKDF2_HMAC(pwd, len(pwd), 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 +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