11from __future__ import annotations
22
33import base64
4- from cryptography import x509
5- from cryptography .hazmat .primitives import hashes
6- from cryptography .hazmat .primitives .asymmetric import ec
7- from cryptography .hazmat .primitives .kdf .hkdf import HKDF
8- from cryptography .hazmat .primitives .ciphers import (
9- Cipher , algorithms , modes
10- )
114
125
136def aes_gcm_encrypt_bytes (key : bytes , iv : bytes , plain_bytes : bytes , associated_data : bytes = b"" ) -> bytes :
147 # aes_gcm_encrypt_bytes encrypt message using AES-GCM
8+ from cryptography .hazmat .primitives .ciphers import (
9+ Cipher , algorithms , modes
10+ )
1511 encryptor = Cipher (
1612 algorithms .AES (key ),
1713 modes .GCM (iv ),
@@ -37,6 +33,9 @@ def aes_gcm_encrypt_base64_string(key: bytes, nonce: bytes, plaintext: str) -> s
3733def aes_gcm_decrypt_bytes (key : bytes , iv : bytes , cipher_bytes : bytes , associated_data : bytes = b"" ) -> bytes :
3834 """aes_gcm_decrypt_bytes Decrypt message from bytes to bytes using AES-GCM
3935 """
36+ from cryptography .hazmat .primitives .ciphers import (
37+ Cipher , algorithms , modes
38+ )
4039 tag_length = 16 # default aes gcm tag length
4140 cipher = cipher_bytes [:- tag_length ]
4241 tag = cipher_bytes [- tag_length :]
@@ -60,7 +59,7 @@ def aes_gcm_decrypt_base64_string(key: bytes, nonce: bytes, ciphertext: str) ->
6059 return aes_gcm_decrypt_bytes (key , nonce , cipher_bytes ).decode ()
6160
6261
63- def marshal_cryptography_pub_key (key : ec . EllipticCurvePublicNumbers ) -> bytes :
62+ def marshal_cryptography_pub_key (key ) -> bytes :
6463 # python version of crypto/elliptic/elliptic.go Marshal
6564 # without point on curve check
6665 return bytes ([4 ]) + key .x .to_bytes (32 , 'big' ) + key .y .to_bytes (32 , 'big' )
@@ -70,10 +69,19 @@ class key_agreement_client():
7069 def __init__ (self , certificate_pem_string : str ) -> None :
7170 """ Load cert and extract public key
7271 """
72+ __fixed_version__ = "43.0.3" # version check
73+ from cryptography import __version__
74+ if __version__ != __fixed_version__ :
75+ raise Exception ("The cryptography package of Ark SDK only supports version {}, "
76+ "please install the cryptography package by using pip install cryptography=={}" .
77+ format (__fixed_version__ , __fixed_version__ ))
78+ from cryptography import x509
79+ from cryptography .hazmat .primitives .asymmetric import ec
80+
7381 pem_data = certificate_pem_string .encode ()
7482 self ._cert = x509 .load_pem_x509_certificate (pem_data )
7583 cert_pub = self ._cert .public_key ().public_numbers ()
76- self ._curve = ec ._CURVE_TYPES [self ._cert .public_key ().curve .name ]()
84+ self ._curve = ec ._CURVE_TYPES [self ._cert .public_key ().curve .name ]
7785 self ._public_key = ec .EllipticCurvePublicNumbers (
7886 cert_pub .x , cert_pub .y , self ._curve ).public_key ()
7987
@@ -101,6 +109,9 @@ def decrypt_string_with_key(self, key: bytes, nonce: bytes, ciphertext: str) ->
101109 def generate_ecies_key_pair (self ) -> tuple [bytes , bytes , str ]:
102110 """generate_ecies_key_pair generate ECIES key pair
103111 """
112+ from cryptography .hazmat .primitives import hashes
113+ from cryptography .hazmat .primitives .kdf .hkdf import HKDF
114+ from cryptography .hazmat .primitives .asymmetric import ec
104115 # Generate an ephemeral elliptic curve scalar and point
105116 peer_private_key = ec .generate_private_key (self ._curve )
106117 dh = peer_private_key .exchange (ec .ECDH (), self ._public_key )
0 commit comments