<s> """<STR_LIT>""" <EOL> import hashlib <EOL> from os import urandom <EOL> from base64 import b64encode , b64decode <EOL> from itertools import izip <EOL> from pbkdf2 import pbkdf2_bin <EOL> SALT_LENGTH = <NUM_LIT:12> <EOL> KEY_LENGTH = <NUM_LIT> <EOL> HASH_FUNCTION = '<STR_LIT>' <EOL> COST_FACTOR = <NUM_LIT> <EOL> def make_hash ( password ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( password , unicode ) : <EOL> password = password . encode ( '<STR_LIT:utf-8>' ) <EOL> salt = b64encode ( urandom ( SALT_LENGTH ) ) <EOL> return '<STR_LIT>' . format ( <EOL> HASH_FUNCTION , <EOL> COST_FACTOR , <EOL> salt , <EOL> b64encode ( pbkdf2_bin ( password , salt , COST_FACTOR , KEY_LENGTH , <EOL> getattr ( hashlib , HASH_FUNCTION ) ) ) ) <EOL> def check_hash ( password , hash_ ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( password , unicode ) : <EOL> password = password . encode ( '<STR_LIT:utf-8>' ) <EOL> algorithm , hash_function , cost_factor , salt , hash_a = hash_ . split ( '<STR_LIT:$>' ) <EOL> assert algorithm == '<STR_LIT>' <EOL> hash_a = b64decode ( hash_a ) <EOL> hash_b = pbkdf2_bin ( password , salt , int ( cost_factor ) , len ( hash_a ) , <EOL> getattr ( hashlib , hash_function ) ) <EOL> assert len ( hash_a ) == len ( hash_b ) <EOL> diff = <NUM_LIT:0> <EOL> for char_a , char_b in izip ( hash_a , hash_b ) : <EOL> diff |= ord ( char_a ) ^ ord ( char_b ) <EOL> return diff == <NUM_LIT:0> </s>
