009-application_crypter.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import pathlib
  2. current = pathlib.Path(__file__).parent
  3. root = current.parent
  4. import sys
  5. sys.path.append(str(root))
  6. import assets
  7. def drop_db() -> None:
  8. db = pathlib.Path("./009-application_crypter.db")
  9. if db.is_file():
  10. db.unlink()
  11. drop_db()
  12. import sqlmodel
  13. connection = sqlmodel.create_engine("sqlite:///009-application_crypted.db")
  14. user_app = assets.application_user(connection)
  15. crypter_app = assets.application_crypter(connection)
  16. sqlmodel.SQLModel.metadata.create_all(connection)
  17. user_app.register("test", "password")
  18. apikey = user_app.login("test", "password")["apikey"]
  19. print("Create test user, apikey: " + apikey)
  20. code_key = user_app.get(apikey)["code_key"]
  21. coder = assets.code_key_manager("password", code_key).coder
  22. outside_sample_plain = "SECRET"
  23. outside_sample_crypted = coder.encrypt(outside_sample_plain)
  24. print()
  25. print("Testing encryption works...")
  26. result_a = crypter_app.encrypt(apikey, "password", outside_sample_plain)
  27. print("Crypted:")
  28. print(result_a)
  29. print("Encrypted with other coder:")
  30. print(coder.decrypt(result_a["crypted"]))
  31. print()
  32. print("Testing decryption works...")
  33. print("Decrypted:")
  34. print(crypter_app.decrypt(apikey, "password", outside_sample_crypted))
  35. print()
  36. print("Testing bad password:")
  37. print(crypter_app.decrypt(apikey, "bad_psk", outside_sample_crypted))
  38. print()
  39. print("Testing with bad apikey:")
  40. print(crypter_app.decrypt("NOT_EXISTS", "bad_psk", outside_sample_crypted))
  41. drop_db()