Struct sequoia_openpgp::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 11.3 of RFC 4880.

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!");

Methods from Deref<Target = PacketPile>§

source

pub fn pretty_print(&self)

Pretty prints the message to stderr.

This function is primarily intended for debugging purposes.

source

pub fn path_ref(&self, pathspec: &[usize]) -> Option<&Packet>

Returns a reference to the packet at the location described by pathspec.

pathspec is a slice of the form [0, 1, 2]. Each element is the index of packet in a container. Thus, the previous path specification means: return the third child of the second child of the first top-level packet. In other words, the starred packet in the following tree:

        PacketPile
       /     |     \
      0      1      2  ...
    /   \
   /     \
 0         1  ...
       /   |   \  ...
      0    1    2
                *

And, [10] means return the 11th top-level packet.

Note: there is no packet at the root. Thus, the path [] returns None.

§Examples
let pile = PacketPile::from(packets);

if let Some(packet) = pile.path_ref(&[0]) {
    // There is a packet at this path.
}

if let None = pile.path_ref(&[0, 1, 2]) {
    // But none here.
}
source

pub fn descendants(&self) -> Iter<'_>

Returns an iterator over all of the packet’s descendants, in depth-first order.

let mut lit = Literal::new(DataFormat::Text);
lit.set_body(b"test".to_vec());

let pile = PacketPile::from(vec![lit.into()]);

for packet in pile.descendants() {
    assert_eq!(packet.tag(), Tag::Literal);
}
source

pub fn children( &self ) -> impl Iterator<Item = &Packet> + ExactSizeIterator + Send + Sync

Returns an iterator over the top-level packets.

let mut lit = Literal::new(DataFormat::Text);
lit.set_body(b"test".to_vec());

let pile = PacketPile::from(vec![lit.into()]);

assert_eq!(pile.children().len(), 1);

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

§

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<Self>

Reads a Message from the specified reader.

See Message::try_from for more details.

source§

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

Reads a Message from the specified file.

See Message::try_from for more details.

source§

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

Reads a Message from buf.

See Message::try_from for more details.

source§

impl PartialEq for Message

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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 11.3 of RFC 4880.

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

§

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.

§

type Error = Error

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

impl Deref for Message

§

type Target = PacketPile

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
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

§

type Output = T

Should always be Self
source§

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

§

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

§

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.