Advertisement
surik00

new_password_helper

Oct 28th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. import os
  2. import hashlib
  3.  
  4. from typing import Tuple
  5.  
  6. HASH_NAME = "sha256"
  7. ITERATIONS = 100_000
  8.  
  9.  
  10.  
  11. def gen_salt() -> bytes:
  12.     return os.urandom(32)
  13.  
  14.  
  15. def hash_password(salt: bytes, password: str) -> bytes:
  16.     """
  17.    Generate a key for password using provided salt
  18.    :param salt:
  19.    :param password:
  20.    :return:
  21.    """
  22.     new_key: bytes = hashlib.pbkdf2_hmac(HASH_NAME, password.encode("utf-8"), salt, ITERATIONS)
  23.     return new_key
  24.  
  25.  
  26. def create_new_password(password: str) -> Tuple[bytes, bytes]:
  27.     """
  28.    Creates new salt and hashes passed password
  29.    Returned tuple contains the key (hash) and salt
  30.    :param password:
  31.    :return:
  32.    """
  33.     salt = gen_salt()
  34.     new_key = hash_password(salt, password)
  35.     return new_key, salt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement