@@ -418,45 +418,8 @@ impl StoreCipher {
418418 /// # anyhow::Ok(()) };
419419 /// ```
420420 pub fn encrypt_value ( & self , value : & impl Serialize ) -> Result < Vec < u8 > , Error > {
421- Ok ( serde_json:: to_vec ( & self . encrypt_value_typed ( value) ?) ?)
422- }
423-
424- /// Encrypt a value before it is inserted into the key/value store.
425- ///
426- /// A value can be decrypted using the
427- /// [`StoreCipher::decrypt_value_typed()`] method. This is the lower
428- /// level function to `encrypt_value`, but returns the
429- /// full `EncryptdValue`-type
430- ///
431- /// # Arguments
432- ///
433- /// * `value` - A value that should be encrypted, any value that implements
434- /// `Serialize` can be given to this method. The value will be serialized
435- /// as json before it is encrypted.
436- ///
437- ///
438- /// # Examples
439- ///
440- /// ```no_run
441- /// # let example = || {
442- /// use matrix_sdk_store_encryption::StoreCipher;
443- /// use serde_json::{json, value::Value};
444- ///
445- /// let store_cipher = StoreCipher::new()?;
446- ///
447- /// let value = json!({
448- /// "some": "data",
449- /// });
450- ///
451- /// let encrypted = store_cipher.encrypt_value_typed(&value)?;
452- /// let decrypted: Value = store_cipher.decrypt_value_typed(encrypted)?;
453- ///
454- /// assert_eq!(value, decrypted);
455- /// # anyhow::Ok(()) };
456- /// ```
457- pub fn encrypt_value_typed ( & self , value : & impl Serialize ) -> Result < EncryptedValue , Error > {
458421 let data = serde_json:: to_vec ( value) ?;
459- self . encrypt_value_data ( data)
422+ Ok ( serde_json :: to_vec ( & self . encrypt_value_data ( data) ? ) ? )
460423 }
461424
462425 /// Encrypt some data before it is inserted into the key/value store.
@@ -497,47 +460,6 @@ impl StoreCipher {
497460 Ok ( EncryptedValue { version : VERSION , ciphertext, nonce } )
498461 }
499462
500- /// Encrypt a value before it is inserted into the key/value store.
501- ///
502- /// A value can be decrypted using the
503- /// [`StoreCipher::decrypt_value_typed()`] method. This is the lower
504- /// level function to `encrypt_value`, but returns the
505- /// full `EncryptdValue`-type
506- ///
507- /// # Arguments
508- ///
509- /// * `value` - A value that should be encrypted, any value that implements
510- /// `Serialize` can be given to this method. The value will be serialized
511- /// as json before it is encrypted.
512- ///
513- ///
514- /// # Examples
515- ///
516- /// ```no_run
517- /// # let example = || {
518- /// use matrix_sdk_store_encryption::StoreCipher;
519- /// use serde_json::{json, value::Value};
520- ///
521- /// let store_cipher = StoreCipher::new()?;
522- ///
523- /// let value = json!({
524- /// "some": "data",
525- /// });
526- ///
527- /// let encrypted = store_cipher.encrypt_value_typed(&value)?;
528- /// let decrypted: Value = store_cipher.decrypt_value_typed(encrypted)?;
529- ///
530- /// assert_eq!(value, decrypted);
531- /// # anyhow::Ok(()) };
532- /// ```
533- pub fn encrypt_value_base64_typed (
534- & self ,
535- value : & impl Serialize ,
536- ) -> Result < EncryptedValueBase64 , Error > {
537- let data = serde_json:: to_vec ( value) ?;
538- self . encrypt_value_base64_data ( data)
539- }
540-
541463 /// Encrypt some data before it is inserted into the key/value store,
542464 /// using base64 for arrays of integers.
543465 ///
@@ -603,89 +525,12 @@ impl StoreCipher {
603525 /// ```
604526 pub fn decrypt_value < T : DeserializeOwned > ( & self , value : & [ u8 ] ) -> Result < T , Error > {
605527 let value: EncryptedValue = serde_json:: from_slice ( value) ?;
606- self . decrypt_value_typed ( value)
607- }
608-
609- /// Decrypt a value after it was fetched from the key/value store.
610- ///
611- /// A value can be encrypted using the
612- /// [`StoreCipher::encrypt_value_typed()`] method. Lower level method to
613- /// [`StoreCipher::decrypt_value_typed()`]
614- ///
615- /// # Arguments
616- ///
617- /// * `value` - The EncryptedValue of a value that should be decrypted.
618- ///
619- /// The method will deserialize the decrypted value into the expected type.
620- ///
621- /// # Examples
622- ///
623- /// ```
624- /// # let example = || {
625- /// use matrix_sdk_store_encryption::StoreCipher;
626- /// use serde_json::{json, value::Value};
627- ///
628- /// let store_cipher = StoreCipher::new()?;
629- ///
630- /// let value = json!({
631- /// "some": "data",
632- /// });
633- ///
634- /// let encrypted = store_cipher.encrypt_value_typed(&value)?;
635- /// let decrypted: Value = store_cipher.decrypt_value_typed(encrypted)?;
636- ///
637- /// assert_eq!(value, decrypted);
638- /// # anyhow::Ok(()) };
639- /// ```
640- pub fn decrypt_value_typed < T : DeserializeOwned > (
641- & self ,
642- value : EncryptedValue ,
643- ) -> Result < T , Error > {
644528 let mut plaintext = self . decrypt_value_data ( value) ?;
645529 let ret = serde_json:: from_slice ( & plaintext) ;
646530 plaintext. zeroize ( ) ;
647531 Ok ( ret?)
648532 }
649533
650- /// Decrypt a base64-encoded value after it was fetched from the key/value
651- /// store.
652- ///
653- /// A value can be encrypted using the
654- /// [`StoreCipher::encrypt_value_base64_typed()`] method.
655- ///
656- /// # Arguments
657- ///
658- /// * `value` - The EncryptedValueBase64 of a value that should be
659- /// decrypted.
660- ///
661- /// The method will deserialize the decrypted value into the expected type.
662- ///
663- /// # Examples
664- ///
665- /// ```
666- /// # let example = || {
667- /// use matrix_sdk_store_encryption::StoreCipher;
668- /// use serde_json::{json, value::Value};
669- ///
670- /// let store_cipher = StoreCipher::new()?;
671- ///
672- /// let value = json!({
673- /// "some": "data",
674- /// });
675- ///
676- /// let encrypted = store_cipher.encrypt_value_base64_typed(&value)?;
677- /// let decrypted: Value = store_cipher.decrypt_value_base64_typed(encrypted)?;
678- ///
679- /// assert_eq!(value, decrypted);
680- /// # anyhow::Ok(()) };
681- /// ```
682- pub fn decrypt_value_base64_typed < T : DeserializeOwned > (
683- & self ,
684- value : EncryptedValueBase64 ,
685- ) -> Result < T , Error > {
686- self . decrypt_value_typed ( value. try_into ( ) ?)
687- }
688-
689534 /// Decrypt a base64-encoded value after it was fetched from the key/value
690535 /// store.
691536 ///
@@ -1100,8 +945,11 @@ mod tests {
1100945
1101946 let store_cipher = StoreCipher :: new ( ) ?;
1102947
1103- let encrypted = store_cipher. encrypt_value_base64_typed ( & event) ?;
1104- let decrypted: Value = store_cipher. decrypt_value_base64_typed ( encrypted) ?;
948+ let data = serde_json:: to_vec ( & event) ?;
949+ let encrypted = store_cipher. encrypt_value_base64_data ( data) ?;
950+
951+ let plaintext = store_cipher. decrypt_value_base64_data ( encrypted) ?;
952+ let decrypted: Value = serde_json:: from_slice ( & plaintext) ?;
1105953
1106954 assert_eq ! ( event, decrypted) ;
1107955
0 commit comments