#!/usr/bin/env python3 """Three controlled scratch-file cases; no user media or software is read.""" import hashlib import json from pathlib import Path import platform import shutil import tempfile def digest(path): result = hashlib.sha256() with path.open('rb') as stream: for block in iter(lambda: stream.read(65536), b''): result.update(block) return result.hexdigest() def main(): with tempfile.TemporaryDirectory(prefix='compsyscon-fixity-demo-') as scratch: root = Path(scratch) source, copied = root/'source.txt', root/'copy.txt' source.write_bytes(b'PRESERVATION TEACHING FILE\nversion=1\n') baseline = digest(source) shutil.copyfile(source, copied) copied_hash = digest(copied) assert copied_hash == baseline and copied.read_bytes() == source.read_bytes() copy_size = copied.stat().st_size copied.write_bytes(copied.read_bytes().replace(b'version=1', b'version=2')) altered_hash = digest(copied) assert copied.stat().st_size == copy_size and altered_hash != baseline assert digest(source) == baseline # Original scratch fixture preserved. recomputed_reference = altered_hash # A new reference, not the original baseline. assert digest(copied) == recomputed_reference result = { 'synthetic_only': True, 'real_media_recovery_or_software_test': False, 'python': platform.python_version(), 'algorithm': 'SHA-256', 'fixture_utf8_text': source.read_text(), 'bytes': copy_size, 'original_reference': baseline, 'cases': [ {'case': 'unchanged_copy', 'sha256': copied_hash, 'matches_original_reference': True}, {'case': 'one_byte_changed_same_size', 'sha256': altered_hash, 'matches_original_reference': False}, {'case': 'changed_copy_with_new_reference', 'sha256': digest(copied), 'matches_new_reference': True, 'matches_original_reference': False}, ], 'original_scratch_source_unchanged': True, 'authenticity_functionality_safety_rights_tested': False, } print(json.dumps(result, indent=2)) if __name__ == '__main__': main()