1
1
"""Main Asherah class, for encrypting and decrypting of data"""
2
+
2
3
# pylint: disable=line-too-long
3
4
4
5
from __future__ import annotations
5
6
6
7
import json
7
8
import os
8
9
from typing import ByteString , Union
9
-
10
10
from cobhan import Cobhan
11
-
12
11
from . import exceptions , types
13
12
14
13
15
14
class Asherah :
16
15
"""The main class for providing encryption and decryption functionality"""
17
16
18
- JSON_OVERHEAD = 256
19
- KEY_SIZE = 64
17
+ ENCRYPTION_OVERHEAD = 48
18
+ ENVELOPE_OVERHEAD = 185
19
+ BASE64_OVERHEAD = 1.34
20
20
21
21
def __init__ (self ):
22
22
self .__cobhan = Cobhan ()
@@ -35,6 +35,7 @@ def __init__(self):
35
35
36
36
def setup (self , config : types .AsherahConfig ) -> None :
37
37
"""Set up/initialize the underlying encryption library."""
38
+ self .ik_overhead = len (config .service_name ) + len (config .product_id )
38
39
config_json = json .dumps (config .to_json ())
39
40
config_buf = self .__cobhan .str_to_buf (config_json )
40
41
result = self .__libasherah .SetupJson (config_buf )
@@ -55,7 +56,13 @@ def encrypt(self, partition_id: str, data: Union[ByteString, str]):
55
56
partition_id_buf = self .__cobhan .str_to_buf (partition_id )
56
57
data_buf = self .__cobhan .bytearray_to_buf (data )
57
58
# 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 )
59
66
60
67
result = self .__libasherah .EncryptToJson (partition_id_buf , data_buf , json_buf )
61
68
if result < 0 :
0 commit comments