@@ -8,6 +8,8 @@ pub use builder::Builder;
88pub use libsql_sys:: { Cipher , EncryptionConfig } ;
99
1010use crate :: { Connection , Result } ;
11+ #[ cfg( any( feature = "remote" , feature = "sync" ) ) ]
12+ use base64:: { engine:: general_purpose, Engine } ;
1113use std:: fmt;
1214use std:: sync:: atomic:: AtomicU64 ;
1315
@@ -100,6 +102,7 @@ enum DbType {
100102 auth_token : String ,
101103 connector : crate :: util:: ConnectorService ,
102104 _bg_abort : Option < std:: sync:: Arc < crate :: sync:: DropAbort > > ,
105+ remote_encryption : Option < EncryptionContext > ,
103106 } ,
104107 #[ cfg( feature = "remote" ) ]
105108 Remote {
@@ -108,6 +111,7 @@ enum DbType {
108111 connector : crate :: util:: ConnectorService ,
109112 version : Option < String > ,
110113 namespace : Option < String > ,
114+ remote_encryption : Option < EncryptionContext > ,
111115 } ,
112116}
113117
@@ -214,7 +218,7 @@ cfg_replication! {
214218 endpoint,
215219 auth_token,
216220 https,
217- encryption_config
221+ encryption_config,
218222 ) . await
219223 }
220224
@@ -524,7 +528,7 @@ cfg_remote! {
524528 url: impl Into <String >,
525529 auth_token: impl Into <String >,
526530 connector: C ,
527- version: Option <String >
531+ version: Option <String >,
528532 ) -> Result <Self >
529533 where
530534 C : tower:: Service <http:: Uri > + Send + Clone + Sync + ' static ,
@@ -544,6 +548,7 @@ cfg_remote! {
544548 connector: crate :: util:: ConnectorService :: new( svc) ,
545549 version,
546550 namespace: None ,
551+ remote_encryption: None
547552 } ,
548553 max_write_replication_index: Default :: default ( ) ,
549554 } )
@@ -677,6 +682,7 @@ impl Database {
677682 url,
678683 auth_token,
679684 connector,
685+ remote_encryption,
680686 ..
681687 } => {
682688 use crate :: {
@@ -708,6 +714,7 @@ impl Database {
708714 connector. clone ( ) ,
709715 None ,
710716 None ,
717+ remote_encryption. clone ( ) ,
711718 ) ,
712719 read_your_writes : * read_your_writes,
713720 context : db. sync_ctx . clone ( ) . unwrap ( ) ,
@@ -730,6 +737,7 @@ impl Database {
730737 connector,
731738 version,
732739 namespace,
740+ remote_encryption,
733741 } => {
734742 let conn = std:: sync:: Arc :: new (
735743 crate :: hrana:: connection:: HttpConnection :: new_with_connector (
@@ -738,6 +746,7 @@ impl Database {
738746 connector. clone ( ) ,
739747 version. as_ref ( ) . map ( |s| s. as_str ( ) ) ,
740748 namespace. as_ref ( ) . map ( |s| s. as_str ( ) ) ,
749+ remote_encryption. clone ( ) ,
741750 ) ,
742751 ) ;
743752
@@ -781,3 +790,29 @@ impl std::fmt::Debug for Database {
781790 f. debug_struct ( "Database" ) . finish ( )
782791 }
783792}
793+
794+ #[ cfg( any( feature = "remote" , feature = "sync" ) ) ]
795+ #[ derive( Debug , Clone ) ]
796+ pub enum EncryptionKey {
797+ /// The key is a base64-encoded string.
798+ Base64Encoded ( String ) ,
799+ /// The key is a byte array.
800+ Bytes ( Vec < u8 > ) ,
801+ }
802+
803+ #[ cfg( any( feature = "remote" , feature = "sync" ) ) ]
804+ impl EncryptionKey {
805+ pub fn as_string ( & self ) -> String {
806+ match self {
807+ EncryptionKey :: Base64Encoded ( s) => s. clone ( ) ,
808+ EncryptionKey :: Bytes ( b) => general_purpose:: STANDARD . encode ( b) ,
809+ }
810+ }
811+ }
812+
813+ #[ cfg( any( feature = "remote" , feature = "sync" ) ) ]
814+ #[ derive( Debug , Clone ) ]
815+ pub struct EncryptionContext {
816+ /// The base64-encoded key for the encryption, sent on every request.
817+ pub key : EncryptionKey ,
818+ }
0 commit comments