Skip to content

Commit 1f0ceb1

Browse files
author
BitsAdmin
committed
Merge branch 'feat/pycryptography' into 'integration_2024-11-28_165707478799'
feat: [development task] ark-runtime-manual-Python (880625) See merge request iaasng/volcengine-python-sdk!439
2 parents 3a8236a + 59f3980 commit 1f0ceb1

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"httpx>=0.23.0, <1",
3030
"anyio>=3.5.0, <5",
3131
"cached-property; python_version < '3.8'",
32-
"cryptography>=38.0.4, <39.0.0"
32+
"cryptography>=43.0.3, <43.0.4"
3333
]
3434
},
3535
)

volcenginesdkarkruntime/_utils/_key_agreement.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
from __future__ import annotations
22

33
import 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

136
def 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
3733
def 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

Comments
 (0)