pub struct Message { /* private fields */ }Expand description
A struct describing a message.
Each message is composed of:
- a payload - optionally encrypted binary data,
- a header -
Headerstruct containing the message’s metadata.
A message is constructed by using the Message::new function which
returns a MessageBuilder. Then the builder configures the message using
the state pattern and after calling finalize() returns a message ready
for sending.
§Serialization
Messages implement serde::Serialize and serde::Deserialize
traits, which allow for serialization and deserialization using any
serde format. Additionally for human readable formats messages
serialize bytes as hex encoded strings to please the users’ eyes.
§Examples
Constructing a signed and encrypted message.
use pyrus_crypto::message::Message;
use pyrus_crypto::message::MessageFinal;
let secret_text = b"Let's meet up in the evening";
let msg = Message::new(&larry)? // returns MessageBuilder here
.write(&secret_text[..])? // MessageBuilder
.sign() // SignedMessage (almost the same API as the builder)
.encrypt_for(&friends)? // AsmEncryptedMessage (can only finalize)
.finalize()?; // returns MessageThe message may then be base64 encoded for transmission:
let b64msg: String = msg.b64encode()?;And then decoded back into a message:
let msg_recv = Message::b64decode(b64msg.as_bytes())?;
assert_eq!(msg, msg_recv);Implementations§
Source§impl Message
impl Message
Sourcepub fn new<'a>(issuer: &'a Cert) -> Result<MessageBuilder<'a>>
pub fn new<'a>(issuer: &'a Cert) -> Result<MessageBuilder<'a>>
Starts the construction of the message by returning a
MessageBuilder. The builder should be a temporary object
and live only for a short duration in a method call chain.
§Errors
CryptoError::NotSecretif the issuer’s certificate does not contain secret keys. (This is not your certificate, why do you try to impersonate people?)
Sourcepub fn describe(&self) -> MessageDesc
pub fn describe(&self) -> MessageDesc
Reads the header of the message, discards any associated data and
returns the type of the message. See MessageDesc for more details.
Sourcepub fn issuer(&self) -> &Fingerprint
pub fn issuer(&self) -> &Fingerprint
Convenience function. Instead of checking the header for the message issuer, this function may be use to forward it.
Sourcepub fn header(&self) -> &Header
pub fn header(&self) -> &Header
Returns the header of this message for inspection. In reality this is
only useful for automated message parsing. The MessageParser
internals access the header differently.
Sourcepub fn payload(&self) -> &[u8] ⓘ
pub fn payload(&self) -> &[u8] ⓘ
Returns the payload of this message. The payload is the optionally encrypted message content and on its own is not useful unless the goal is to implement automated message parsing.
Sourcepub fn parse<'a>(self, recipient: &'a Cert) -> Result<MessageParser<'a>>
pub fn parse<'a>(self, recipient: &'a Cert) -> Result<MessageParser<'a>>
Starts the process of message parsing. See MessageParser for more
details.
§Errors
CryptoError::NotSecretif the recipient’s (decrypting) certificate does not contain secret keys.
Sourcepub fn b64encode(&self) -> Result<String>
pub fn b64encode(&self) -> Result<String>
Encodes the message as a base64 string.
§Errors
CryptoError::SerializationErrorif a serialization error occured.
Sourcepub fn b64decode(repr: impl AsRef<[u8]>) -> Result<Self>
pub fn b64decode(repr: impl AsRef<[u8]>) -> Result<Self>
Decodes a message from a base64 representation.
§Errors
CryptoError::SerializationErrorif a serialization error occured.CryptoError::DecodingErrorif the base64 representation is invalid.