Skip to content

Commit 5265878

Browse files
authored
Merge pull request #14 from thoag-godaddy/large_input_support
result buffer calculation aligned with typescript solution. 1mb testcase
2 parents 384dfd3 + c270044 commit 5265878

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

asherah/asherah.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
"""Main Asherah class, for encrypting and decrypting of data"""
2+
23
# pylint: disable=line-too-long
34

45
from __future__ import annotations
56

67
import json
78
import os
89
from typing import ByteString, Union
9-
1010
from cobhan import Cobhan
11-
1211
from . import exceptions, types
1312

1413

1514
class Asherah:
1615
"""The main class for providing encryption and decryption functionality"""
1716

18-
JSON_OVERHEAD = 256
19-
KEY_SIZE = 64
17+
ENCRYPTION_OVERHEAD = 48
18+
ENVELOPE_OVERHEAD = 185
19+
BASE64_OVERHEAD = 1.34
2020

2121
def __init__(self):
2222
self.__cobhan = Cobhan()
@@ -35,6 +35,7 @@ def __init__(self):
3535

3636
def setup(self, config: types.AsherahConfig) -> None:
3737
"""Set up/initialize the underlying encryption library."""
38+
self.ik_overhead = len(config.service_name) + len(config.product_id)
3839
config_json = json.dumps(config.to_json())
3940
config_buf = self.__cobhan.str_to_buf(config_json)
4041
result = self.__libasherah.SetupJson(config_buf)
@@ -55,7 +56,13 @@ def encrypt(self, partition_id: str, data: Union[ByteString, str]):
5556
partition_id_buf = self.__cobhan.str_to_buf(partition_id)
5657
data_buf = self.__cobhan.bytearray_to_buf(data)
5758
# Outputs
58-
json_buf = self.__cobhan.allocate_buf(len(data_buf) + self.JSON_OVERHEAD)
59+
buffer_estimate = int(
60+
self.ENVELOPE_OVERHEAD
61+
+ self.ik_overhead
62+
+ len(partition_id_buf)
63+
+ ((len(data_buf) + self.ENCRYPTION_OVERHEAD) * self.BASE64_OVERHEAD)
64+
)
65+
json_buf = self.__cobhan.allocate_buf(buffer_estimate)
5966

6067
result = self.__libasherah.EncryptToJson(partition_id_buf, data_buf, json_buf)
6168
if result < 0:

tests/test_asherah.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ def test_decrypted_data_equals_original_data(self):
3535
encrypted = self.asherah.encrypt("partition", data)
3636
decrypted = self.asherah.decrypt("partition", encrypted)
3737
self.assertEqual(decrypted, data)
38+
39+
def test_encrypt_decrypt_large_data(self):
40+
data = b"a" * 1024 * 1024
41+
encrypted = self.asherah.encrypt("partition", data)
42+
decrypted = self.asherah.decrypt("partition", encrypted)
43+
self.assertEqual(decrypted, data)

0 commit comments

Comments
 (0)