Skip to content

Commit 5da95cc

Browse files
committed
Add EncryptionKey enum for the key
1 parent 330a3be commit 5da95cc

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

libsql/examples/encryption_sync.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Example of using offline writes with encryption
22

3-
use libsql::{params, Builder, EncryptionContext};
3+
use libsql::{params, Builder, EncryptionContext, EncryptionKey};
44

55
#[tokio::main]
66
async fn main() {
@@ -18,7 +18,7 @@ async fn main() {
1818
// Optional encryption key for the database, if provided.
1919
let encryption = if let Ok(key) = std::env::var("LIBSQL_ENCRYPTION_KEY") {
2020
Some(EncryptionContext {
21-
key_32_bytes_base64_encoded: key.to_string(),
21+
key: EncryptionKey::Base64Encoded(key),
2222
})
2323
} else {
2424
None

libsql/src/hrana/hyper.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ impl HttpSender {
6868
}
6969

7070
if let Some(remote_encryption) = &self.remote_encryption {
71-
req_builder = req_builder.header(
72-
"x-turso-encryption-key",
73-
remote_encryption.key_32_bytes_base64_encoded.as_str(),
74-
);
71+
req_builder =
72+
req_builder.header("x-turso-encryption-key", remote_encryption.key.as_string());
7573
}
7674

7775
let req = req_builder

libsql/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ cfg_sync! {
133133
mod sync;
134134
pub use database::SyncProtocol;
135135
pub use sync::EncryptionContext;
136+
pub use sync::EncryptionKey;
136137
}
137138

138139
cfg_replication! {

libsql/src/sync.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::{local::Connection, util::ConnectorService, Error, Result};
22

3-
use std::path::Path;
4-
3+
use base64::engine::general_purpose;
4+
use base64::Engine;
55
use bytes::Bytes;
66
use chrono::Utc;
77
use http::{HeaderValue, StatusCode};
88
use hyper::Body;
9+
use std::path::Path;
910
use tokio::io::AsyncWriteExt as _;
1011
use uuid::Uuid;
1112

@@ -118,10 +119,27 @@ struct PushFramesResult {
118119
baton: Option<String>,
119120
}
120121

122+
#[derive(Debug, Clone)]
123+
pub enum EncryptionKey {
124+
/// The key is a base64-encoded string.
125+
Base64Encoded(String),
126+
/// The key is a byte array.
127+
Bytes(Vec<u8>),
128+
}
129+
130+
impl EncryptionKey {
131+
pub fn as_string(&self) -> String {
132+
match self {
133+
EncryptionKey::Base64Encoded(s) => s.clone(),
134+
EncryptionKey::Bytes(b) => general_purpose::STANDARD.encode(b),
135+
}
136+
}
137+
}
138+
121139
#[derive(Debug, Clone)]
122140
pub struct EncryptionContext {
123141
/// The base64-encoded key for the encryption, sent on every request.
124-
pub key_32_bytes_base64_encoded: String,
142+
pub key: EncryptionKey,
125143
}
126144

127145
pub struct SyncContext {
@@ -314,10 +332,7 @@ impl SyncContext {
314332
}
315333

316334
if let Some(remote_encryption) = &self.remote_encryption {
317-
req = req.header(
318-
"x-turso-encryption-key",
319-
remote_encryption.key_32_bytes_base64_encoded.as_str(),
320-
);
335+
req = req.header("x-turso-encryption-key", remote_encryption.key.as_string());
321336
}
322337

323338
let req = req.body(body.clone().into()).expect("valid body");
@@ -432,10 +447,7 @@ impl SyncContext {
432447
}
433448

434449
if let Some(remote_encryption) = &self.remote_encryption {
435-
req = req.header(
436-
"x-turso-encryption-key",
437-
remote_encryption.key_32_bytes_base64_encoded.as_str(),
438-
);
450+
req = req.header("x-turso-encryption-key", remote_encryption.key.as_string());
439451
}
440452

441453
let req = req.body(Body::empty()).expect("valid request");
@@ -602,10 +614,7 @@ impl SyncContext {
602614
}
603615

604616
if let Some(remote_encryption) = &self.remote_encryption {
605-
req = req.header(
606-
"x-turso-encryption-key",
607-
remote_encryption.key_32_bytes_base64_encoded.as_str(),
608-
);
617+
req = req.header("x-turso-encryption-key", remote_encryption.key.as_string());
609618
}
610619

611620
let req = req.body(Body::empty()).expect("valid request");
@@ -705,10 +714,7 @@ impl SyncContext {
705714
}
706715

707716
if let Some(remote_encryption) = &self.remote_encryption {
708-
req = req.header(
709-
"x-turso-encryption-key",
710-
remote_encryption.key_32_bytes_base64_encoded.as_str(),
711-
);
717+
req = req.header("x-turso-encryption-key", remote_encryption.key.as_string());
712718
}
713719

714720
let req = req.body(Body::empty()).expect("valid request");

0 commit comments

Comments
 (0)