summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichaël Ball <michael.ball@gmail.com>2017-12-07 21:01:30 +0000
committerMichaël Ball <michael.ball@gmail.com>2017-12-07 21:01:30 +0000
commit75b28f31e5603c39215b7c91b35610269c7fd0ed (patch)
tree71d47112dbce48dd49db97731b1fb9b93d439790
Initial commit
-rw-r--r--.gitignore3
-rw-r--r--Cargo.toml8
-rw-r--r--src/main.rs107
3 files changed, 118 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0196246
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+
+/target/
+**/*.rs.bk
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..9ee688f
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "passphrasegen"
+version = "0.1.0"
+authors = ["Michaël Ball <michael.ball@gmail.com>"]
+
+[dependencies]
+rand = "0.3"
+getopts = "0.2" \ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..c78441e
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,107 @@
+extern crate getopts;
+extern crate rand;
+
+use getopts::Options;
+use rand::{thread_rng, Rng};
+use std::env;
+use std::vec::Vec;
+use std::fs::File;
+use std::io::{self, BufRead, BufReader};
+use std::path::Path;
+
+fn lines_from_file<P>(filename: &P) -> Result<Vec<String>, io::Error>
+ where P: AsRef<Path>
+{
+ let file = try!(File::open(filename));
+ let buf = BufReader::new(file);
+ Ok(buf.lines()
+ .map(|l| l.expect("Could not parse line"))
+ .collect())
+}
+
+fn print_usage(program: &str, opts: Options) {
+ let brief = format!("Usage: {} DICTIONARY_FILE [options]", program);
+ print!("{}", opts.usage(&brief));
+}
+
+fn random_words_from_dictionary(dictionary: &Vec<String>, words: &usize) -> Vec<String> {
+ let mut rng = thread_rng();
+ let max_idx = dictionary.len() - 1;
+
+ let mut vec = Vec::with_capacity(*words);
+ for _ in 0..*words {
+ let n: usize = rng.gen_range(0, max_idx);
+ let ref token = dictionary[n];
+ vec.push(token.to_string());
+ }
+
+ vec
+}
+
+#[test]
+fn test_random_words_from_dictionary() {
+ let test_dict = vec!["This".to_string(),
+ "is".to_string(),
+ "a".to_string(),
+ "test".to_string(),
+ "vector".to_string()];
+ let num_words = 3;
+
+ let new_vec = random_words_from_dictionary(&test_dict, &num_words);
+ assert_eq!(new_vec.len(), 3);
+}
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ let program = args[0].clone();
+
+ let mut opts = Options::new();
+ opts.optopt("w",
+ "",
+ "number of words to use in passphrase, default is 4",
+ "WORDS");
+
+ let matches = match opts.parse(&args[1..]) {
+ Ok(m) => m,
+ Err(f) => panic!("{}", f),
+ };
+
+ let words_opt = matches.opt_str("w");
+
+ let input = if !matches.free.is_empty() {
+ matches.free[0].clone()
+ } else {
+ print_usage(&program, opts);
+ return;
+ };
+
+ let num_words;
+ match words_opt {
+ Some(x) => {
+ match x.parse::<usize>() {
+ Ok(n) => num_words = n,
+ Err(_e) => {
+ println!("Please enter an integer for the -w flag");
+ return;
+ }
+ }
+ }
+ None => num_words = 4,
+ }
+
+ let lines;
+
+ match lines_from_file(&input) {
+ Ok(n) => lines = n,
+ Err(_e) => {
+ println!("Cannot read file {}. Please ensure it exists and you have permission to \
+ read it.",
+ input);
+ return;
+ }
+ }
+
+ let passphrase = random_words_from_dictionary(&lines, &num_words).join(" ");
+
+ println!("{}", passphrase);
+}
7'>307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567