Unverified Commit a96cae4f authored by Yeuoly's avatar Yeuoly Committed by GitHub

refine: faster rsa implement (#2182)

parent 7cb75cb2
This diff is collapsed.
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
import hashlib import hashlib
from Crypto.Cipher import AES, PKCS1_OAEP from Crypto.Cipher import AES
from Crypto.PublicKey import RSA from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes from Crypto.Random import get_random_bytes
from extensions.ext_redis import redis_client from extensions.ext_redis import redis_client
from extensions.ext_storage import storage from extensions.ext_storage import storage
import libs.gmpy2_pkcs10aep_cipher as gmpy2_pkcs10aep_cipher
def generate_key_pair(tenant_id): def generate_key_pair(tenant_id):
private_key = RSA.generate(2048) private_key = RSA.generate(2048)
...@@ -35,7 +36,7 @@ def encrypt(text, public_key): ...@@ -35,7 +36,7 @@ def encrypt(text, public_key):
ciphertext, tag = cipher_aes.encrypt_and_digest(text.encode()) ciphertext, tag = cipher_aes.encrypt_and_digest(text.encode())
rsa_key = RSA.import_key(public_key) rsa_key = RSA.import_key(public_key)
cipher_rsa = PKCS1_OAEP.new(rsa_key) cipher_rsa = gmpy2_pkcs10aep_cipher.new(rsa_key)
enc_aes_key = cipher_rsa.encrypt(aes_key) enc_aes_key = cipher_rsa.encrypt(aes_key)
...@@ -58,7 +59,7 @@ def get_decrypt_decoding(tenant_id): ...@@ -58,7 +59,7 @@ def get_decrypt_decoding(tenant_id):
redis_client.setex(cache_key, 120, private_key) redis_client.setex(cache_key, 120, private_key)
rsa_key = RSA.import_key(private_key) rsa_key = RSA.import_key(private_key)
cipher_rsa = PKCS1_OAEP.new(rsa_key) cipher_rsa = gmpy2_pkcs10aep_cipher.new(rsa_key)
return rsa_key, cipher_rsa return rsa_key, cipher_rsa
......
...@@ -65,4 +65,5 @@ httpx[socks]~=0.24.1 ...@@ -65,4 +65,5 @@ httpx[socks]~=0.24.1
pydub~=0.25.1 pydub~=0.25.1
matplotlib~=3.8.2 matplotlib~=3.8.2
yfinance~=0.2.35 yfinance~=0.2.35
pydub~=0.25.1 pydub~=0.25.1
\ No newline at end of file gmpy2~=2.1.5
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment