<s> """<STR_LIT>""" <EOL> import os <EOL> import hmac <EOL> import hashlib <EOL> import posixpath <EOL> import codecs <EOL> from struct import Struct <EOL> from random import SystemRandom <EOL> from operator import xor <EOL> from itertools import starmap <EOL> from werkzeug . _compat import range_type , PY2 , text_type , izip , to_bytes , string_types , to_native <EOL> SALT_CHARS = '<STR_LIT>' <EOL> DEFAULT_PBKDF2_ITERATIONS = <NUM_LIT:1000> <EOL> _pack_int = Struct ( '<STR_LIT>' ) . pack <EOL> _builtin_safe_str_cmp = getattr ( hmac , '<STR_LIT>' , None ) <EOL> _sys_rng = SystemRandom ( ) <EOL> _os_alt_seps = list ( sep for sep in [ os . path . sep , os . path . altsep ] <EOL> if sep not in ( None , '<STR_LIT:/>' ) ) <EOL> def _find_hashlib_algorithms ( ) : <EOL> algos = getattr ( hashlib , '<STR_LIT>' , None ) <EOL> if algos is None : <EOL> algos = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> rv = { } <EOL> for algo in algos : <EOL> func = getattr ( hashlib , algo , None ) <EOL> if func is not None : <EOL> rv [ algo ] = func <EOL> return rv <EOL> _hash_funcs = _find_hashlib_algorithms ( ) <EOL> def pbkdf2_hex ( data , salt , iterations = DEFAULT_PBKDF2_ITERATIONS , <EOL> keylen = None , hashfunc = None ) : <EOL> """<STR_LIT>""" <EOL> rv = pbkdf2_bin ( data , salt , iterations , keylen , hashfunc ) <EOL> return to_native ( codecs . encode ( rv , '<STR_LIT>' ) ) <EOL> def pbkdf2_bin ( data , salt , iterations = DEFAULT_PBKDF2_ITERATIONS , <EOL> keylen = None , hashfunc = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( hashfunc , string_types ) : <EOL> hashfunc = _hash_funcs [ hashfunc ] <EOL> elif not hashfunc : <EOL> hashfunc = hashlib . sha1 <EOL> salt = to_bytes ( salt ) <EOL> mac = hmac . HMAC ( to_bytes ( data ) , None , hashfunc ) <EOL> if not keylen : <EOL> keylen = mac . digest_size <EOL> def _pseudorandom ( x , mac = mac ) : <EOL> h = mac . copy ( ) <EOL> h . update ( x ) <EOL> return bytearray ( h . digest ( ) ) <EOL> buf = bytearray ( ) <EOL> for block in range_type ( <NUM_LIT:1> , - ( - keylen // mac . digest_size ) + <NUM_LIT:1> ) : <EOL> rv = u = _pseudorandom ( salt + _pack_int ( block ) ) <EOL> for i in range_type ( iterations - <NUM_LIT:1> ) : <EOL> u = _pseudorandom ( bytes ( u ) ) <EOL> rv = bytearray ( starmap ( xor , izip ( rv , u ) ) ) <EOL> buf . extend ( rv ) <EOL> return bytes ( buf [ : keylen ] ) <EOL> def safe_str_cmp ( a , b ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( a , text_type ) : <EOL> a = a . encode ( '<STR_LIT:utf-8>' ) <EOL> if isinstance ( b , text_type ) : <EOL> b = b . encode ( '<STR_LIT:utf-8>' ) <EOL> if _builtin_safe_str_cmp is not None : <EOL> return _builtin_safe_str_cmp ( a , b ) <EOL> if len ( a ) != len ( b ) : <EOL> return False <EOL> rv = <NUM_LIT:0> <EOL> if PY2 : <EOL> for x , y in izip ( a , b ) : <EOL> rv |= ord ( x ) ^ ord ( y ) <EOL> else : <EOL> for x , y in izip ( a , b ) : <EOL> rv |= x ^ y <EOL> return rv == <NUM_LIT:0> <EOL> def gen_salt ( length ) : <EOL> """<STR_LIT>""" <EOL> if length <= <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' . join ( _sys_rng . choice ( SALT_CHARS ) for _ in range_type ( length ) ) <EOL> def _hash_internal ( method , salt , password ) : <EOL> """<STR_LIT>""" <EOL> if method == '<STR_LIT>' : <EOL> return password , method <EOL> if isinstance ( password , text_type ) : <EOL> password = password . encode ( '<STR_LIT:utf-8>' ) <EOL> if method . startswith ( '<STR_LIT>' ) : <EOL> args = method [ <NUM_LIT:7> : ] . split ( '<STR_LIT::>' ) <EOL> if len ( args ) not in ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> method = args . pop ( <NUM_LIT:0> ) <EOL> iterations = args and int ( args [ <NUM_LIT:0> ] or <NUM_LIT:0> ) or DEFAULT_PBKDF2_ITERATIONS <EOL> is_pbkdf2 = True <EOL> actual_method = '<STR_LIT>' % ( method , iterations ) <EOL> else : <EOL> is_pbkdf2 = False <EOL> actual_method = method <EOL> hash_func = _hash_funcs . get ( method ) <EOL> if hash_func is None : <EOL> raise TypeError ( '<STR_LIT>' % method ) <EOL> if is_pbkdf2 : <EOL> if not salt : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> rv = pbkdf2_hex ( password , salt , iterations , <EOL> hashfunc = hash_func ) <EOL> elif salt : <EOL> if isinstance ( salt , text_type ) : <EOL> salt = salt . encode ( '<STR_LIT:utf-8>' ) <EOL> rv = hmac . HMAC ( salt , password , hash_func ) . hexdigest ( ) <EOL> else : <EOL> h = hash_func ( ) <EOL> h . update ( password ) <EOL> rv = h . hexdigest ( ) <EOL> return rv , actual_method <EOL> def generate_password_hash ( password , method = '<STR_LIT>' , salt_length = <NUM_LIT:8> ) : <EOL> """<STR_LIT>""" <EOL> salt = method != '<STR_LIT>' and gen_salt ( salt_length ) or '<STR_LIT>' <EOL> h , actual_method = _hash_internal ( method , salt , password ) <EOL> return '<STR_LIT>' % ( actual_method , salt , h ) <EOL> def check_password_hash ( pwhash , password ) : <EOL> """<STR_LIT>""" <EOL> if pwhash . count ( '<STR_LIT:$>' ) < <NUM_LIT:2> : <EOL> return False <EOL> method , salt , hashval = pwhash . split ( '<STR_LIT:$>' , <NUM_LIT:2> ) <EOL> return safe_str_cmp ( _hash_internal ( method , salt , password ) [ <NUM_LIT:0> ] , hashval ) <EOL> def safe_join ( directory , filename ) : <EOL> """<STR_LIT>""" <EOL> filename = posixpath . normpath ( filename ) <EOL> for sep in _os_alt_seps : <EOL> if sep in filename : <EOL> return None <EOL> if os . path . isabs ( filename ) or filename . startswith ( '<STR_LIT>' ) : <EOL> return None <EOL> return os . path . join ( directory , filename ) </s>
