Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

encrypter.py 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import base64
  2. from libs import rsa
  3. def obfuscated_token(token: str):
  4. if not token:
  5. return token
  6. if len(token) <= 8:
  7. return "*" * 20
  8. return token[:6] + "*" * 12 + token[-2:]
  9. def encrypt_token(tenant_id: str, token: str):
  10. from models.account import Tenant
  11. from models.engine import db
  12. if not (tenant := db.session.query(Tenant).where(Tenant.id == tenant_id).first()):
  13. raise ValueError(f"Tenant with id {tenant_id} not found")
  14. assert tenant.encrypt_public_key is not None
  15. encrypted_token = rsa.encrypt(token, tenant.encrypt_public_key)
  16. return base64.b64encode(encrypted_token).decode()
  17. def decrypt_token(tenant_id: str, token: str) -> str:
  18. return rsa.decrypt(base64.b64decode(token), tenant_id)
  19. def batch_decrypt_token(tenant_id: str, tokens: list[str]):
  20. rsa_key, cipher_rsa = rsa.get_decrypt_decoding(tenant_id)
  21. return [rsa.decrypt_token_with_decoding(base64.b64decode(token), rsa_key, cipher_rsa) for token in tokens]
  22. def get_decrypt_decoding(tenant_id: str):
  23. return rsa.get_decrypt_decoding(tenant_id)
  24. def decrypt_token_with_decoding(token: str, rsa_key, cipher_rsa):
  25. return rsa.decrypt_token_with_decoding(base64.b64decode(token), rsa_key, cipher_rsa)