Projeto do Agent Contas ORACLE
This commit is contained in:
30
app/domain/contas/integrations/secure_pdf_crypto.py
Normal file
30
app/domain/contas/integrations/secure_pdf_crypto.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
from urllib.parse import quote_plus, unquote_plus
|
||||
|
||||
from cryptography.hazmat.decrepit.ciphers.algorithms import Blowfish
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher, modes
|
||||
from cryptography.hazmat.primitives.padding import PKCS7
|
||||
|
||||
_SECURE_PDF_KEY = b"T1mC0nT4"
|
||||
|
||||
|
||||
def encrypt_secure_pdf_value(value: str) -> str:
|
||||
plaintext = str(value).encode("utf-8")
|
||||
padder = PKCS7(Blowfish.block_size).padder()
|
||||
padded = padder.update(plaintext) + padder.finalize()
|
||||
encryptor = Cipher(Blowfish(_SECURE_PDF_KEY), modes.ECB()).encryptor()
|
||||
ciphertext = encryptor.update(padded) + encryptor.finalize()
|
||||
encoded = base64.b64encode(ciphertext).decode("ascii")
|
||||
return quote_plus(encoded, safe="")
|
||||
|
||||
|
||||
def decrypt_secure_pdf_value(value: str) -> str:
|
||||
encoded = unquote_plus(str(value))
|
||||
ciphertext = base64.b64decode(encoded, validate=True)
|
||||
decryptor = Cipher(Blowfish(_SECURE_PDF_KEY), modes.ECB()).decryptor()
|
||||
padded = decryptor.update(ciphertext) + decryptor.finalize()
|
||||
unpadder = PKCS7(Blowfish.block_size).unpadder()
|
||||
plaintext = unpadder.update(padded) + unpadder.finalize()
|
||||
return plaintext.decode("utf-8")
|
||||
Reference in New Issue
Block a user