Opener

Struct Opener 

Source
pub struct Opener {}
Expand description

Opener for bottles.

The Opener provides methods to decrypt and inspect bottles. It can decrypt bottles with multiple encryption layers, working from the outermost layer inward.

§Example

use rust_bottle::*;
use rand::rngs::OsRng;

let mut bottle = Bottle::new(b"Secret".to_vec());
let rng = &mut OsRng;
let key = X25519Key::generate(rng);
bottle.encrypt(rng, &key.public_key_bytes()).unwrap();

let opener = Opener::new();
let decrypted = opener.open(&bottle, Some(&key.private_key_bytes())).unwrap();

Implementations§

Source§

impl Opener

Source

pub fn new() -> Self

Create a new opener.

§Returns

A new Opener instance

Source

pub fn open( &self, bottle: &Bottle, private_key: Option<&[u8]>, ) -> Result<Vec<u8>>

Open a bottle, decrypting if needed.

This method decrypts all encryption layers sequentially, starting from the outermost layer and working inward. Each layer requires the appropriate private key.

§Arguments
  • bottle - The bottle to open
  • private_key - Optional private key for decryption. Required if the bottle is encrypted.
§Returns
  • Ok(Vec<u8>) - The decrypted message
  • Err(BottleError::NoAppropriateKey) - If encryption exists but no key provided
  • Err(BottleError::Decryption) - If decryption fails
  • Err(BottleError::InvalidKeyType) - If the key format is invalid
§Note

For layered encryption, the same key is used for all layers in the current implementation. Future versions may support different keys per layer.

§Example
use rust_bottle::*;
use rand::rngs::OsRng;

let message = b"Hello, world!";
let mut bottle = Bottle::new(message.to_vec());

let rng = &mut OsRng;
let key = X25519Key::generate(rng);
bottle.encrypt(rng, &key.public_key_bytes()).unwrap();

let opener = Opener::new();
let decrypted = opener.open(&bottle, Some(&key.private_key_bytes())).unwrap();
assert_eq!(decrypted, message);
Source

pub fn open_info(&self, bottle: &Bottle) -> Result<BottleInfo>

Get information about a bottle without decrypting it.

This method provides metadata about encryption and signature status without requiring decryption keys. Useful for inspecting bottles before attempting to decrypt them.

§Arguments
  • bottle - The bottle to inspect
§Returns
  • Ok(BottleInfo) - Information about the bottle
§Example
use rust_bottle::*;
use rand::rngs::OsRng;

let mut bottle = Bottle::new(b"Message".to_vec());
let rng = &mut OsRng;
let key = Ed25519Key::generate(rng);
let pub_key = key.public_key_bytes();
bottle.sign(rng, &key, &pub_key).unwrap();

let opener = Opener::new();
let info = opener.open_info(&bottle).unwrap();
assert!(info.is_signed);
assert!(info.is_signed_by(&pub_key));

Trait Implementations§

Source§

impl Default for Opener

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Opener

§

impl RefUnwindSafe for Opener

§

impl Send for Opener

§

impl Sync for Opener

§

impl Unpin for Opener

§

impl UnwindSafe for Opener

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V