Skip to content
Snippets Groups Projects
Commit b3239243 authored by Nicolas Barthes's avatar Nicolas Barthes
Browse files

hash update (split create, check & add function)

parent 6e35d0f4
No related branches found
No related tags found
No related merge requests found
from Packages import * from Packages import *
def create_hash(spectra): def create_hash(to_hash):
#using the md5 hash function. #using the md5 hash function.
hash_func = hashlib.md5() hash_func = hashlib.md5()
spectra = str(spectra) to_hash = str(to_hash)
encoded_spectra = spectra.encode() encoded_to_hash = to_hash.encode()
hash_func.update(encoded_spectra) hash_func.update(encoded_to_hash)
hash = hash_func.hexdigest() hash = hash_func.hexdigest()
return hash return hash
def check_hash(hash): def check_hash(hash, hash_type):
# path to hash file and grep/cat functions for Win # path to hash file and grep/cat functions for Win
subprocess_path = Path("src/data/hash/") subprocess_path = Path("src/data/hash/")
# run a grep from the hash onto the hash file # 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) nb_hash = subprocess.run([subprocess_path / 'grep.exe', '-c', hash, subprocess_path / str(hash_type + ".txt")], shell=True)
# if hash present # if hash present
if 'returncode=0' in str(nb_hash): if 'returncode=0' in str(nb_hash):
return 'existing hash' return 'existing hash'
# if hash not present, add it to the file with cat function # if hash not present
else: else:
add_hash = subprocess.run(['echo', str(hash) + '>>', subprocess_path / "hash.txt"], shell=True) return 'missing hash'
if 'returncode=0' in str(add_hash):
return 'hash added' def add_hash(hash, hash_type):
else: # add it to the file with cat function
return 'error while adding the new hash' add_hash = subprocess.run(['echo', str(hash) + '>>', subprocess_path / str(hash_type + ".txt")], shell=True)
\ No newline at end of file if 'returncode=0' in str(add_hash):
return 'hash added'
else:
return 'error while adding the new hash'
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment