MessageParser

Struct MessageParser 

Source
pub struct MessageParser<'a> { /* private fields */ }
Expand description

Parses a message.

This struct implements something akin to the state pattern by using enums and internal variables.

§Examples

Typical call chain used to decrypt a message.

use pyrus_crypto::prelude::*;


let secret_text = b"Hello there.";
let (message, content, signature) = message
    .parse(&agentcow)? // recieve the message as agentcow
    .decrypt(larry.clone())? // asymmetrically decrypt from larry
    .verify_signature(larry.clone())? // verify the signature from larry
    .finalize(); // finish parsing, return the results as a tuple
                 // (<original message>, <decrypted content>, Option<signature>)

assert_eq!(&secret_text[..], &content[..]);
assert!(signature.is_some()); // signature is Some
assert!(signature.unwrap()); // signature is good

Decrypting a symmetric message.

use pyrus_crypto::prelude::*;

let secret_text = b"This is a very secret message";
// another of the aforementioned shortcomings
// assume we already have a message
let (_, content, sig) = message
    .parse(&dummy)? // a dummy certificate is needed
    .decrypt_with(&b"password123"[..])?
    .finalize();

assert_eq!(&secret_text[..], &content[..]);
assert!(sig.is_none()); // the message is not signed

Implementations§

Source§

impl<'a> MessageParser<'a>

Source

pub fn decrypt(self, issuer: Arc<Cert>) -> Result<Self>

Decrypts an asymmetrically encrypted message using issuer’s public key and a private certificate supplied to Message::parse.

§Errors
Source

pub fn decrypt_with(self, passphrase: impl AsRef<[u8]>) -> Result<Self>

Decrypts a symmetrically encrypted message using a passphrase.

§Errors
Source

pub fn verify_signature(self, issuer: Arc<Cert>) -> Result<Self>

Verifies the signed message using issuer’s public key.

§Errors
Source

pub fn finalize(self) -> (Message, Vec<u8>, Option<bool>)

Finishes the process of parsing the message. Returns a tuple of the form: (<original message>, <decrypted content>, Option<signature>).

  • original message is the message which was moved into the parser, its ownership is now moved back,
  • decrypted content the plaintext content of the message. If the message was not encrypted, it will be the plaintext content which was signed.
  • Option<signature>:
    • Some(signature) - signature is true if the signature is ok, false otherwise,
    • None - the message was not signed or signature verification was not attempted.

Auto Trait Implementations§

§

impl<'a> Freeze for MessageParser<'a>

§

impl<'a> RefUnwindSafe for MessageParser<'a>

§

impl<'a> Send for MessageParser<'a>

§

impl<'a> Sync for MessageParser<'a>

§

impl<'a> Unpin for MessageParser<'a>

§

impl<'a> UnwindSafe for MessageParser<'a>

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> 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, 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.