Skip to main content

KeyBuilder

Struct KeyBuilder 

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

Builder over rnp_op_generate_* for primary keys.

Terminal method KeyBuilder::build consumes self, executes the generation, and returns the resulting Key.

let key = KeyBuilder::new(Algorithm::Rsa)
    .bits(2048)
    .userid("alice <alice@example.com>")
    .hash(Hash::Sha256)
    .add_usage(KeyUsage::Sign)
    .add_usage(KeyUsage::Certify)
    .build(&ctx)
    .unwrap();

Implementations§

Source§

impl KeyBuilder

Source

pub fn new(alg: Algorithm) -> Self

Source

pub fn bits(self, n: u32) -> Self

Source

pub fn hash(self, h: Hash) -> Self

Source

pub fn dsa_qbits(self, n: u32) -> Self

Source

pub fn curve(self, c: Curve) -> Self

Source

pub fn userid(self, uid: impl Into<String>) -> Self

Source

pub fn expiration(self, seconds: u32) -> Self

Source

pub fn add_usage(self, u: KeyUsage) -> Self

Source

pub fn add_pref_hash(self, h: Hash) -> Self

Source

pub fn add_pref_cipher(self, c: Cipher) -> Self

Source

pub fn add_pref_compression(self, c: Compression) -> Self

Source

pub fn clear_usage(self) -> Self

Clear all key usage flags. Equivalent to rnp_op_generate_clear_usage: the builder defers every setter to build time, so clearing the local vector is exactly the C-side clear.

Source

pub fn clear_pref_hash(self) -> Self

Clear all preferred hashes. Equivalent to rnp_op_generate_clear_pref_hash (see Self::clear_usage for why the local vector is cleared instead of the C-side state).

Source

pub fn clear_pref_cipher(self) -> Self

Clear all preferred ciphers. Equivalent to rnp_op_generate_clear_pref_cipher (see Self::clear_usage).

Source

pub fn clear_pref_compression(self) -> Self

Clear all preferred compressions. Equivalent to rnp_op_generate_clear_pref_compression (see Self::clear_usage).

Source

pub fn pref_keyserver(self, s: impl Into<String>) -> Self

Source

pub fn protection(self, opts: &ProtectOptions) -> Self

Apply protection (encryption of secret material) at generation time. Takes the same ProtectOptions type used by Key::protect — single canonical config.

Source

pub fn request_password(self) -> Self

Have librnp ask the configured password provider for the protection password at execution time (instead of supplying it inline via Self::protection).

Source

pub fn v6(self) -> Self

Generate a v6 (RFC 9580) primary key instead of v4. Requires the crypto-refresh Cargo feature and a librnp built with ENABLE_CRYPTO_REFRESH=ON.

Source

pub fn add_subkey(self, sub: SubkeyBuilder) -> Self

Add a subkey to be generated alongside the primary in KeyBuilder::build.

The subkey is created after the primary key, with a binding signature. This is the idiomatic way to build a composite keypair (primary + signing subkey + encryption subkey) in one call.

KeyBuilder::new(Algorithm::Rsa)
    .bits(2048)
    .userid("alice")
    .add_subkey(SubkeyBuilder::new(Algorithm::Rsa)
        .bits(2048)
        .add_usage(KeyUsage::EncryptComms))
    .add_subkey(SubkeyBuilder::new(Algorithm::Eddsa)
        .add_usage(KeyUsage::Sign));
Source

pub fn add_pqc_encryption_subkey(self, alg: PqcAlgorithm) -> Self

Add a PQC encryption subkey (e.g. ML-KEM-768+X25519). Requires the pqc Cargo feature.

Source

pub fn add_pqc_signing_subkey(self, alg: PqcAlgorithm) -> Self

Add a PQC signing subkey (e.g. ML-DSA-65+ED25519). Requires the pqc Cargo feature.

Source

pub fn build<'ctx>(self, ctx: &'ctx Context) -> Result<Key<'ctx>>

Execute the generation. The returned Key borrows ctx for its lifetime (matching the rest of the crate’s lifetime discipline).

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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.