"""Authenticated streaming media archive. Key is the first stdin line, never argv. Run in Deck's existing Python runtime (cryptography already installed). """ import hashlib,io,json,os,sys,tarfile from cryptography.hazmat.primitives.ciphers import Cipher,algorithms,modes MAGIC=b'ATHENA-MEDIA-GCM1\n' CHUNK=1024*1024 def derive(key,salt):return hashlib.scrypt(key,salt=salt,n=32768,r=8,p=1,maxmem=64*1024**2,dklen=32) def encrypt(source,target,key): salt=os.urandom(16);nonce=os.urandom(12);head=MAGIC+salt+nonce enc=Cipher(algorithms.AES(derive(key,salt)),modes.GCM(nonce)).encryptor();enc.authenticate_additional_data(head) target.write(head) while b:=source.read(CHUNK):target.write(enc.update(b)) target.write(enc.finalize());target.write(enc.tag) class Decrypted(io.RawIOBase): def __init__(self,source,key): self.source=source;head=source.read(len(MAGIC)+28) if not head.startswith(MAGIC) or len(head)!=len(MAGIC)+28:raise ValueError('Kein Medienbackup') self.dec=Cipher(algorithms.AES(derive(key,head[len(MAGIC):len(MAGIC)+16])),modes.GCM(head[-12:])).decryptor();self.dec.authenticate_additional_data(head);self.tail=b'';self.buffer=b'';self.done=False def readable(self):return True def read(self,size=-1): if size<0: result=[] while b:=self.read(CHUNK):result.append(b) return b''.join(result) while len(self.buffer)