2020#include " ../../common/exceptions.h"
2121#include " ../../common/enum_utils.h"
2222#include < cstring>
23- #include < functional>
2423#include < iostream>
2524#include < type_traits>
2625
@@ -31,37 +30,33 @@ using namespace dbps::external;
3130// Functions for encrypting and decrypting byte arrays.
3231// ---------------------------------------------------------------------------
3332
34- std::vector<uint8_t > BasicXorEncryptor::XorEncrypt (tcb::span<const uint8_t > data, const std::string& key_id ) {
33+ std::vector<uint8_t > BasicXorEncryptor::XorEncrypt (tcb::span<const uint8_t > data) {
3534 if (data.empty ()) {
3635 return {};
3736 }
38- if (key_id.empty ()) {
39- throw std::invalid_argument (" XorEncrypt: key must not be empty for non-empty data" );
40- }
37+ size_t key_hash = key_id_hash_;
4138 std::vector<uint8_t > out (data.size ());
42- std::hash<std::string> hasher;
43- size_t key_hash = hasher (key_id);
4439 for (size_t i = 0 ; i < data.size (); ++i) {
4540 out[i] = data[i] ^ (key_hash & 0xFF );
4641 key_hash = (key_hash << 1 ) | (key_hash >> 31 );
4742 }
4843 return out;
4944}
5045
51- std::vector<uint8_t > BasicXorEncryptor::XorDecrypt (tcb::span<const uint8_t > data, const std::string& key_id ) {
52- return XorEncrypt (data, key_id );
46+ std::vector<uint8_t > BasicXorEncryptor::XorDecrypt (tcb::span<const uint8_t > data) {
47+ return XorEncrypt (data);
5348}
5449
5550// ---------------------------------------------------------------------------
5651// Block encryption
5752// ---------------------------------------------------------------------------
5853
5954std::vector<uint8_t > BasicXorEncryptor::EncryptBlock (tcb::span<const uint8_t > data) {
60- return XorEncrypt (data, key_id_ );
55+ return XorEncrypt (data);
6156}
6257
6358std::vector<uint8_t > BasicXorEncryptor::DecryptBlock (tcb::span<const uint8_t > data) {
64- return XorDecrypt (data, key_id_ );
59+ return XorDecrypt (data);
6560}
6661
6762// ---------------------------------------------------------------------------
@@ -95,7 +90,7 @@ std::vector<uint8_t> BasicXorEncryptor::DecryptBlock(tcb::span<const uint8_t> da
9590
9691template <typename TypedBuffer>
9792std::vector<uint8_t > BasicXorEncryptor::EncryptTypedElements (
98- const TypedBuffer& input_buffer, const std::string& key_id ) {
93+ const TypedBuffer& input_buffer) {
9994 constexpr bool is_fixed = TypedBuffer::is_fixed_sized;
10095 constexpr size_t prefix_length = is_fixed ? kFixedHeaderLength : kVariableHeaderLength ;
10196 const size_t num_elements = input_buffer.GetNumElements ();
@@ -122,7 +117,7 @@ std::vector<uint8_t> BasicXorEncryptor::EncryptTypedElements(
122117 num_elements, prefix_length, RawBytesFixedSizedCodec{element_size}};
123118 size_t output_index = 0 ;
124119 for (const auto raw_bytes : input_buffer.raw_elements ()) {
125- auto encrypted = XorEncrypt (raw_bytes, key_id );
120+ auto encrypted = XorEncrypt (raw_bytes);
126121 output_buffer.SetElement (output_index, tcb::span<const uint8_t >(encrypted));
127122 output_index++;
128123 }
@@ -136,7 +131,7 @@ std::vector<uint8_t> BasicXorEncryptor::EncryptTypedElements(
136131 num_elements, reserved_bytes_hint, true , prefix_length};
137132 size_t output_index = 0 ;
138133 for (const auto raw_bytes : input_buffer.raw_elements ()) {
139- auto encrypted = XorEncrypt (raw_bytes, key_id );
134+ auto encrypted = XorEncrypt (raw_bytes);
140135 output_buffer.SetElement (output_index, tcb::span<const uint8_t >(encrypted));
141136 output_index++;
142137 }
@@ -167,7 +162,7 @@ std::vector<uint8_t> BasicXorEncryptor::EncryptValueList(
167162 // std::visit extracts the concrete buffer type from the TypedValuesBuffer variant
168163 // and forwards it to EncryptTypedElements, which handles all buffer types generically.
169164 return std::visit ([&](const auto & input_buffer) {
170- return EncryptTypedElements (input_buffer, key_id_ );
165+ return EncryptTypedElements (input_buffer);
171166 }, typed_buffer);
172167}
173168
@@ -181,10 +176,10 @@ std::vector<uint8_t> BasicXorEncryptor::EncryptValueList(
181176// Helper function to decrypt fixed-size elements into the output TypedBuffer type.
182177template <typename TypedBuffer>
183178TypedBuffer BasicXorEncryptor::DecryptFixedSizedElementsIntoTypedBuffer (
184- const TypedBufferRawBytesFixedSized& encrypted_buffer, const std::string& key_id, TypedBuffer output_buffer) {
179+ const TypedBufferRawBytesFixedSized& encrypted_buffer, TypedBuffer output_buffer) {
185180 size_t output_index = 0 ;
186181 for (const auto raw_bytes : encrypted_buffer.raw_elements ()) {
187- auto decrypted_bytes = XorDecrypt (raw_bytes, key_id );
182+ auto decrypted_bytes = XorDecrypt (raw_bytes);
188183 output_buffer.SetRawElement (output_index, tcb::span<const uint8_t >(decrypted_bytes));
189184 output_index++;
190185 }
@@ -208,22 +203,22 @@ TypedValuesBuffer BasicXorEncryptor::DecryptValueList(
208203 switch (datatype_) {
209204 case Type::INT32 :
210205 return DecryptFixedSizedElementsIntoTypedBuffer (
211- encrypted_buffer, key_id_, TypedBufferI32{num_elements});
206+ encrypted_buffer, TypedBufferI32{num_elements});
212207 case Type::INT64 :
213208 return DecryptFixedSizedElementsIntoTypedBuffer (
214- encrypted_buffer, key_id_, TypedBufferI64{num_elements});
209+ encrypted_buffer, TypedBufferI64{num_elements});
215210 case Type::INT96 :
216211 return DecryptFixedSizedElementsIntoTypedBuffer (
217- encrypted_buffer, key_id_, TypedBufferInt96{num_elements});
212+ encrypted_buffer, TypedBufferInt96{num_elements});
218213 case Type::FLOAT :
219214 return DecryptFixedSizedElementsIntoTypedBuffer (
220- encrypted_buffer, key_id_, TypedBufferFloat{num_elements});
215+ encrypted_buffer, TypedBufferFloat{num_elements});
221216 case Type::DOUBLE :
222217 return DecryptFixedSizedElementsIntoTypedBuffer (
223- encrypted_buffer, key_id_, TypedBufferDouble{num_elements});
218+ encrypted_buffer, TypedBufferDouble{num_elements});
224219 case Type::FIXED_LEN_BYTE_ARRAY :
225220 return DecryptFixedSizedElementsIntoTypedBuffer (
226- encrypted_buffer, key_id_,
221+ encrypted_buffer,
227222 TypedBufferRawBytesFixedSized{num_elements, 0 , RawBytesFixedSizedCodec{header.element_size }});
228223 default :
229224 throw InvalidInputException (
@@ -244,7 +239,7 @@ TypedValuesBuffer BasicXorEncryptor::DecryptValueList(
244239 TypedBufferRawBytesVariableSized output_buffer{num_elements, reserved_bytes_hint, true };
245240 size_t output_index = 0 ;
246241 for (const auto element : encrypted_buffer) {
247- auto decrypted_bytes = XorDecrypt (element, key_id_ );
242+ auto decrypted_bytes = XorDecrypt (element);
248243 output_buffer.SetElement (output_index, tcb::span<const uint8_t >(decrypted_bytes));
249244 output_index++;
250245 }
0 commit comments