Created
June 30, 2022 07:04
-
-
Save jreisinger/e27f1f8c8c111225b40e9732ba6a704a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse, hashlib, sys | |
from pathlib import Path | |
def store(path, data, key): | |
data_path = Path(path) | |
hash_path = data_path.with_suffix('.hash') | |
hash_value = hashlib.blake2b(data, key=key).hexdigest() | |
with data_path.open(mode='x'), hash_path.open(mode='x'): | |
data_path.write_bytes(data) | |
hash_path.write_text(hash_value) | |
def is_modified(path, key): | |
data_path = Path(path) | |
hash_path = data_path.with_suffix('.hash') | |
data = data_path.read_bytes() | |
original_hash_value = hash_path.read_text() | |
hash_value = hashlib.blake2b(data, key=key).hexdigest() | |
return original_hash_value != hash_value | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Demo keyed hashing.') | |
parser.add_argument('--store', action='store_true', help='store hash (default: verify hash)') | |
args = parser.parse_args() | |
path = 'x' | |
data = b'bla' | |
key = b'123' | |
if args.store: | |
store(path, data, key) | |
sys.exit(0) | |
if is_modified(path, key): | |
print("Mallory was here!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment