Skip to content

Instantly share code, notes, and snippets.

@earonesty
Created May 14, 2019 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save earonesty/e24df86bf815031479e6b761dccd100f to your computer and use it in GitHub Desktop.
Save earonesty/e24df86bf815031479e6b761dccd100f to your computer and use it in GitHub Desktop.
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import ( Cipher, algorithms, modes )
import os
nonce = os.urandom(16)
key = os.urandom(32)
mode = modes.GCM(nonce, None)
cipher = Cipher(algorithms.AES(key), mode, backend=default_backend())
enc = cipher.encryptor()
dec = cipher.decryptor()
a = os.urandom(64)
b = os.urandom(64)
import gc
from SecureBytes import clearmem, scanmem, scanmem_supported
assert scanmem_supported
data = a + b
print(data)
assert scanmem(a, b)
clearmem(data)
assert not scanmem(a, b)
data = a + b
ct = enc.update(data) + enc.finalize()
tag = enc.tag
assert len(ct) == 128
clearmem(data)
assert not scanmem(a, b)
pt = dec.update(ct) + dec.finalize_with_tag(tag)
assert len(pt) == 128
print(pt)
assert scanmem(a, b)
clearmem(pt)
assert not scanmem(a, b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment