<s> """<STR_LIT>""" <EOL> import ctypes <EOL> import ctypes . util <EOL> import hashlib <EOL> import platform <EOL> import os . path <EOL> import binascii <EOL> import sys <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> __version__ = '<STR_LIT>' <EOL> def _commoncrypto_hashlib_to_crypto_map_get ( hashfunc ) : <EOL> hashlib_to_crypto_map = { hashlib . sha1 : <NUM_LIT:1> , <EOL> hashlib . sha224 : <NUM_LIT:2> , <EOL> hashlib . sha256 : <NUM_LIT:3> , <EOL> hashlib . sha384 : <NUM_LIT:4> , <EOL> hashlib . sha512 : <NUM_LIT:5> } <EOL> crypto_hashfunc = hashlib_to_crypto_map . get ( hashfunc ) <EOL> if crypto_hashfunc is None : <EOL> raise ValueError ( '<STR_LIT>' % hashfunc ) <EOL> return crypto_hashfunc <EOL> def _commoncrypto_pbkdf2 ( data , salt , iterations , digest , keylen ) : <EOL> """<STR_LIT>""" <EOL> c_hashfunc = ctypes . c_uint32 ( _commoncrypto_hashlib_to_crypto_map_get ( digest ) ) <EOL> c_pass = ctypes . c_char_p ( data ) <EOL> c_passlen = ctypes . c_size_t ( len ( data ) ) <EOL> c_salt = ctypes . c_char_p ( salt ) <EOL> c_saltlen = ctypes . c_size_t ( len ( salt ) ) <EOL> c_iter = ctypes . c_uint ( iterations ) <EOL> c_keylen = ctypes . c_size_t ( keylen ) <EOL> c_buff = ctypes . create_string_buffer ( keylen ) <EOL> crypto . CCKeyDerivationPBKDF . restype = ctypes . c_int <EOL> crypto . CCKeyDerivationPBKDF . argtypes = [ ctypes . c_uint32 , <EOL> ctypes . c_char_p , <EOL> ctypes . c_size_t , <EOL> ctypes . c_char_p , <EOL> ctypes . c_size_t , <EOL> ctypes . c_uint32 , <EOL> ctypes . c_uint , <EOL> ctypes . c_char_p , <EOL> ctypes . c_size_t ] <EOL> ret = crypto . CCKeyDerivationPBKDF ( <NUM_LIT:2> , <EOL> c_pass , c_passlen , <EOL> c_salt , c_saltlen , <EOL> c_hashfunc , <EOL> c_iter , <EOL> c_buff , <EOL> c_keylen ) <EOL> return ( <NUM_LIT:1> - ret , c_buff ) <EOL> def _openssl_hashlib_to_crypto_map_get ( hashfunc ) : <EOL> hashlib_to_crypto_map = { hashlib . md5 : crypto . EVP_md5 , <EOL> hashlib . sha1 : crypto . EVP_sha1 , <EOL> hashlib . sha256 : crypto . EVP_sha256 , <EOL> hashlib . sha224 : crypto . EVP_sha224 , <EOL> hashlib . sha384 : crypto . EVP_sha384 , <EOL> hashlib . sha512 : crypto . EVP_sha512 } <EOL> crypto_hashfunc = hashlib_to_crypto_map . get ( hashfunc ) <EOL> if crypto_hashfunc is None : <EOL> raise ValueError ( '<STR_LIT>' % hashfunc ) <EOL> crypto_hashfunc . restype = ctypes . c_void_p <EOL> return crypto_hashfunc ( ) <EOL> def _openssl_pbkdf2 ( data , salt , iterations , digest , keylen ) : <EOL> """<STR_LIT>""" <EOL> c_hashfunc = ctypes . c_void_p ( _openssl_hashlib_to_crypto_map_get ( digest ) ) <EOL> c_pass = ctypes . c_char_p ( data ) <EOL> c_passlen = ctypes . c_int ( len ( data ) ) <EOL> c_salt = ctypes . c_char_p ( salt ) <EOL> c_saltlen = ctypes . c_int ( len ( salt ) ) <EOL> c_iter = ctypes . c_int ( iterations ) <EOL> c_keylen = ctypes . c_int ( keylen ) <EOL> c_buff = ctypes . create_string_buffer ( keylen ) <EOL> crypto . PKCS5_PBKDF2_HMAC . argtypes = [ ctypes . c_char_p , ctypes . c_int , <EOL> ctypes . c_char_p , ctypes . c_int , <EOL> ctypes . c_int , ctypes . c_void_p , <EOL> ctypes . c_int , ctypes . c_char_p ] <EOL> crypto . PKCS5_PBKDF2_HMAC . restype = ctypes . c_int <EOL> err = crypto . PKCS5_PBKDF2_HMAC ( c_pass , c_passlen , <EOL> c_salt , c_saltlen , <EOL> c_iter , <EOL> c_hashfunc , <EOL> c_keylen , <EOL> c_buff ) <EOL> return ( err , c_buff ) <EOL> try : <EOL> system = platform . system ( ) <EOL> if system == '<STR_LIT>' : <EOL> if platform . architecture ( ) [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> libname = ctypes . util . find_library ( '<STR_LIT>' ) <EOL> if not libname : <EOL> raise OSError ( '<STR_LIT>' ) <EOL> crypto = ctypes . CDLL ( libname ) <EOL> else : <EOL> libname = ctypes . util . find_library ( '<STR_LIT>' ) <EOL> if not libname : <EOL> raise OSError ( '<STR_LIT>' ) <EOL> crypto = ctypes . CDLL ( libname ) <EOL> _pbkdf2_hmac = _openssl_pbkdf2 <EOL> crypto . PKCS5_PBKDF2_HMAC <EOL> elif system == '<STR_LIT>' : <EOL> if [ int ( x ) for x in platform . mac_ver ( ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:.>' ) ] < [ <NUM_LIT:10> , <NUM_LIT:7> , <NUM_LIT:0> ] : <EOL> raise OSError ( '<STR_LIT>' % platform . mac_ver ( ) [ <NUM_LIT:0> ] ) <EOL> libname = ctypes . util . find_library ( '<STR_LIT>' ) <EOL> if not libname : <EOL> raise OSError ( '<STR_LIT>' ) <EOL> crypto = ctypes . CDLL ( os . path . basename ( libname ) ) <EOL> _pbkdf2_hmac = _commoncrypto_pbkdf2 <EOL> else : <EOL> libname = ctypes . util . find_library ( '<STR_LIT>' ) <EOL> if not libname : <EOL> raise OSError ( '<STR_LIT>' ) <EOL> crypto = ctypes . CDLL ( os . path . basename ( libname ) ) <EOL> _pbkdf2_hmac = _openssl_pbkdf2 <EOL> crypto . PKCS5_PBKDF2_HMAC <EOL> except ( OSError , AttributeError ) : <EOL> _ , e , _ = sys . exc_info ( ) <EOL> raise ImportError ( '<STR_LIT>' <EOL> '<STR_LIT>' % e ) <EOL> def pkcs5_pbkdf2_hmac ( data , salt , iterations = <NUM_LIT:1000> , keylen = <NUM_LIT> , hashfunc = None ) : <EOL> if hashfunc is None : <EOL> hashfunc = hashlib . sha1 <EOL> err , c_buff = _pbkdf2_hmac ( data , salt , iterations , hashfunc , keylen ) <EOL> if err == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return c_buff . raw [ : keylen ] <EOL> def pbkdf2_hex ( data , salt , iterations = <NUM_LIT:1000> , keylen = <NUM_LIT> , hashfunc = None ) : <EOL> return binascii . hexlify ( pkcs5_pbkdf2_hmac ( data , salt , iterations , keylen , hashfunc ) ) <EOL> def pbkdf2_bin ( data , salt , iterations = <NUM_LIT:1000> , keylen = <NUM_LIT> , hashfunc = None ) : <EOL> return pkcs5_pbkdf2_hmac ( data , salt , iterations , keylen , hashfunc ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> try : <EOL> crypto . SSLeay_version . restype = ctypes . c_char_p <EOL> print ( crypto . SSLeay_version ( <NUM_LIT:0> ) ) <EOL> except : <EOL> pass <EOL> import platform <EOL> if platform . python_version_tuple ( ) < ( '<STR_LIT:3>' , '<STR_LIT:0>' , '<STR_LIT:0>' ) : <EOL> def bytes ( * args ) : <EOL> return str ( args [ <NUM_LIT:0> ] ) <EOL> for h in [ hashlib . sha1 , hashlib . sha224 , hashlib . sha256 , <EOL> hashlib . sha384 , hashlib . sha512 ] : <EOL> print ( binascii . hexlify ( pkcs5_pbkdf2_hmac ( bytes ( '<STR_LIT>' , '<STR_LIT:utf-8>' ) * <NUM_LIT:11> , <EOL> bytes ( '<STR_LIT>' , '<STR_LIT:utf-8>' ) , <EOL> hashfunc = h ) ) ) </s>
