Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/sdk/src/account/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ impl LocalAccount {
vault_id: &VaultId,
new_key: AccessKey,
) -> Result<Vec<u8>> {
use crate::passwd::ChangePassword;
use crate::vault::ChangePassword;
let paths = self.paths().clone();
// Get the current vault passphrase from the identity vault
let current_key = self
Expand Down
53 changes: 53 additions & 0 deletions crates/sdk/src/passwd/diceware.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! Helper functions for generating diceware passwords.
use crate::{Error, Result};
use chbs::{
config::{BasicConfig, BasicConfigBuilder},
prelude::*,
probability::Probability,
word::{WordList, WordSampler},
};
use secrecy::{Secret, SecretString};

use once_cell::sync::Lazy;

static WORD_LIST: Lazy<WordList> = Lazy::new(WordList::builtin_eff_large);

/// Generate a passphrase using the given config.
pub fn generate_passphrase_config(
config: &BasicConfig<WordSampler>,
) -> Result<(SecretString, f64)> {
if config.words < 6 {
return Err(Error::DicewareWordsTooFew(config.words, 6));
}

let scheme = config.to_scheme();
Ok((Secret::new(scheme.generate()), scheme.entropy().bits()))
}

/// Generate a diceware passphrase with the given number of words.
///
/// The number of words must be at least six.
pub fn generate_passphrase_words(
words: usize,
) -> Result<(SecretString, f64)> {
let config = default_config(words);
generate_passphrase_config(&config)
}

/// Generate a diceware passphrase with six words which is ~171 bits of entropy.
pub fn generate_passphrase() -> Result<(SecretString, f64)> {
generate_passphrase_words(6)
}

/// Get the default config for diceware passphrase generation.
pub fn default_config(words: usize) -> BasicConfig<WordSampler> {
let config = BasicConfigBuilder::default()
.word_provider(WORD_LIST.sampler())
.words(words)
.separator(' ')
.capitalize_first(Probability::Never)
.capitalize_words(Probability::Never)
.build()
.unwrap();
config
}
Loading