initial commit uwu

This commit is contained in:
anna 2022-06-01 05:15:31 +00:00
commit 6a8625aa36
Signed by: fef
GPG key ID: EC22E476DC2D3D84
8 changed files with 10495 additions and 0 deletions

10
.editorconfig Normal file
View file

@ -0,0 +1,10 @@
root = true
[*]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

206
Cargo.lock generated Normal file
View file

@ -0,0 +1,206 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "3.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2dbdf4bdacb33466e854ce889eee8dfd5729abf7ccd7664d0a2d60cd384440b"
dependencies = [
"atty",
"bitflags",
"clap_lex",
"indexmap",
"strsim",
"termcolor",
"textwrap",
]
[[package]]
name = "clap_lex"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "getrandom"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "indexmap"
version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6012d540c5baa3589337a98ce73408de9b5a25ec9fc2c6fd6be8f0d39e0ca5a"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "libc"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
[[package]]
name = "os_str_bytes"
version = "6.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21326818e99cfe6ce1e524c2a805c189a99b5ae555a35d19f9a284b427d86afa"
[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "xkpw"
version = "0.1.0"
dependencies = [
"clap",
"rand",
]

10
Cargo.toml Normal file
View file

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

22
LICENSE Normal file
View file

@ -0,0 +1,22 @@
Copyright 2022 anna <owo@fef.moe>
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

27
README.md Normal file
View file

@ -0,0 +1,27 @@
# xkpw
xkpw is a simple command line utility for generating passwords in the style
of [xkpasswd](https://xkpasswd.net/), which in turn was inspired by
[xkcd #936](https://xkcd.com/936/).
Only a minimal subset of the features is supported at the moment; run the
program with `--help` to get an overview of all options.
## Disclaimer
This is **alpha grade software**.
I have no idea whether the passwords this tool spits out are actually any good,
even though they *seem* reasonable at first glance.
Use at your own risk and be aware of the security implications.
## Legal Stuff
Copyright © 2022 anna <owo@fef.moe>.
This program is licensed under the Simplified BSD License.
There is NO WARRANTY, to the extent permitted by law.
See the `LICENSE` file for details.
The `dict.txt` file was taken from
[google-10000-english.txt](https://github.com/first20hours/google-10000-english)
by Josh Kaufman et al.,
and to be honest i have no idea whether it's even okay to just steal it like
this lmao.

10000
dict.txt Normal file

File diff suppressed because it is too large Load diff

219
src/main.rs Normal file
View file

@ -0,0 +1,219 @@
// See the end of this file for license terms and copyright info.
use std::fs::File;
use std::io::{prelude::*, BufReader};
use clap::{Arg, ArgMatches, Command};
use rand::Rng;
struct Config {
pub num: u32,
pub words: u32,
pub dict: String,
pub separator: char,
}
static DEFAULT_SEPARATORS: &'static [char] = &['-', '+', '=', '/', '%', '#', '!'];
fn main() {
let cmd = Command::new("xkpw")
.version("0.1.0")
.author("anna <owo@fef.moe>")
.about("xkcd style password generator")
.arg(
Arg::new("num")
.short('n')
.takes_value(true)
.help("Number of passwords to generate"),
)
.arg(
Arg::new("dict")
.short('f')
.takes_value(true)
.help("Dictionary file to choose words from"),
)
.arg(
Arg::new("words")
.short('w')
.takes_value(true)
.help("Number of words"),
)
.arg(
Arg::new("separator")
.short('s')
.takes_value(true)
.help("Separator character"),
)
.arg(
Arg::new("license")
.long("license")
.takes_value(false)
.help("Print license information and exit"),
);
let matches = cmd.get_matches();
if matches.is_present("license") {
println!("{}", LICENSE);
return;
}
let config = Config::from_args(&matches);
let config = match config {
Ok(c) => c,
Err(e) => {
eprintln!("{}", e.to_string());
return;
}
};
let dict = lines_from_file(&config.dict);
let dict = match dict {
Ok(d) => d,
Err(e) => {
eprintln!("Cannot open dictionary file {}: {}", config.dict, e);
return;
}
};
for _ in 0..config.num {
println!("{}", generate(&config, &dict));
}
}
/// Generate a single password.
fn generate(config: &Config, dict: &Vec<String>) -> String {
let mut words: Vec<&str> = Vec::with_capacity(config.words as usize);
let mut rng = rand::thread_rng();
let len = dict.len();
let sep = String::from(config.separator);
for _ in 0..config.words {
words.push(dict[rng.gen_range(0..len)].as_str());
}
format!("{:02}{}{}{}{:02}", rng.gen_range(0..=99), config.separator, words.join(sep.as_str()), config.separator, rng.gen_range(0..=99))
}
fn lines_from_file(filename: &String) -> Result<Vec<String>, std::io::Error> {
let file = File::open(filename)?;
let reader = BufReader::new(file);
reader.lines().collect()
}
// the rest of this file is really just argument parsing and not that interesting.
// also, it's gotten kind of complex because i wanted to be smart about the whole
// thing and couple the type system directly into the parser for some reason.
/// Argument type.
/// This is only used for displaying errors at the moment.
enum ArgType {
Char,
Int,
}
impl ArgType {
pub fn to_str(&self) -> &str {
match self {
ArgType::Char => "character",
ArgType::Int => "integer",
}
}
}
struct ArgError {
arg: String,
expected: ArgType,
actual: String,
}
impl ArgError {
pub fn new(arg: &str, expected: ArgType, actual: &str) -> ArgError {
ArgError {
arg: String::from(arg),
expected,
actual: String::from(actual),
}
}
pub fn to_string(&self) -> String {
format!(
"Argument \"{}\": Expected {}, got \"{}\" instead",
self.arg,
self.expected.to_str(),
self.actual
)
}
}
/// This trait does the type magic for parsing arguments.
/// The `default` parameter dictates what type it parses the argument to.
trait ParseArg<T> {
fn parse_arg(matches: &ArgMatches, val: &str, default: T) -> Result<T, ArgError>;
}
impl Config {
pub fn from_args(matches: &ArgMatches) -> Result<Config, ArgError> {
let mut rng = rand::thread_rng();
Ok(Config {
num: Config::parse_arg(matches, "num", 1)?,
words: Config::parse_arg(matches, "words", 3)?,
dict: Config::parse_arg(matches, "dict", String::from("dict.txt"))?,
separator: Config::parse_arg(
matches,
"separator",
DEFAULT_SEPARATORS[rng.gen_range(0..DEFAULT_SEPARATORS.len())],
)?,
})
}
}
impl ParseArg<char> for Config {
fn parse_arg(matches: &ArgMatches, name: &str, default: char) -> Result<char, ArgError> {
let default = default.to_string();
let val = matches.value_of(name).unwrap_or(default.as_str());
match val.len() {
1 => Ok(val.chars().nth(0).unwrap()),
_ => Err(ArgError::new(name, ArgType::Char, val)),
}
}
}
impl ParseArg<u32> for Config {
fn parse_arg(matches: &ArgMatches, name: &str, default: u32) -> Result<u32, ArgError> {
let default = default.to_string();
let val = matches.value_of(name).unwrap_or(default.as_str());
match val.parse::<u32>() {
Ok(num) => Ok(num),
Err(_) => Err(ArgError::new(name, ArgType::Int, val)),
}
}
}
impl ParseArg<String> for Config {
fn parse_arg(matches: &ArgMatches, name: &str, default: String) -> Result<String, ArgError> {
Ok(String::from(
matches.value_of(name).unwrap_or(default.as_str()),
))
}
}
static LICENSE: &'static str = "Copyright 2022 anna <owo@fef.moe>
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.";