@@ -48,15 +48,10 @@ static std::unique_ptr<DBPSEncryptor> CreateEncryptor(
4848 const std::string& key_id,
4949 const std::string& column_name,
5050 const std::string& user_id,
51- const std::string& application_context,
52- const std::string& encryptor_type) {
51+ const std::string& application_context) {
5352
5453 // Return a BasicEncryptor instance.
55- if (encryptor_type == " basic" ) {
56- return std::make_unique<BasicEncryptor>(key_id, column_name, user_id, application_context);
57- }
58- // TODO: Add other encryptor types here (e.g., ProtegrityEncryptor).
59- throw DBPSUnsupportedException (" Unsupported encryptor type: " + encryptor_type);
54+ return std::make_unique<BasicEncryptor>(key_id, column_name, user_id, application_context);
6055}
6156
6257// Constructor implementation
@@ -83,7 +78,34 @@ DataBatchEncryptionSequencer::DataBatchEncryptionSequencer(
8378 user_id_(user_id),
8479 application_context_(application_context),
8580 encryption_metadata_(encryption_metadata),
86- encryptor_(CreateEncryptor(key_id, column_name, user_id, application_context, " basic" )) {}
81+ encryptor_(CreateEncryptor(key_id, column_name, user_id, application_context)) {}
82+
83+ // Constructor with pre-built encryptor
84+ DataBatchEncryptionSequencer::DataBatchEncryptionSequencer (
85+ const std::string& column_name,
86+ Type::type datatype,
87+ const std::optional<int >& datatype_length,
88+ CompressionCodec::type compression,
89+ Format::type format,
90+ const std::map<std::string, std::string>& encoding_attributes,
91+ CompressionCodec::type encrypted_compression,
92+ const std::string& key_id,
93+ const std::string& user_id,
94+ const std::string& application_context,
95+ const std::map<std::string, std::string>& encryption_metadata,
96+ std::unique_ptr<DBPSEncryptor> encryptor
97+ ) : column_name_(column_name),
98+ datatype_(datatype),
99+ datatype_length_(datatype_length),
100+ compression_(compression),
101+ format_(format),
102+ encoding_attributes_(encoding_attributes),
103+ encrypted_compression_(encrypted_compression),
104+ key_id_(key_id),
105+ user_id_(user_id),
106+ application_context_(application_context),
107+ encryption_metadata_(encryption_metadata),
108+ encryptor_(std::move(encryptor)) {}
87109
88110// Top level encryption/decryption methods.
89111
@@ -108,11 +130,13 @@ bool DataBatchEncryptionSequencer::ConvertAndEncrypt(const std::vector<uint8_t>&
108130 // Parse value bytes into typed list
109131 auto typed_list = ParseValueBytesIntoTypedList (value_bytes, datatype_, datatype_length_, format_);
110132
111- // Encrypt the typed list and level bytes
112- auto encrypted_bytes = encryptor_->EncryptValueList (typed_list, level_bytes);
133+ // Encrypt the typed list and level bytes, then join them into a single encrypted byte vector.
134+ auto encrypted_value_bytes = encryptor_->EncryptValueList (typed_list);
135+ auto encrypted_level_bytes = encryptor_->EncryptBlock (level_bytes);
136+ auto joined_encrypted_bytes = JoinWithLengthPrefix (encrypted_level_bytes, encrypted_value_bytes);
113137
114- // Compress the encrypted bytes
115- encrypted_result_ = Compress (encrypted_bytes , encrypted_compression_);
138+ // Compress the joined encrypted bytes
139+ encrypted_result_ = Compress (joined_encrypted_bytes , encrypted_compression_);
116140
117141 } catch (const DBPSUnsupportedException& e) {
118142 // If any stage is as of yet unsupported, default to whole payload (per-block) encryption
@@ -175,13 +199,15 @@ bool DataBatchEncryptionSequencer::ConvertAndDecrypt(const std::vector<uint8_t>&
175199 // Decompress the encrypted bytes
176200 auto decompressed_encrypted_bytes = Decompress (ciphertext, encrypted_compression_);
177201
178- // Decrypt the typed list and level bytes
179- auto [typed_list, level_bytes] = encryptor_->DecryptValueList (decompressed_encrypted_bytes);
202+ // Split the joined encrypted bytes, then decrypt the level and value bytes separately.
203+ auto [encrypted_level_bytes, encrypted_value_bytes] = SplitWithLengthPrefix (decompressed_encrypted_bytes);
204+ auto level_bytes = encryptor_->DecryptBlock (encrypted_level_bytes);
205+ auto typed_list = encryptor_->DecryptValueList (encrypted_value_bytes);
180206
181- // Convert typed list back to value bytes
207+ // Convert the decrypted typed list back to value bytes
182208 auto value_bytes = GetTypedListAsValueBytes (typed_list, datatype_, datatype_length_, format_);
183209
184- // Join level and value bytes and compress to get plaintext
210+ // Join the decrypted level and value bytes, then compress to get plaintext
185211 decrypted_result_ = CompressAndJoin (level_bytes, value_bytes);
186212 }
187213
0 commit comments