Keychain

Struct Keychain 

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

A keychain provides secure storage for private keys.

Keychains store private keys indexed by their public key fingerprints, allowing easy lookup and signing operations. Keys must implement the SignerKey trait, which includes both Sign and key identification methods.

§Example

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

let mut keychain = Keychain::new();
let rng = &mut OsRng;

let key1 = Ed25519Key::generate(rng);
let key2 = EcdsaP256Key::generate(rng);

keychain.add_key(key1);
keychain.add_key(key2);

let pub_key = keychain.signers().next().unwrap().public_key();
let signature = keychain.sign(rng, &pub_key, b"Message").unwrap();

Implementations§

Source§

impl Keychain

Source

pub fn new() -> Self

Create a new empty keychain.

§Returns

A new Keychain instance with no keys

§Example
use rust_bottle::Keychain;

let keychain = Keychain::new();
Source

pub fn add_key<K: SignerKey + 'static>(&mut self, key: K)

Add a key to the keychain.

The key is indexed by its public key fingerprint. If a key with the same fingerprint already exists, it will be replaced.

§Arguments
  • key - A key implementing SignerKey (e.g., Ed25519Key, EcdsaP256Key)
§Example
use rust_bottle::*;
use rand::rngs::OsRng;

let mut keychain = Keychain::new();
let rng = &mut OsRng;
let key = Ed25519Key::generate(rng);

keychain.add_key(key);
Source

pub fn add_keys<K: SignerKey + 'static>(&mut self, keys: Vec<K>)

Add multiple keys to the keychain at once.

§Arguments
  • keys - A vector of keys to add
§Example
use rust_bottle::*;
use rand::rngs::OsRng;

let mut keychain = Keychain::new();
let rng = &mut OsRng;
let key1 = Ed25519Key::generate(rng);
let key2 = Ed25519Key::generate(rng);

keychain.add_keys(vec![key1, key2]);
Source

pub fn get_key(&self, public_key: &[u8]) -> Result<&dyn SignerKey>

Get a key by its public key.

The public key is hashed to find the corresponding private key in the keychain.

§Arguments
  • public_key - The public key to look up
§Returns
  • Ok(&dyn SignerKey) - Reference to the key
  • Err(BottleError::KeyNotFound) - If the key is not in the keychain
§Example
use rust_bottle::*;
use rand::rngs::OsRng;

let mut keychain = Keychain::new();
let rng = &mut OsRng;
let key = Ed25519Key::generate(rng);
let pub_key = key.public_key_bytes();

keychain.add_key(key);
let retrieved = keychain.get_key(&pub_key).unwrap();
Source

pub fn get_signer(&self, public_key: &[u8]) -> Result<&dyn SignerKey>

Get a signer by its public key (alias for get_key).

§Arguments
  • public_key - The public key to look up
§Returns
  • Ok(&dyn SignerKey) - Reference to the signer
  • Err(BottleError::KeyNotFound) - If the key is not in the keychain
Source

pub fn sign<R: RngCore>( &self, rng: &mut R, public_key: &[u8], message: &[u8], ) -> Result<Vec<u8>>

Sign a message with a specific key from the keychain.

This is a convenience method that looks up the key and signs the message in one operation.

§Arguments
  • rng - A random number generator
  • public_key - The public key of the key to use for signing
  • message - The message to sign
§Returns
  • Ok(Vec<u8>) - Signature bytes
  • Err(BottleError::KeyNotFound) - If the key is not in the keychain
  • Err(BottleError::VerifyFailed) - If signing fails
§Example
use rust_bottle::*;
use rand::rngs::OsRng;

let mut keychain = Keychain::new();
let rng = &mut OsRng;
let key = Ed25519Key::generate(rng);
let pub_key = key.public_key_bytes();

keychain.add_key(key);
let signature = keychain.sign(rng, &pub_key, b"Message").unwrap();
Source

pub fn signers(&self) -> impl Iterator<Item = &dyn SignerKey>

Iterate over all signers in the keychain.

§Returns

An iterator over all stored keys (as &dyn SignerKey)

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

let mut keychain = Keychain::new();
let rng = &mut OsRng;
let key1 = Ed25519Key::generate(rng);
let key2 = EcdsaP256Key::generate(rng);

keychain.add_key(key1);
keychain.add_key(key2);

for signer in keychain.signers() {
    let pub_key = signer.public_key();
    println!("Key: {:?}", pub_key);
}

Trait Implementations§

Source§

impl Default for Keychain

Source§

fn default() -> Self

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

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

Source§

fn vzip(self) -> V