@@ -68,8 +68,11 @@ def encrypt_as_json(key: Union[str, bytes], data: Union[str, Dict[str, Any]],
68
68
69
69
ciphertext = encrypt_bin (key , data , cipher = bcipher )
70
70
71
- # TODO - adjust depending on cipher, sizes are different
72
- nonce , ciphertext , tag = ciphertext [:16 ], ciphertext [16 :- 16 ], ciphertext [- 16 :]
71
+ # extract nonce/tag depending on cipher, sizes are different
72
+ if cipher in aes_ciphers :
73
+ nonce , ciphertext , tag = ciphertext [:16 ], ciphertext [16 :- 16 ], ciphertext [- 16 :]
74
+ else :
75
+ nonce , ciphertext , tag = ciphertext [:12 ], ciphertext [12 :- 16 ], ciphertext [- 16 :]
73
76
74
77
encoder = pybase64 .b64encode if cipher in b64_ciphers else hexlify
75
78
@@ -108,7 +111,6 @@ def decrypt_from_json(key: Union[str, bytes], data: Union[str, bytes], cipher: J
108
111
109
112
ciphertext = decoder (data ["ciphertext" ])
110
113
if "tag" not in data : # web crypto compatibility
111
- # TODO - adjust depending on cipher, sizes are different
112
114
ciphertext , tag = ciphertext [:- 16 ], ciphertext [- 16 :]
113
115
else :
114
116
tag = decoder (data ["tag" ])
@@ -169,8 +171,11 @@ def decrypt_bin(key: Union[str, bytes], ciphertext: bytes, cipher: BinaryCiphers
169
171
if cipher not in BinaryCiphers :
170
172
raise InvalidCipher (f"Invalid binary cipher: { str (cipher )} " )
171
173
172
- # TODO - adjust depending on cipher, sizes are different
173
- nonce , ciphertext , tag = ciphertext [:16 ], ciphertext [16 :- 16 ], ciphertext [- 16 :]
174
+ # extract nonce/tag depending on cipher, sizes are different
175
+ if cipher == BinaryCiphers .BINARY_AES_GCM_128 :
176
+ nonce , ciphertext , tag = ciphertext [:16 ], ciphertext [16 :- 16 ], ciphertext [- 16 :]
177
+ else :
178
+ nonce , ciphertext , tag = ciphertext [:12 ], ciphertext [12 :- 16 ], ciphertext [- 16 :]
174
179
175
180
decryptor = decrypt_AES_GCM_128 if cipher == BinaryCiphers .BINARY_AES_GCM_128 else decrypt_ChaCha20_Poly1305
176
181
try :
0 commit comments