def hash_token(token, salt=8, rounds=16384, algorithm='sha512'):
 h = hashlib.new(algorithm)
 if isinstance(salt, int):
  salt = b2a_hex(os.urandom(salt))
 if isinstance(salt, bytes):
  bsalt = salt
  salt = salt.decode('utf8')
 else:
  bsalt = salt.encode('utf8')
 btoken = token.encode('utf8', 'replace')
 h.update(bsalt)
 for i in range(rounds):
  h.update(btoken)
 digest = h.hexdigest()
 return '{algorithm}:{rounds}:{salt}:{digest}'.format(**locals())

def hash_token ( token , salt = 8 , rounds = 16384 , algorithm = 'sha512' ) : h = hashlib . new ( algorithm ) if isinstance ( salt , int ) : salt = b2a_hex ( os . urandom ( salt ) ) if isinstance ( salt , bytes ) : bsalt = salt salt = salt . decode ( 'utf8' ) else : bsalt = salt . encode ( 'utf8' ) btoken = token . encode ( 'utf8' , 'replace' ) h . update ( bsalt ) for i in range ( rounds ) : h . update ( btoken ) digest = h . hexdigest ( ) return '{algorithm}:{rounds}:{salt}:{digest}' . format ( ** locals ( ) )
hash a token .