Struct Message

Source
pub struct Message { /* private fields */ }
Expand description

A message.

An OpenPGP message is a structured sequence of OpenPGP packets. Basically, it’s an optionally encrypted, optionally signed literal data packet. The exact structure is defined in Section 10.3 of RFC 9580.

ASCII Armored Messages are wrapped in -----BEGIN PGP MESSAGE----- header and -----END PGP MESSAGE----- tail lines:

-----BEGIN PGP MESSAGE-----

xA0DAAoW5saJekzviSQByxBiAAAAAADYtdiv2KfZgtipwnUEABYKACcFglwJHYoW
IQRnpIdTo4Cms7fffcXmxol6TO+JJAkQ5saJekzviSQAAIJ6APwK6FxtHXn8txDl
tBFsIXlOSLOs4BvArlZzZSMomIyFLAEAwCLJUChMICDxWXRlHxORqU5x6hlO3DdW
sl/1DAbnRgI=
=AqoO
-----END PGP MESSAGE-----

§Examples

Creating a Message encrypted with a password.

use std::io::Write;
use openpgp::serialize::stream::{Message, Encryptor, LiteralWriter};

let mut sink = vec![];
let message = Encryptor::with_passwords(
    Message::new(&mut sink), Some("ściśle tajne")).build()?;
let mut w = LiteralWriter::new(message).build()?;
w.write_all(b"Hello world.")?;
w.finalize()?;

Implementations§

Source§

impl Message

Source

pub fn body(&self) -> Option<&Literal>

Returns the body of the message.

Returns None if no literal data packet is found. This happens if a SEIP container has not been decrypted.

§Examples
use std::io;
use std::io::Read;
use openpgp::Message;
use openpgp::armor::{Reader, ReaderMode};
use openpgp::parse::Parse;

let data = "yxJiAAAAAABIZWxsbyB3b3JsZCE="; // base64 over literal data packet

let mut cursor = io::Cursor::new(&data);
let mut reader = Reader::from_reader(&mut cursor, ReaderMode::VeryTolerant);

let mut buf = Vec::new();
reader.read_to_end(&mut buf)?;

let message = Message::from_bytes(&buf)?;
assert_eq!(message.body().unwrap().body(), b"Hello world!");
Source

pub fn packets(&self) -> &PacketPile

Returns a reference to the message’s packets.

Trait Implementations§

Source§

impl Debug for Message

Source§

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

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

impl From<Message> for PacketPile

Source§

fn from(m: Message) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Message

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Marshal for Message

Source§

fn serialize(&self, o: &mut dyn Write) -> Result<()>

Writes a serialized version of the specified Message to o.

Source§

fn export(&self, o: &mut dyn Write) -> Result<()>

Exports a serialized version of the object to o. Read more
Source§

impl MarshalInto for Message

Source§

fn serialized_len(&self) -> usize

Computes the maximal length of the serialized representation. Read more
Source§

fn serialize_into(&self, buf: &mut [u8]) -> Result<usize>

Serializes into the given buffer. Read more
Source§

fn export_into(&self, buf: &mut [u8]) -> Result<usize>

Exports into the given buffer. Read more
Source§

fn to_vec(&self) -> Result<Vec<u8>>

Serializes the packet to a vector.
Source§

fn export_to_vec(&self) -> Result<Vec<u8>>

Exports to a vector. Read more
Source§

impl<'a> Parse<'a, Message> for Message

Source§

fn from_buffered_reader<R>(reader: R) -> Result<Message>
where R: BufferedReader<Cookie> + 'a,

Reads a Message from the specified reader.

See Message::try_from for more details.

Source§

fn from_reader<R: 'a + Read + Send + Sync>(reader: R) -> Result<T>

Reads from the given reader. Read more
Source§

fn from_file<P: AsRef<Path>>(path: P) -> Result<T>

Reads from the given file. Read more
Source§

fn from_bytes<D: AsRef<[u8]> + ?Sized + Send + Sync>(data: &'a D) -> Result<T>

Reads from the given slice. Read more
Source§

impl PartialEq for Message

Source§

fn eq(&self, other: &Message) -> 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 Message

Source§

fn serialize(&self, o: &mut dyn Write) -> Result<()>

Writes a serialized version of the object to o.
Source§

fn export(&self, o: &mut dyn Write) -> Result<()>

Exports a serialized version of the object to o. Read more
Source§

impl SerializeInto for Message

Source§

fn serialized_len(&self) -> usize

Computes the maximal length of the serialized representation. Read more
Source§

fn serialize_into(&self, buf: &mut [u8]) -> Result<usize>

Serializes into the given buffer. Read more
Source§

fn to_vec(&self) -> Result<Vec<u8>>

Serializes the packet to a vector.
Source§

fn export_into(&self, buf: &mut [u8]) -> Result<usize>

Exports into the given buffer. Read more
Source§

fn export_to_vec(&self) -> Result<Vec<u8>>

Exports to a vector. Read more
Source§

impl TryFrom<PacketPile> for Message

Source§

fn try_from(pile: PacketPile) -> Result<Self>

Converts the PacketPile to a Message.

Converting a PacketPile to a Message doesn’t change the packets; it asserts that the packet sequence is an optionally encrypted, optionally signed, optionally compressed literal data packet. The exact grammar is defined in Section 10.3 of RFC 9580.

Caveats: this function assumes that any still encrypted parts or still compressed parts are valid messages.

Source§

type Error = Error

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

impl TryFrom<Vec<Packet>> for Message

Source§

fn try_from(packets: Vec<Packet>) -> Result<Self>

Converts the vector of Packets to a Message.

See Message::try_from for more details.

Source§

type Error = Error

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

impl StructuralPartialEq for Message

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> 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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T