Skip to main content

Envelope

Struct Envelope 

Source
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

Source

pub fn verify(&self) -> Result<()>

Source

pub fn to_cbor(&self) -> Result<Vec<u8>>

Source

pub fn from_cbor(bytes: &[u8]) -> Result<Self>

Source

pub fn open( &self, recipient_document: &Document, recipient_key: &EncryptionKey, sender_document: &Document, ) -> Result<Message>

Source

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 Clone for Envelope

Source§

fn clone(&self) -> Envelope

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Envelope

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Envelope

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Envelope

Source§

fn eq(&self, other: &Envelope) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Envelope

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Envelope

Source§

impl StructuralPartialEq for Envelope

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,