去除
from pbkdf2_ctypes import *
导入ctypes。util
libname=ctypes。util。查找库('libeay32')
加密=ctypes。CDLL(libname)
def \u openssl\u hashlib\u to\u crypto\u map\u get(hashfunc):
hashlib。sha1:加密。执行副总裁sha1,
hashlib。sha224:加密。执行副总裁sha224,
hashlib。sha384:加密。执行副总裁sha384,
crypto\u hashfunc=hashlib\u to\u crypto\u映射。get(hashfunc)
如果crypto_hashfunc为无:
crypto_hashfunc。重新类型=ctypes。c\u void\u p
返回crypto_hashfunc()
“”“OpenSSL兼容包装器
c_hashfunc=ctypes。c\u void\u p(\u openssl\u hashlib\u to\u crypto\u map\u get(摘要))
c_pass = ctypes.c_char_p(data)
c_passlen = ctypes.c_int(len(data))
c_salt = ctypes.c_char_p(salt)
c_saltlen = ctypes.c_int(len(salt))
c_iter = ctypes.c_int(iterations)
c_keylen = ctypes.c_int(keylen)
c_buff = ctypes.create_string_buffer(keylen)
crypto.PKCS5_PBKDF2_HMAC.argtypes = [ctypes.c_char_p, ctypes.c_int,
ctypes.c_char_p, ctypes.c_int,
ctypes.c_int, ctypes.c_void_p,
ctypes.c_int, ctypes.c_char_p]
crypto.PKCS5_PBKDF2_HMAC.restype = ctypes.c_int
err = crypto.PKCS5_PBKDF2_HMAC(c_pass, c_passlen,
c_salt, c_saltlen,
c_iter,
c_hashfunc,
c_keylen,
c_buff)
return (err, c_buff)
如果hashfunc为None:
hashfunc=hashlib。沙1
err,c\u buff=\u openssl\u pbkdf2(数据,salt,迭代,hashfunc,keylen)
if err == 0:
raise ValueError('wrong parameters')
return c_buff.raw[:keylen]
def pbkdf2_hex(数据,salt,迭代次数=1000,keylen=24,hashfunc=无):
返回binascii。hexlify(pkcs5\u pbkdf2\u hmac(数据,salt,迭代,keylen,hashfunc))