keyfork-mnemonic-generate: alter signature of generate_mnemonic to take size as input

This commit is contained in:
Ryan Heywood 2023-09-12 00:29:01 -05:00
parent 76d369a6d5
commit b7fd2ef8e5
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
3 changed files with 16 additions and 15 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "keyfork-mnemonic-generate" name = "keyfork-mnemonic-generate"
version = "0.1.1" version = "0.2.0"
description = "A tool to generate BIP-0039 mnemonics." description = "A tool to generate BIP-0039 mnemonics."
license = "GPL-3.0" license = "GPL-3.0"
repository = "https://git.distrust.co/public/keyfork" repository = "https://git.distrust.co/public/keyfork"

View File

@ -1,5 +1,5 @@
use std::{ use std::{
env, env::vars,
error::Error, error::Error,
fs::{read_dir, read_to_string, File}, fs::{read_dir, read_to_string, File},
io::Read, io::Read,
@ -78,24 +78,14 @@ fn ensure_offline() {
} }
} }
pub fn generate_mnemonic() -> Result<Mnemonic> { pub fn generate_mnemonic(bit_size: usize) -> Result<Mnemonic> {
if !env::vars() if !vars()
.any(|(name, _)| name == "SHOOT_SELF_IN_FOOT" || name == "INSECURE_HARDWARE_ALLOWED") .any(|(name, _)| name == "SHOOT_SELF_IN_FOOT" || name == "INSECURE_HARDWARE_ALLOWED")
{ {
ensure_safe_kernel_version(); ensure_safe_kernel_version();
ensure_offline(); ensure_offline();
} }
let bit_size: usize = env::args()
.nth(1)
.unwrap_or(String::from("256"))
.parse()
.expect("Expected integer bit size");
assert!(
bit_size == 128 || bit_size == 256,
"Only 12 or 24 word mnemonics are supported"
);
let mut rng = Entropy::new()?; let mut rng = Entropy::new()?;
let entropy = &mut [0u8; 256 / 8]; let entropy = &mut [0u8; 256 / 8];
rng.read_into(&mut entropy[..])?; rng.read_into(&mut entropy[..])?;

View File

@ -1,7 +1,18 @@
use std::env::args;
use keyfork_mnemonic_generate::*; use keyfork_mnemonic_generate::*;
fn main() -> Result<()> { fn main() -> Result<()> {
let mnemonic = generate_mnemonic()?; let bit_size: usize = args()
.nth(1)
.unwrap_or(String::from("256"))
.parse()
.expect("Expected integer bit size");
assert!(
bit_size == 128 || bit_size == 256,
"Only 12 or 24 word mnemonics are supported"
);
let mnemonic = generate_mnemonic(bit_size)?;
println!("{mnemonic}"); println!("{mnemonic}");