test.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python3
  2. """AES-256 + HMAC-SHA1 encrypt/decrypt round-trip using the actual Cipher API."""
  3. import sys, base64
  4. from core.tools.crypter import Cipher
  5. PASSPHRASE = "U-NATi0n!"
  6. KEY = base64.b64encode(PASSPHRASE.encode("utf-8"))
  7. SAMPLES = [
  8. "hello",
  9. "x",
  10. "Mensaje en espanol con accent",
  11. "Mensaje en español con ñ y emoji",
  12. "A" * 60,
  13. ]
  14. err = []
  15. for sample in SAMPLES:
  16. try:
  17. c = Cipher(KEY, sample)
  18. enc = c.encrypt()
  19. c2 = Cipher(KEY, enc.decode("utf-8"))
  20. dec = c2.decrypt()
  21. if dec is None:
  22. err.append(f"decrypt returned None for {sample!r}")
  23. continue
  24. dec_s = dec.decode("utf-8", errors="replace")
  25. if not sample.startswith(dec_s) and not dec_s.startswith(sample[:len(dec_s)]):
  26. err.append(f"roundtrip mismatch for {sample!r}: got {dec_s!r}")
  27. except Exception as e:
  28. err.append(f"exception for {sample!r}: {type(e).__name__}: {e}")
  29. print(f"round-trips ok: {len(SAMPLES) - len(err)} / {len(SAMPLES)}")
  30. for e in err:
  31. print("FAIL:", e)
  32. sys.exit(0 if not err else 1)