Skip to main content

Vault

Struct Vault 

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

A vault that stores its master key in the OS keychain and delegates encryption/decryption to encryptman.

Each Vault instance is bound to a service name (and optionally a target username) that identifies the keychain entry. The same service name is also used as the HKDF context in encryptman, so different service names produce different encryption keys from the same underlying keychain entry.

§Examples

use encryptman_keyring::Vault;

let vault = Vault::new("my-app").unwrap();
let ct = vault.encrypt("secret").unwrap();
let pt = vault.decrypt(&ct).unwrap();
assert_eq!(pt, "secret");
Vault::delete("my-app").unwrap();

Implementations§

Source§

impl Vault

Source

pub fn new(service: &str) -> Result<Self, Error>

Create or open a vault with the given service name.

On first call, a new random master key is generated and stored in the OS keychain. On subsequent calls, the existing key is loaded.

The service is used as the keyring service name and as the encryptman HKDF context ("encryptman:{service}").

§Errors

Returns Error::Keychain if the OS keychain is unavailable.

Source

pub fn new_with_target(service: &str, target: &str) -> Result<Self, Error>

Create or open a vault with a custom target (username) in the keyring.

This is useful when multiple independent vaults are needed within the same service namespace.

§Errors

Returns Error::Keychain if the OS keychain is unavailable.

Source

pub fn migrate_from_file(service: &str, key_path: &Path) -> Result<Self, Error>

Migrate a file-based key into the OS keychain.

Reads a 32-byte key from key_path, stores it in the keychain under the given service name, and deletes the file on success.

Returns the vault ready for use.

§Errors
Source

pub fn migrate_from_file_with_target( service: &str, target: &str, key_path: &Path, ) -> Result<Self, Error>

Migrate a file-based key with a custom target (username).

See Vault::migrate_from_file for details.

Source

pub fn encrypt(&self, plaintext: &str) -> Result<String, Error>

Encrypt a plaintext string using the vault’s master key.

Delegates to encryptman::encrypt. Each call produces a unique ciphertext (random nonce).

§Errors

Returns Error::Crypto if encryption fails.

Source

pub fn decrypt(&self, ciphertext: &str) -> Result<String, Error>

Decrypt a ciphertext string using the vault’s master key.

Delegates to encryptman::decrypt.

§Errors

Returns Error::Crypto if decryption fails (wrong key, corrupted data, or invalid base64).

Source

pub fn encrypt_with_context( &self, context: &str, plaintext: &str, ) -> Result<String, Error>

Encrypt with a custom HKDF context.

The context is appended to "encryptman:" to derive a domain-specific AES key from the master key.

§Errors

Returns Error::Crypto if encryption fails.

Source

pub fn decrypt_with_context( &self, context: &str, ciphertext: &str, ) -> Result<String, Error>

Decrypt with a custom HKDF context.

The context must match the one used during encryption.

§Errors

Returns Error::Crypto if decryption fails.

Source

pub fn master_key(&self) -> &MasterKey

Return a reference to the underlying master key.

This is useful when you need direct access to the key for advanced use cases (e.g., custom encryption contexts or binary data).

Source

pub fn encrypt_bytes(&self, plaintext: &[u8]) -> Result<Vec<u8>, Error>

Encrypt arbitrary bytes using the vault’s master key.

Delegates to encryptman::encrypt_bytes_with_context using the vault’s service name as context.

§Errors

Returns Error::Crypto if encryption fails.

Source

pub fn decrypt_bytes(&self, packed: &[u8]) -> Result<Vec<u8>, Error>

Decrypt arbitrary bytes using the vault’s master key.

Delegates to encryptman::decrypt_bytes_with_context using the vault’s service name as context.

§Errors

Returns Error::Crypto if decryption fails.

Source

pub fn encrypt_bytes_with_context( &self, context: &str, plaintext: &[u8], ) -> Result<Vec<u8>, Error>

Encrypt arbitrary bytes with a custom HKDF context.

§Errors

Returns Error::Crypto if encryption fails.

Source

pub fn decrypt_bytes_with_context( &self, context: &str, packed: &[u8], ) -> Result<Vec<u8>, Error>

Decrypt arbitrary bytes with a custom HKDF context.

§Errors

Returns Error::Crypto if decryption fails.

Source

pub fn delete(service: &str) -> Result<(), Error>

Delete the master key from the OS keychain.

This is an associated function because deletion only requires the service name — no vault instance (or master key) is needed.

Warning: This is destructive. All encrypted data will become unrecoverable unless you have a backup of the key.

§Errors

Returns Error::Keychain if the keychain entry cannot be deleted.

Source

pub fn delete_with_target(service: &str, target: &str) -> Result<(), Error>

Delete the master key with a custom target from the OS keychain.

This is an associated function — no vault instance needed.

See Vault::delete for details.

Trait Implementations§

Source§

impl Debug for Vault

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Vault

§

impl RefUnwindSafe for Vault

§

impl Send for Vault

§

impl Sync for Vault

§

impl Unpin for Vault

§

impl UnsafeUnpin for Vault

§

impl UnwindSafe for Vault

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more