Skip to content
Snippets Groups Projects
Hash.py 967 B
Newer Older
from Packages import *

def create_hash(spectra):
    #using the md5 hash function.
    hash_func = hashlib.md5()
    spectra = str(spectra)
    encoded_spectra = spectra.encode()
    hash_func.update(encoded_spectra)
    hash = hash_func.hexdigest()
    return hash

def check_hash(hash):
    # 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 / "hash.txt"], shell=True)
    # if hash present
    if 'returncode=0' in str(nb_hash):
        return 'existing hash'
    # if hash not present, add it to the file with cat function
    else:
        add_hash = subprocess.run(['echo', str(hash) + '>>', subprocess_path / "hash.txt"], shell=True)
        if 'returncode=0' in str(add_hash):
            return 'hash added'
        else:
            return 'error while adding the new hash'