♻️ refactor: extract ptth_diceware to its own crate

main
_ 2022-10-16 19:53:08 +00:00
parent 9d0bf4c0ba
commit 5d560b91de
4 changed files with 1312 additions and 12 deletions

21
Cargo.lock generated
View File

@ -1205,6 +1205,13 @@ dependencies = [
"tracing-futures",
]
[[package]]
name = "ptth_diceware"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "ptth_file_server"
version = "1.1.0"
@ -1474,14 +1481,13 @@ dependencies = [
[[package]]
name = "rand"
version = "0.8.4"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
@ -1503,15 +1509,6 @@ dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
"rand_core",
]
[[package]]
name = "rcgen"
version = "0.8.13"

View File

@ -0,0 +1,9 @@
[package]
name = "ptth_diceware"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.5"

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,41 @@
use rand::Rng;
pub fn main () {
let diceware = Diceware::default ();
let random_words: Vec <&str> = (0..8)
.map (|_| diceware.random_word ())
.collect ();
let passphrase = random_words.join (" ");
println! ("{}", passphrase);
}
pub struct Diceware {
words: Vec <String>,
}
impl Default for Diceware {
fn default () -> Self
{
let wordlist = include_str! ("eff_short_wordlist_1.txt");
let words: Vec <_> = wordlist.split ('\n').take (1253).map (str::to_string).collect ();
assert_eq! (words.len (), 1253);
assert_eq! (words [0], "acid");
assert_eq! (words [600], "large");
assert_eq! (words [1252], "zoom");
Self {
words,
}
}
}
impl Diceware {
pub fn random_word (&self) -> &str
{
&self.words [rand::thread_rng ().gen_range (0..self.words.len ())]
}
}