Advertisement
Guest User

Untitled

a guest
May 25th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. from cryptography.hazmat.backends import default_backend
  2. from cryptography.hazmat.primitives.ciphers import ( Cipher, algorithms, modes )
  3.  
  4. import os
  5.  
  6. nonce = os.urandom(16)
  7. key = os.urandom(32)
  8.  
  9. mode = modes.GCM(nonce, None)
  10.  
  11. cipher = Cipher(algorithms.AES(key), mode, backend=default_backend())
  12.  
  13. enc = cipher.encryptor()
  14. dec = cipher.decryptor()
  15.  
  16. a = os.urandom(64)
  17. b = os.urandom(64)
  18.  
  19. import gc
  20. from SecureBytes import clearmem, scanmem, scanmem_supported
  21.  
  22. assert scanmem_supported
  23.  
  24. data = a + b
  25.  
  26. print(data)
  27.  
  28. assert scanmem(a, b)
  29.  
  30. clearmem(data)
  31. assert not scanmem(a, b)
  32.  
  33. data = a + b
  34.  
  35. ct = enc.update(data) + enc.finalize()
  36. tag = enc.tag
  37. assert len(ct) == 128
  38.  
  39. clearmem(data)
  40. assert not scanmem(a, b)
  41.  
  42. pt = dec.update(ct) + dec.finalize_with_tag(tag)
  43.  
  44. assert len(pt) == 128
  45.  
  46. print(pt)
  47.  
  48. assert scanmem(a, b)
  49.  
  50. clearmem(pt)
  51.  
  52. assert not scanmem(a, b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement