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,32 @@ 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, size_t key_hash ) {
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- }
4137 std::vector<uint8_t > out (data.size ());
42- std::hash<std::string> hasher;
43- size_t key_hash = hasher (key_id);
4438 for (size_t i = 0 ; i < data.size (); ++i) {
4539 out[i] = data[i] ^ (key_hash & 0xFF );
4640 key_hash = (key_hash << 1 ) | (key_hash >> 31 );
4741 }
4842 return out;
4943}
5044
51- std::vector<uint8_t > BasicXorEncryptor::XorDecrypt (tcb::span<const uint8_t > data, const std::string& key_id ) {
52- return XorEncrypt (data, key_id );
45+ std::vector<uint8_t > BasicXorEncryptor::XorDecrypt (tcb::span<const uint8_t > data, size_t key_hash ) {
46+ return XorEncrypt (data, key_hash );
5347}
5448
5549// ---------------------------------------------------------------------------
5650// Block encryption
5751// ---------------------------------------------------------------------------
5852
5953std::vector<uint8_t > BasicXorEncryptor::EncryptBlock (tcb::span<const uint8_t > data) {
60- return XorEncrypt (data, key_id_ );
54+ return XorEncrypt (data, key_id_hash_ );
6155}
6256
6357std::vector<uint8_t > BasicXorEncryptor::DecryptBlock (tcb::span<const uint8_t > data) {
64- return XorDecrypt (data, key_id_ );
58+ return XorDecrypt (data, key_id_hash_ );
6559}
6660
6761// ---------------------------------------------------------------------------
@@ -95,7 +89,7 @@ std::vector<uint8_t> BasicXorEncryptor::DecryptBlock(tcb::span<const uint8_t> da
9589
9690template <typename TypedBuffer>
9791std::vector<uint8_t > BasicXorEncryptor::EncryptTypedElements (
98- const TypedBuffer& input_buffer, const std::string& key_id ) {
92+ const TypedBuffer& input_buffer, size_t key_hash ) {
9993 constexpr bool is_fixed = TypedBuffer::is_fixed_sized;
10094 constexpr size_t prefix_length = is_fixed ? kFixedHeaderLength : kVariableHeaderLength ;
10195 const size_t num_elements = input_buffer.GetNumElements ();
@@ -122,7 +116,7 @@ std::vector<uint8_t> BasicXorEncryptor::EncryptTypedElements(
122116 num_elements, prefix_length, RawBytesFixedSizedCodec{element_size}};
123117 size_t output_index = 0 ;
124118 for (const auto raw_bytes : input_buffer.raw_elements ()) {
125- auto encrypted = XorEncrypt (raw_bytes, key_id );
119+ auto encrypted = XorEncrypt (raw_bytes, key_hash );
126120 output_buffer.SetElement (output_index, tcb::span<const uint8_t >(encrypted));
127121 output_index++;
128122 }
@@ -136,7 +130,7 @@ std::vector<uint8_t> BasicXorEncryptor::EncryptTypedElements(
136130 num_elements, reserved_bytes_hint, true , prefix_length};
137131 size_t output_index = 0 ;
138132 for (const auto raw_bytes : input_buffer.raw_elements ()) {
139- auto encrypted = XorEncrypt (raw_bytes, key_id );
133+ auto encrypted = XorEncrypt (raw_bytes, key_hash );
140134 output_buffer.SetElement (output_index, tcb::span<const uint8_t >(encrypted));
141135 output_index++;
142136 }
@@ -167,7 +161,7 @@ std::vector<uint8_t> BasicXorEncryptor::EncryptValueList(
167161 // std::visit extracts the concrete buffer type from the TypedValuesBuffer variant
168162 // and forwards it to EncryptTypedElements, which handles all buffer types generically.
169163 return std::visit ([&](const auto & input_buffer) {
170- return EncryptTypedElements (input_buffer, key_id_ );
164+ return EncryptTypedElements (input_buffer, key_id_hash_ );
171165 }, typed_buffer);
172166}
173167
@@ -181,10 +175,10 @@ std::vector<uint8_t> BasicXorEncryptor::EncryptValueList(
181175// Helper function to decrypt fixed-size elements into the output TypedBuffer type.
182176template <typename TypedBuffer>
183177TypedBuffer BasicXorEncryptor::DecryptFixedSizedElementsIntoTypedBuffer (
184- const TypedBufferRawBytesFixedSized& encrypted_buffer, const std::string& key_id , TypedBuffer output_buffer) {
178+ const TypedBufferRawBytesFixedSized& encrypted_buffer, size_t key_hash , TypedBuffer output_buffer) {
185179 size_t output_index = 0 ;
186180 for (const auto raw_bytes : encrypted_buffer.raw_elements ()) {
187- auto decrypted_bytes = XorDecrypt (raw_bytes, key_id );
181+ auto decrypted_bytes = XorDecrypt (raw_bytes, key_hash );
188182 output_buffer.SetRawElement (output_index, tcb::span<const uint8_t >(decrypted_bytes));
189183 output_index++;
190184 }
@@ -208,22 +202,22 @@ TypedValuesBuffer BasicXorEncryptor::DecryptValueList(
208202 switch (datatype_) {
209203 case Type::INT32 :
210204 return DecryptFixedSizedElementsIntoTypedBuffer (
211- encrypted_buffer, key_id_ , TypedBufferI32{num_elements});
205+ encrypted_buffer, key_id_hash_ , TypedBufferI32{num_elements});
212206 case Type::INT64 :
213207 return DecryptFixedSizedElementsIntoTypedBuffer (
214- encrypted_buffer, key_id_ , TypedBufferI64{num_elements});
208+ encrypted_buffer, key_id_hash_ , TypedBufferI64{num_elements});
215209 case Type::INT96 :
216210 return DecryptFixedSizedElementsIntoTypedBuffer (
217- encrypted_buffer, key_id_ , TypedBufferInt96{num_elements});
211+ encrypted_buffer, key_id_hash_ , TypedBufferInt96{num_elements});
218212 case Type::FLOAT :
219213 return DecryptFixedSizedElementsIntoTypedBuffer (
220- encrypted_buffer, key_id_ , TypedBufferFloat{num_elements});
214+ encrypted_buffer, key_id_hash_ , TypedBufferFloat{num_elements});
221215 case Type::DOUBLE :
222216 return DecryptFixedSizedElementsIntoTypedBuffer (
223- encrypted_buffer, key_id_ , TypedBufferDouble{num_elements});
217+ encrypted_buffer, key_id_hash_ , TypedBufferDouble{num_elements});
224218 case Type::FIXED_LEN_BYTE_ARRAY :
225219 return DecryptFixedSizedElementsIntoTypedBuffer (
226- encrypted_buffer, key_id_ ,
220+ encrypted_buffer, key_id_hash_ ,
227221 TypedBufferRawBytesFixedSized{num_elements, 0 , RawBytesFixedSizedCodec{header.element_size }});
228222 default :
229223 throw InvalidInputException (
@@ -244,7 +238,7 @@ TypedValuesBuffer BasicXorEncryptor::DecryptValueList(
244238 TypedBufferRawBytesVariableSized output_buffer{num_elements, reserved_bytes_hint, true };
245239 size_t output_index = 0 ;
246240 for (const auto element : encrypted_buffer) {
247- auto decrypted_bytes = XorDecrypt (element, key_id_ );
241+ auto decrypted_bytes = XorDecrypt (element, key_id_hash_ );
248242 output_buffer.SetElement (output_index, tcb::span<const uint8_t >(decrypted_bytes));
249243 output_index++;
250244 }
0 commit comments