Skip to content

Commit eb75a9b

Browse files
authored
Merge pull request #7 from cipherstash/feat/add-bulk-encrypt
Add bulk encryption
2 parents 7ebb5a5 + ddaeb5f commit eb75a9b

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/crypto/mod.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,25 @@ where
105105
{
106106
let protected_attributes = target.protected_attributes();
107107

108-
// TODO: Maybe use a wrapper type?
109-
let mut attributes: HashMap<String, String> = Default::default();
110-
for (name, plaintext) in protected_attributes.iter() {
111-
// TODO: Use the bulk encrypt
112-
if let Some(ct) = cipher
113-
.encrypt_single(&plaintext, &format!("{}#{}", E::type_name(), name))
114-
.await?
115-
{
116-
attributes.insert(name.to_string(), ct);
117-
}
118-
}
108+
let entries_to_encrypt = protected_attributes
109+
.into_iter()
110+
.map(|(name, plaintext)| (name, plaintext, format!("{}#{}", E::type_name(), name)))
111+
.collect::<Vec<_>>();
112+
113+
let encrypted = cipher
114+
.encrypt(
115+
entries_to_encrypt
116+
.iter()
117+
.map(|(_, plaintext, descriptor)| (plaintext, descriptor.as_str())),
118+
)
119+
.await?;
120+
121+
let attributes: HashMap<String, String> = entries_to_encrypt
122+
.into_iter()
123+
.map(|(name, _, _)| name)
124+
.zip(encrypted.into_iter())
125+
.flat_map(|(name, ct)| ct.map(|ct| (name.to_string(), ct)))
126+
.collect();
119127

120128
let partition_key = encrypt_partition_key(&target.partition_key(), cipher)?;
121129

0 commit comments

Comments
 (0)