encrypter.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. import base64
  2. from extensions.ext_database import db
  3. from libs import rsa
  4. from models.account import Tenant
  5. def obfuscated_token(token: str):
  6. return token[:6] + '*' * (len(token) - 8) + token[-2:]
  7. def encrypt_token(tenant_id: str, token: str):
  8. tenant = db.session.query(Tenant).filter(Tenant.id == tenant_id).first()
  9. encrypted_token = rsa.encrypt(token, tenant.encrypt_public_key)
  10. return base64.b64encode(encrypted_token).decode()
  11. def decrypt_token(tenant_id: str, token: str):
  12. return rsa.decrypt(base64.b64decode(token), tenant_id)
  13. def batch_decrypt_token(tenant_id: str, tokens: list[str]):
  14. rsa_key, cipher_rsa = rsa.get_decrypt_decoding(tenant_id)
  15. return [rsa.decrypt_token_with_decoding(base64.b64decode(token), rsa_key, cipher_rsa) for token in tokens]
  16. def get_decrypt_decoding(tenant_id: str):
  17. return rsa.get_decrypt_decoding(tenant_id)
  18. def decrypt_token_with_decoding(token: str, rsa_key, cipher_rsa):
  19. return rsa.decrypt_token_with_decoding(base64.b64decode(token), rsa_key, cipher_rsa)