|
1 |
| -# Copyright 2016-2022. Couchbase, Inc. |
| 1 | +# Copyright 2016-2025. Couchbase, Inc. |
2 | 2 | # All Rights Reserved.
|
3 | 3 | #
|
4 | 4 | # Licensed under the Apache License, Version 2.0 (the "License")
|
|
25 | 25 |
|
26 | 26 | class EncryptionResult:
|
27 | 27 | def __init__(self,
|
28 |
| - alg, # type: str |
29 |
| - kid=None, # type: Optional[str] |
30 |
| - ciphertext=None, # type: Optional[str] |
31 |
| - **kwargs, # type: Optional[Any] |
| 28 | + alg: str = '', |
| 29 | + kid: Optional[str] = None, |
| 30 | + ciphertext: Optional[str] = None, |
| 31 | + **kwargs: Any |
32 | 32 | ):
|
33 |
| - self._map = {"alg": alg} |
| 33 | + if not alg: |
| 34 | + raise InvalidArgumentException('EncryptionResult must include alg property.') |
| 35 | + |
| 36 | + self._map: dict[str, Any] = {'alg': alg} |
34 | 37 |
|
35 | 38 | if kid:
|
36 |
| - self._map["kid"] = kid |
| 39 | + self._map['kid'] = kid |
37 | 40 |
|
38 | 41 | if ciphertext and self._valid_base64(ciphertext):
|
39 |
| - self._map["ciphertext"] = ciphertext |
| 42 | + self._map['ciphertext'] = ciphertext |
40 | 43 |
|
41 | 44 | if kwargs:
|
42 | 45 | self._map.update(**kwargs)
|
43 | 46 |
|
44 | 47 | @classmethod
|
45 |
| - def new_encryption_result_from_dict(cls, |
46 |
| - values, # type: dict |
47 |
| - ) -> EncryptionResult: |
48 |
| - |
49 |
| - alg = values.pop("alg", None) |
50 |
| - if not alg: |
51 |
| - raise InvalidArgumentException( |
52 |
| - "EncryptionResult must include alg property." |
53 |
| - ) |
54 |
| - |
55 |
| - return EncryptionResult(alg, **values) |
| 48 | + def new_encryption_result_from_dict(cls, values: dict[str, Any]) -> EncryptionResult: |
| 49 | + return EncryptionResult(**values) |
56 | 50 |
|
57 |
| - def put(self, |
58 |
| - key, # type: str |
59 |
| - val # type: Any |
60 |
| - ): |
| 51 | + def put(self, key: str, val: Any) -> None: |
61 | 52 | self._map[key] = val
|
62 | 53 |
|
63 |
| - def put_and_base64_encode(self, |
64 |
| - key, # type: str |
65 |
| - val, # type: bytes |
66 |
| - ): |
| 54 | + def put_and_base64_encode(self, key: str, val: bytes) -> None: |
67 | 55 | if not isinstance(val, bytes):
|
68 |
| - raise ValueError("Provided value must be of type bytes.") |
| 56 | + raise ValueError('Provided value must be of type bytes.') |
69 | 57 | self._map[key] = base64.b64encode(val)
|
70 | 58 |
|
71 |
| - def get(self, |
72 |
| - key, # type: str |
73 |
| - ) -> Any: |
| 59 | + def get(self, key: str) -> Any: |
74 | 60 | val = self._map.get(key, None)
|
75 | 61 | if not val:
|
76 |
| - raise CryptoKeyNotFoundException( |
77 |
| - message="No mapping to EncryptionResult value found for key: '{}'.".format( |
78 |
| - key) |
79 |
| - ) |
| 62 | + raise CryptoKeyNotFoundException(message=f"No mapping to EncryptionResult value found for key: '{key}'.") |
80 | 63 |
|
81 | 64 | return val
|
82 | 65 |
|
83 | 66 | def algorithm(self) -> str:
|
84 |
| - return self._map["alg"] |
| 67 | + return self._map['alg'] |
85 | 68 |
|
86 |
| - def get_with_base64_decode(self, |
87 |
| - key, # type: str |
88 |
| - ) -> bytes: |
| 69 | + def get_with_base64_decode(self, key: str) -> bytes: |
89 | 70 | val = self._map.get(key, None)
|
90 | 71 | if not val:
|
91 |
| - raise CryptoKeyNotFoundException( |
92 |
| - message="No mapping to EncryptionResult value found for key: '{}'.".format( |
93 |
| - key) |
94 |
| - ) |
| 72 | + raise CryptoKeyNotFoundException(message=f"No mapping to EncryptionResult value found for key: '{key}'.") |
95 | 73 |
|
96 | 74 | return base64.b64decode(val)
|
97 | 75 |
|
98 | 76 | def asdict(self) -> dict:
|
99 | 77 | return self._map
|
100 | 78 |
|
101 |
| - def _valid_base64(self, |
102 |
| - val, # type: Union[str, bytes, bytearray] |
103 |
| - ) -> bool: |
| 79 | + def _valid_base64(self, val: Union[str, bytes, bytearray]) -> bool: |
104 | 80 | try:
|
105 | 81 | if isinstance(val, str):
|
106 |
| - bytes_val = bytes(val, "ascii") |
| 82 | + bytes_val = bytes(val, 'ascii') |
107 | 83 | elif isinstance(val, bytes):
|
108 | 84 | bytes_val = val
|
109 | 85 | elif isinstance(val, bytearray):
|
110 | 86 | bytes_val = val
|
111 | 87 | else:
|
112 |
| - raise ValueError( |
113 |
| - "Provided value must be of type str, bytes or bytearray" |
114 |
| - ) |
| 88 | + raise ValueError('Provided value must be of type str, bytes or bytearray') |
115 | 89 |
|
116 | 90 | return base64.b64encode(base64.b64decode(bytes_val)) == bytes_val
|
117 | 91 |
|
|
0 commit comments