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 goodDecrypting 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 signedImplementations§
Source§impl<'a> MessageParser<'a>
impl<'a> MessageParser<'a>
Sourcepub fn decrypt(self, issuer: Arc<Cert>) -> Result<Self>
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
CryptoError::NotARecipientif the private certificate holder is not a recipient,CryptoError::NotTypeif the message cannot be asymmetrically decrypted,CryptoError::UnauthenticCipherif the message’s AAD cannot be verified,CryptoError::InsufficientMemory,CryptoError::SerializationError.
Sourcepub fn decrypt_with(self, passphrase: impl AsRef<[u8]>) -> Result<Self>
pub fn decrypt_with(self, passphrase: impl AsRef<[u8]>) -> Result<Self>
Decrypts a symmetrically encrypted message using a passphrase.
§Errors
CryptoError::NotTypeif the message cannot be symmetrically decrypted,CryptoError::UnauthenticCipherif the message’s AAD cannot be verified,CryptoError::InsufficientMemory,CryptoError::SerializationError.
Sourcepub fn verify_signature(self, issuer: Arc<Cert>) -> Result<Self>
pub fn verify_signature(self, issuer: Arc<Cert>) -> Result<Self>
Verifies the signed message using issuer’s public key.
§Errors
CryptoError::NotTypeif the message was not signed,CryptoError::NotDecryptedif the message is signed, but is not decrypted.
Sourcepub fn finalize(self) -> (Message, Vec<u8>, Option<bool>)
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 messageis the message which was moved into the parser, its ownership is now moved back,decrypted contentthe plaintext content of the message. If the message was not encrypted, it will be the plaintext content which was signed.Option<signature>:Some(signature)-signatureis 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> 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