forked from langgenius/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for PKCS1OAEP_Cipher with gmpy2 (langgenius#3760)
- Loading branch information
1 parent
b249767
commit a34e8cb
Showing
4 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import rsa as pyrsa | ||
from Crypto.PublicKey import RSA | ||
|
||
from libs import gmpy2_pkcs10aep_cipher | ||
|
||
|
||
def test_gmpy2_pkcs10aep_cipher() -> None: | ||
rsa_key_pair = pyrsa.newkeys(2048) | ||
public_key = rsa_key_pair[0].save_pkcs1() | ||
private_key = rsa_key_pair[1].save_pkcs1() | ||
|
||
public_rsa_key = RSA.import_key(public_key) | ||
public_cipher_rsa2 = gmpy2_pkcs10aep_cipher.new(public_rsa_key) | ||
|
||
private_rsa_key = RSA.import_key(private_key) | ||
private_cipher_rsa = gmpy2_pkcs10aep_cipher.new(private_rsa_key) | ||
|
||
raw_text = 'raw_text' | ||
raw_text_bytes = raw_text.encode() | ||
|
||
# RSA encryption by public key and decryption by private key | ||
encrypted_by_pub_key = public_cipher_rsa2.encrypt(message=raw_text_bytes) | ||
decrypted_by_pub_key = private_cipher_rsa.decrypt(encrypted_by_pub_key) | ||
assert decrypted_by_pub_key == raw_text_bytes | ||
|
||
# RSA encryption and decryption by private key | ||
encrypted_by_private_key = private_cipher_rsa.encrypt(message=raw_text_bytes) | ||
decrypted_by_private_key = private_cipher_rsa.decrypt(encrypted_by_private_key) | ||
assert decrypted_by_private_key == raw_text_bytes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ dev/pytest/pytest_tools.sh | |
|
||
# Workflow | ||
dev/pytest/pytest_workflow.sh | ||
|
||
# Unit tests | ||
dev/pytest/pytest_unit_tests.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
set -x | ||
|
||
# libs | ||
pytest api/tests/unit_tests/libs |