Skip to content
Snippets Groups Projects
hash.py 1020 B
Newer Older
DIANE's avatar
DIANE committed

def create_hash(to_hash):
    #using the md5 hash function.
DIANE's avatar
DIANE committed
    hash_func = md5()
DIANE's avatar
DIANE committed
    to_hash = str(to_hash)
    encoded_to_hash = to_hash.encode()
    hash_func.update(encoded_to_hash)
    hash = hash_func.hexdigest()
    return hash

def check_hash(hash, hash_type):
    # path to hash file and grep/cat functions for Win
    subprocess_path = Path("src/data/hash/")
    # run a grep from the hash onto the hash file
    nb_hash = subprocess.run([subprocess_path / 'grep.exe', '-c', hash, subprocess_path / str(hash_type + ".txt")], shell=True)
    # if hash present
    if 'returncode=0' in str(nb_hash):
        return 'existing hash'
    # if hash not present
    else:
        return 'missing hash'

def add_hash(hash, hash_type):
    # add it to the file with cat function
    add_hash = subprocess.run(['echo', str(hash) + '>>', subprocess_path / str(hash_type + ".txt")], shell=True)
    if 'returncode=0' in str(add_hash):
        return 'hash added'
    else:
        return 'error while adding the new hash'