Skip to content

Commit 59f3980

Browse files
author
liuhuiqi.7
committed
feat(ark cryptography dependancy): set as optional import
Change-Id: I3a6ada7f8b94cb4e4806f925ab33abffcc14d9c8
1 parent c064185 commit 59f3980

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

volcenginesdkarkruntime/_utils/_key_agreement.py

Lines changed: 12 additions & 8 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')
@@ -76,6 +75,8 @@ def __init__(self, certificate_pem_string: str) -> None:
7675
raise Exception("The cryptography package of Ark SDK only supports version {}, "
7776
"please install the cryptography package by using pip install cryptography=={}".
7877
format(__fixed_version__, __fixed_version__))
78+
from cryptography import x509
79+
from cryptography.hazmat.primitives.asymmetric import ec
7980

8081
pem_data = certificate_pem_string.encode()
8182
self._cert = x509.load_pem_x509_certificate(pem_data)
@@ -108,6 +109,9 @@ def decrypt_string_with_key(self, key: bytes, nonce: bytes, ciphertext: str) ->
108109
def generate_ecies_key_pair(self) -> tuple[bytes, bytes, str]:
109110
"""generate_ecies_key_pair generate ECIES key pair
110111
"""
112+
from cryptography.hazmat.primitives import hashes
113+
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
114+
from cryptography.hazmat.primitives.asymmetric import ec
111115
# Generate an ephemeral elliptic curve scalar and point
112116
peer_private_key = ec.generate_private_key(self._curve)
113117
dh = peer_private_key.exchange(ec.ECDH(), self._public_key)

0 commit comments

Comments
 (0)