pub struct Envelope {
pub ephemeral_key: Vec<u8>,
pub encrypted_content: Vec<u8>,
pub encrypted_headers: Vec<u8>,
}Expand description
An encrypted message envelope for transport.
Contains an ephemeral X25519 public key and XChaCha20-Poly1305 encrypted
headers and content. Created by Message::enclose_for and opened by
Envelope::open or Envelope::open_with_replay_guard.
§Examples
use ma_did::{generate_identity, Message, Envelope, EncryptionKey, SigningKey, Did};
let alice = generate_identity("k51qzi5uqu5dj9807pbuod1pplf0vxh8m4lfy3ewl9qbm2s8dsf9ugdf9gedhr").unwrap();
let bob = generate_identity("k51qzi5uqu5dl96qbq93mwl5drvk2z83fk4s6h4n7xgqnwrxlscs11i1bja7uk").unwrap();
let alice_sign_url = Did::new_url(&alice.subject_url.ipns, None::<String>).unwrap();
let alice_key = SigningKey::from_private_key_bytes(
alice_sign_url,
hex::decode(&alice.signing_private_key_hex).unwrap().try_into().unwrap(),
).unwrap();
let msg = Message::new(
alice.document.id.clone(),
bob.document.id.clone(),
"text/plain",
b"secret".to_vec(),
&alice_key,
).unwrap();
// Encrypt for Bob
let envelope = msg.enclose_for(&bob.document).unwrap();
// Bob decrypts
let bob_enc_url = Did::new_url(&bob.subject_url.ipns, None::<String>).unwrap();
let bob_enc_key = EncryptionKey::from_private_key_bytes(
bob_enc_url,
hex::decode(&bob.encryption_private_key_hex).unwrap().try_into().unwrap(),
).unwrap();
let decrypted = envelope.open(&bob.document, &bob_enc_key, &alice.document).unwrap();
assert_eq!(decrypted.content, b"secret");Fields§
§ephemeral_key: Vec<u8>§encrypted_content: Vec<u8>§encrypted_headers: Vec<u8>Implementations§
Source§impl Envelope
impl Envelope
pub fn verify(&self) -> Result<()>
pub fn to_cbor(&self) -> Result<Vec<u8>>
pub fn from_cbor(bytes: &[u8]) -> Result<Self>
pub fn open( &self, recipient_document: &Document, recipient_key: &EncryptionKey, sender_document: &Document, ) -> Result<Message>
pub fn open_with_replay_guard( &self, recipient_document: &Document, recipient_key: &EncryptionKey, sender_document: &Document, replay_guard: &mut ReplayGuard, ) -> Result<Message>
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Envelope
impl<'de> Deserialize<'de> for Envelope
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl Eq for Envelope
impl StructuralPartialEq for Envelope
Auto Trait Implementations§
impl Freeze for Envelope
impl RefUnwindSafe for Envelope
impl Send for Envelope
impl Sync for Envelope
impl Unpin for Envelope
impl UnsafeUnpin for Envelope
impl UnwindSafe for Envelope
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more