Skip to main content

EncryptedData

Struct EncryptedData 

Source
pub struct EncryptedData(/* private fields */);
Expand description

A newtype over Vec<u8> representing binary (non‑armored) age encrypted data.

§Overview

This is the raw, binary output of age encryption when no armor is requested. It is the complement of [ArmoredData]. The inner bytes are the direct result of the age::Encryptor writer — a compact, but not human‑readable, ciphertext.

§Why wrap a Vec<u8>?

Exactly the same reasons as ArmoredData:

  • Type distinctness – prevents mixing up plaintext, encrypted data, and other byte buffers at the type level.
  • Controlled constructionnew is pub(crate), so only the encryption functions can create an EncryptedData. This guarantees that any EncryptedData you hold came from a successful encryption operation.
  • Ergonomics – implements [AsRef<[u8]>], From conversions, and provides accessor methods. The [Display] implementation shows only the byte length to avoid dumping binary data to the screen.

§Examples

use age_crypto::encrypt;

let encrypted: EncryptedData = encrypt(b"secret", &["age1..."]).unwrap();
println!("{}", encrypted);          // [EncryptedData: 512 bytes]
let raw: &[u8] = encrypted.as_bytes();
let owned: Vec<u8> = encrypted.to_vec();

Implementations§

Source§

impl EncryptedData

Source

pub fn as_bytes(&self) -> &[u8]

Returns the binary ciphertext as a byte slice.

This provides read‑only access to the encrypted bytes. Use it when you need to write the data to a file or network stream.

§Example
std::fs::write("data.age", encrypted.as_bytes())?;
Source

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

Converts the ciphertext into an owned Vec<u8>.

This clones the internal buffer. If you need to take ownership without cloning, use [From<EncryptedData> for Vec<u8>] instead, which consumes the EncryptedData.

Source

pub fn len(&self) -> usize

Returns the number of bytes in the ciphertext.

Source

pub fn is_empty(&self) -> bool

Returns true if the ciphertext is empty.

Note: a valid age encrypted stream is never truly empty because it always contains at least a header. However, this method is provided for consistency.

Trait Implementations§

Source§

impl AsRef<[u8]> for EncryptedData

Allows &EncryptedData to be used wherever &[u8] is expected.

This means you can pass an EncryptedData directly to functions that take a byte slice, such as write_all or copy_to.

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for EncryptedData

Source§

fn clone(&self) -> EncryptedData

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EncryptedData

Source§

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

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

impl Display for EncryptedData

Displays a description showing the byte length only.

Printing raw binary data is seldom useful and could clutter output or accidentally expose ciphertext. The Display implementation limits itself to a short, human‑readable summary.

Source§

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

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

impl From<EncryptedData> for Vec<u8>

Extracts the raw byte vector from the wrapper.

This consumes the EncryptedData, giving you full ownership of the underlying Vec<u8> without cloning.

Source§

fn from(data: EncryptedData) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for EncryptedData

Converts an owned Vec<u8> into an EncryptedData.

Caution: No validation is performed. The caller must guarantee that the bytes constitute a valid age binary ciphertext. In normal use, only the encryption functions should use this conversion.

Source§

fn from(data: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for EncryptedData

Source§

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

Source§

impl StructuralPartialEq for EncryptedData

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> AnyEq for T
where T: Any + PartialEq,

Source§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

impl<T> Base32Len for T
where T: AsRef<[u8]>,

Source§

fn base32_len(&self) -> usize

Calculate the base32 serialized length
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> CheckBase32<Vec<u5>> for T
where T: AsRef<[u8]>,

Source§

type Err = Error

Error type if conversion fails
Source§

fn check_base32(self) -> Result<Vec<u5>, <T as CheckBase32<Vec<u5>>>::Err>

Check if all values are in range and return array-like struct of u5 values
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToBase32 for T
where T: AsRef<[u8]>,

Source§

fn write_base32<W>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err>
where W: WriteBase32,

Encode as base32 and write it to the supplied writer Implementations shouldn’t allocate.
Source§

fn to_base32(&self) -> Vec<u5>

Convert Self to base32 vector
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V