commit d419f979cf89b4e9cf72b5f4590a5453034700d7
parent 2e6e61f9884315189deebc569a581b857691415b
Author: Yuval Langer <yuval.langer@gmail.com>
Date: Sun, 14 Jun 2015 02:31:13 +0300
MiniLock wordlist now works
Diffstat:
M | build.rs | | | 36 | +++++++++++++++++++++++++++--------- |
M | src/main.rs | | | 37 | +++++++++++++++++++++++++++++++++++++ |
2 files changed, 64 insertions(+), 9 deletions(-)
diff --git a/build.rs b/build.rs
@@ -3,6 +3,7 @@ use std::fs::File;
use std::io::Write;
use std::io::Read;
use std::path::Path;
+use std::string::String;
fn make_wordlist(contents: &std::string::String) -> Vec<&str> {
@@ -13,11 +14,10 @@ fn make_wordlist(contents: &std::string::String) -> Vec<&str> {
.collect()
}
-fn make_minilock_wordlist(contents: &std::string::String) -> Vec<&str> {
- contents.split(',')
- .skip(3)
- .take(58109)
- .collect()
+fn make_minilock_wordlist(contents: &str) -> Vec<&str> {
+ unsafe {
+ contents.slice_unchecked(1023, 543718).split(',').collect()
+ }
}
fn make_beale_struct(wordlist: Vec<&str>) -> std::string::String {
@@ -56,21 +56,39 @@ fn make_reinhold_struct(wordlist: Vec<&str>) -> std::string::String {
return output;
}
+fn make_minilock_struct(wordlist: Vec<&str>) -> std::string::String {
+ let mut output = std::string::String::new();
+ output.push_str("const MINILOCK_WORDLIST: [MiniLockWord; 58110] = [\n");
+ for word in wordlist {
+ output.push_str(" MiniLockWord(\"");
+ for c in word.chars() {
+ if c == '"' {
+ panic!("Not supposed to have any double quotes.");
+ } else {
+ output.push(c);
+ }
+ }
+ output.push_str("\"),\n");
+ }
+ output.push_str("];\n");
+ return output;
+}
+
fn main() {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
println!("{}", manifest_dir);
let wordlists_dir = Path::new(&manifest_dir).join("bin").join("wordlists");
let mut beale_wordlist_file = File::open(&wordlists_dir.join("beale.wordlist.asc")).unwrap();
let mut reinhold_wordlist_file = File::open(&wordlists_dir.join("diceware.wordlist.asc")).unwrap();
- let mut minilock_wordlist_file = File::open(&wordlists_dir.join("phrase.js"));
+ let mut minilock_wordlist_file = File::open(&wordlists_dir.join("phrase.js")).unwrap();
let mut beale_wordlist_string = String::new();
let mut reinhold_wordlist_string = String::new();
let mut minilock_wordlist_string = String::new();
- beale_wordlist_file.read_to_string(&mut beale_wordlist_string);
- reinhold_wordlist_file.read_to_string(&mut reinhold_wordlist_string);
- minilock_wordlist_file.read_to_string(&mut minilock_wordlist_string);
+ beale_wordlist_file.read_to_string(&mut beale_wordlist_string).unwrap();
+ reinhold_wordlist_file.read_to_string(&mut reinhold_wordlist_string).unwrap();
+ minilock_wordlist_file.read_to_string(&mut minilock_wordlist_string).unwrap();
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("diceware.rs");
diff --git a/src/main.rs b/src/main.rs
@@ -22,6 +22,9 @@ mod diceware {
#[derive(Debug,Clone,Eq,PartialEq)]
pub struct ReinholdWord(&'static str);
+ #[derive(Debug,Clone,Eq,PartialEq)]
+ pub struct MiniLockWord(&'static str);
+
impl BealeWord {
pub fn new(word: &'static str) -> BealeWord {
BealeWord(word.clone())
@@ -61,6 +64,20 @@ mod diceware {
}
}
}
+
+ impl rand::Rand for MiniLockWord {
+ fn rand<R: rand::Rng>(rng: &mut R) -> MiniLockWord {
+ rng.choose(&MINILOCK_WORDLIST).unwrap().clone()
+ }
+ }
+
+ impl fmt::Display for MiniLockWord {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ &MiniLockWord(w) => write!(f, "{}", w)
+ }
+ }
+ }
}
/*
@@ -116,12 +133,32 @@ fn main() {
}
};
*/
+
let mut rng = rand::OsRng::new().unwrap();
+
+ println!("Beale:");
for _ in 1..8 {
let word: diceware::BealeWord = rng.gen();
print!("{} ", word);
}
println!("");
+ println!("");
+
+ println!("Reinhold:");
+ for _ in 1..8 {
+ let word: diceware::ReinholdWord = rng.gen();
+ print!("{} ", word);
+ }
+ println!("");
+ println!("");
+
+ println!("MiniLock:");
+ for _ in 1..8 {
+ let word: diceware::MiniLockWord = rng.gen();
+ print!("{} ", word);
+ }
+ println!("");
+
}
#[cfg(test)]