Download decrypt_xfer_v2.py from po03087/xd-e6fa843d: direct link, hf CLI and curl.
- Browser
- Download file 4.25 kB
-
https://huggingface.co/datasets/po03087/xd-e6fa843d/resolve/main/decrypt_xfer_v2.py
- Command line
-
hf download hf://datasets/po03087/xd-e6fa843d/decrypt_xfer_v2.py
-
curl -L -o decrypt_xfer_v2.py https://huggingface.co/datasets/po03087/xd-e6fa843d/resolve/main/decrypt_xfer_v2.py
4.25 kB
| # Decrypts and extracts one set of an xfer_v1 transfer (python3 stdlib + openssl >= 1.1.1 + tar). | |
| # v2 (after review): explicit checks instead of asserts; every chunk's size and ciphertext sha256 are checked BEFORE tar starts; | |
| # optional --manifest-sha checks the manifest against a value received over a separate channel; no chunk deletion. | |
| # The passphrase is unwrapped with the recipient's RSA private key, kept in memory and handed to openssl through a pipe. | |
| # Usage: python3 -I decrypt_xfer_v2.py --dir DOWNLOAD_DIR --priv PRIVATE_KEY.pem --set NAME --dest EXTRACT_DIR | |
| # [--manifest-sha SHA256] [--verify-only] | |
| import argparse, hashlib, json, os, subprocess, sys | |
| OPENSSL = os.environ.get('OPENSSL', 'openssl') | |
| ENC = ['-aes-256-ctr', '-pbkdf2', '-iter', '200000', '-md', 'sha256'] | |
| BLK = 64 << 20 | |
| def check(cond, msg): | |
| if not cond: | |
| sys.exit('FAIL: ' + msg) | |
| def sha_file(path): | |
| h = hashlib.sha256() | |
| with open(path, 'rb') as f: | |
| while True: | |
| b = f.read(BLK) | |
| if not b: | |
| return h.hexdigest() | |
| h.update(b) | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument('--dir', required=True) | |
| ap.add_argument('--priv', required=True) | |
| ap.add_argument('--set', required=True) | |
| ap.add_argument('--dest') | |
| ap.add_argument('--manifest-sha') | |
| ap.add_argument('--verify-only', action='store_true', help='decrypt and check hashes, do not extract') | |
| a = ap.parse_args() | |
| check(a.verify_only or a.dest, '--dest is required unless --verify-only') | |
| mpath = os.path.join(a.dir, 'manifest_v1.json') | |
| if a.manifest_sha: | |
| check(sha_file(mpath) == a.manifest_sha, 'manifest sha256 differs from the value given') | |
| man = json.load(open(mpath)) | |
| check(man.get('format') == 'xfer_v1', 'unknown manifest format') | |
| sets = {s['name']: s for s in man['sets']} | |
| check(a.set in sets, 'no set ' + a.set) | |
| s = sets[a.set] | |
| wk = os.path.join(a.dir, 'wrapped_key_v1.bin') | |
| check(sha_file(wk) == man['wrapped_key_sha256'], 'wrapped key sha mismatch') | |
| for c in s['chunks']: | |
| path = os.path.join(a.dir, c['file']) | |
| check(os.path.isfile(path), 'missing chunk ' + c['file']) | |
| check(os.path.getsize(path) == c['bytes_cipher'], 'size mismatch ' + c['file']) | |
| check(sha_file(path) == c['sha256_cipher'], 'ciphertext sha mismatch ' + c['file']) | |
| print('all %d chunks present, ciphertext sha ok' % len(s['chunks']), flush=True) | |
| r = subprocess.run([OPENSSL, 'pkeyutl', '-decrypt', '-inkey', a.priv, '-pkeyopt', 'rsa_padding_mode:oaep', | |
| '-pkeyopt', 'rsa_oaep_md:sha256', '-pkeyopt', 'rsa_mgf1_md:sha256', '-in', wk], capture_output=True) | |
| check(r.returncode == 0, 'key unwrap failed (wrong private key?)') | |
| key = r.stdout.decode() | |
| check(len(key) == 64, 'unexpected passphrase length') | |
| tar = None | |
| if not a.verify_only: | |
| os.makedirs(a.dest, exist_ok=True) | |
| tar = subprocess.Popen(['tar', '-x', '-f', '-', '-C', a.dest], stdin=subprocess.PIPE) | |
| stream = hashlib.sha256() | |
| for i, c in enumerate(s['chunks']): | |
| rfd, wfd = os.pipe() | |
| os.write(wfd, (key + '\n').encode()) | |
| os.close(wfd) | |
| p = subprocess.Popen([OPENSSL, 'enc', '-d'] + ENC + ['-in', os.path.join(a.dir, c['file']), '-pass', 'fd:%d' % rfd], | |
| pass_fds=(rfd,), stdout=subprocess.PIPE) | |
| os.close(rfd) | |
| h, n = hashlib.sha256(), 0 | |
| while True: | |
| b = p.stdout.read(BLK) | |
| if not b: | |
| break | |
| h.update(b) | |
| stream.update(b) | |
| n += len(b) | |
| if tar: | |
| tar.stdin.write(b) | |
| check(p.wait() == 0, 'openssl failed on ' + c['file']) | |
| check(n == c['bytes_plain'] and h.hexdigest() == c['sha256_plain'], 'plaintext mismatch ' + c['file']) | |
| print('chunk ok %d/%d %s' % (i + 1, len(s['chunks']), c['file']), flush=True) | |
| if tar: | |
| tar.stdin.close() | |
| check(tar.wait() == 0, 'tar failed') | |
| check(stream.hexdigest() == s['stream_sha256'], 'stream sha mismatch') | |
| print('SET OK', a.set, 'bytes', s['stream_bytes'], 'stream sha256', s['stream_sha256'], flush=True) | |
| if __name__ == '__main__': | |
| main() | |