sshwot/src/hashing.py

26 lines
682 B
Python

import hashlib
import os
def hash_with_salt(host, salt):
"""hash_with_salt(bytes, bytes) → bytes[32]
Hash the host using sha256 and the give salt"""
assert type(host) == bytes
assert type(salt) == bytes
m = hashlib.sha256()
m.update(host)
m.update(salt)
return m.digest()
def generate_salt():
"""generate_salt() → bytes[32]
Generates 32 bytes of randomness using the system urandom"""
return os.urandom(32)
def hash_host(host):
"""hash_host(bytes) → (bytes[32]: salt, bytes[32]: hashed_host)
Generates a salt and hashes the host with it"""
assert type(host) == bytes
salt = generate_salt()
hashed_host = hash_with_salt(host, salt)
return salt, hashed_host