From 988c7921eacee0cf0b519788c143fe428972b0dc Mon Sep 17 00:00:00 2001 From: Stefan Kreutz Date: Sat, 28 May 2022 15:15:39 +0200 Subject: Extract wpa-psk-cli crate --- wpa-psk-cli/CHANGELOG.md | 16 ++++++++++++++ wpa-psk-cli/Cargo.toml | 19 +++++++++++++++++ wpa-psk-cli/LICENSE-APACHE-2.0 | 12 +++++++++++ wpa-psk-cli/LICENSE-MIT | 19 +++++++++++++++++ wpa-psk-cli/README.md | 47 +++++++++++++++++++++++++++++++++++++++++ wpa-psk-cli/src/bin/wpa-psk.rs | 48 ++++++++++++++++++++++++++++++++++++++++++ wpa-psk-cli/tests/cli.rs | 34 ++++++++++++++++++++++++++++++ 7 files changed, 195 insertions(+) create mode 100644 wpa-psk-cli/CHANGELOG.md create mode 100644 wpa-psk-cli/Cargo.toml create mode 100644 wpa-psk-cli/LICENSE-APACHE-2.0 create mode 100644 wpa-psk-cli/LICENSE-MIT create mode 100644 wpa-psk-cli/README.md create mode 100644 wpa-psk-cli/src/bin/wpa-psk.rs create mode 100644 wpa-psk-cli/tests/cli.rs (limited to 'wpa-psk-cli') diff --git a/wpa-psk-cli/CHANGELOG.md b/wpa-psk-cli/CHANGELOG.md new file mode 100644 index 0000000..3ba3406 --- /dev/null +++ b/wpa-psk-cli/CHANGELOG.md @@ -0,0 +1,16 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep A Changelog][] and this project adheres to +[Semantic Versioning][]. + +[Keep A Changelog]: https://keepachangelog.com/en/1.0.0/ +[Semantic Versioning]: https://semver.org/spec/v2.0.0.html + +## Unreleased + +### Added + +- Added the existing command-line interface from crate `wpa-psk` version 0.1.5. +- Added this changelog. diff --git a/wpa-psk-cli/Cargo.toml b/wpa-psk-cli/Cargo.toml new file mode 100644 index 0000000..6561dde --- /dev/null +++ b/wpa-psk-cli/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "wpa-psk-cli" +version = "0.1.0" +authors = ["Stefan Kreutz "] +edition = "2021" +description = "A command-line utility to compute the WPA-PSK of a Wi-FI SSID and passphrase" +readme = "README.md" +repository = "https://www.skreutz.com/scm/git/wpa-psk.git" +license = "MIT OR Apache-2.0" +keywords = ["wifi", "wpa", "password", "hash"] +categories = ["command-line-utilities"] +publish = true + +[dependencies] +clap = { version = "3.1.18", features = ["derive"] } +wpa-psk = { path = "../wpa-psk", version = "0.2.0" } + +[dev-dependencies] +assert_cmd = "2.0.4" diff --git a/wpa-psk-cli/LICENSE-APACHE-2.0 b/wpa-psk-cli/LICENSE-APACHE-2.0 new file mode 100644 index 0000000..7eefa9d --- /dev/null +++ b/wpa-psk-cli/LICENSE-APACHE-2.0 @@ -0,0 +1,12 @@ +Copyright 2021 Stefan Kreutz + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed +under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +CONDITIONS OF ANY KIND, either express or implied. See the License for the +specific language governing permissions and limitations under the License. diff --git a/wpa-psk-cli/LICENSE-MIT b/wpa-psk-cli/LICENSE-MIT new file mode 100644 index 0000000..4012e61 --- /dev/null +++ b/wpa-psk-cli/LICENSE-MIT @@ -0,0 +1,19 @@ +Copyright 2021 Stefan Kreutz + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/wpa-psk-cli/README.md b/wpa-psk-cli/README.md new file mode 100644 index 0000000..6c00a4c --- /dev/null +++ b/wpa-psk-cli/README.md @@ -0,0 +1,47 @@ +# wpa-psk-cli + +This is a command-line utility to compute the WPA pre-shared key of a Wi-Fi +SSID and passphrase. See also the corresponding Rust library `wpa-psk`. + +## Usage + +Hash a typical SSID and passphrase: + + $ wpa-psk home password123 + 0xde811641af2c516ffd35cc6f851b3abf03c7c84fc703cfb580f1c0456943cdc0 + +Hash *special* albeit valid credentials: + + $ wpa-psk "123abcABC.,-" "456defDEF *<:D" + 0x8a366e5bc51cd5d8fbbeffacc5f1af23fac30e3ac93cdcc368fafbbf63a1085c + +Hash *invalid* credentials: + + $ wpa-psk --force bar 2short + 0xcb5de4e4d23b2ab0bf5b9ba0fe8132c1e2af3bb52298ec801af8ad520cea3437 + +## Installation + +You can install the `wpa-psk` utility using Cargo: + + $ cargo install wpa-psk-cli + +## License + +This work is distributed under the terms of both, the [MIT License][MIT] and +the [Apache License, Version 2.0][Apache-2.0]. + +[MIT]: LICENSE-MIT +[Apache-2.0]: LICENSE-APACHE-2.0 + +## Contribution + +Contributions are welcome! Please [contact][] me via email. + +[contact]: https://www.skreutz.com/contact/ + +## See also + +The popular [wpa_supplicant][] comes with a similar tool, `wpa_passphrase(8)`. + +[wpa_supplicant]: https://hostap.epitest.fi/wpa_supplicant/ diff --git a/wpa-psk-cli/src/bin/wpa-psk.rs b/wpa-psk-cli/src/bin/wpa-psk.rs new file mode 100644 index 0000000..181585c --- /dev/null +++ b/wpa-psk-cli/src/bin/wpa-psk.rs @@ -0,0 +1,48 @@ +use std::process::exit; + +use clap::Parser; +use wpa_psk::{bytes_to_hex, wpa_psk, wpa_psk_unchecked, Passphrase, Ssid}; + +/// Compute the WPA-PSK of a Wi-Fi SSID and passphrase. +#[derive(Debug, Parser)] +#[clap(about, version, author)] +struct Args { + /// An SSID consisting of 1 up to 32 arbitrary bytes. + ssid: String, + + /// A passphrase consisting of 8 up to 63 printable ASCII characters. + passphrase: String, + + /// Don't check the given SSID and passphrase. + #[clap(short, long)] + force: bool, +} + +fn main() { + exit(match run() { + Ok(_) => 0, + Err(err) => { + eprintln!("{}", err); + 1 + } + }) +} + +fn run() -> Result<(), Box> { + let args = Args::try_parse()?; + let psk = if args.force { + wpa_psk_unchecked(args.ssid.as_bytes(), args.passphrase.as_bytes()) + } else { + let ssid = Ssid::try_from(args.ssid.as_bytes())?; + let passphrase = Passphrase::try_from(args.passphrase.as_bytes())?; + wpa_psk(&ssid, &passphrase) + }; + println!("0x{}", bytes_to_hex(&psk)); + Ok(()) +} + +#[test] +fn verify_clap_app() { + use clap::IntoApp; + Args::command().debug_assert() +} diff --git a/wpa-psk-cli/tests/cli.rs b/wpa-psk-cli/tests/cli.rs new file mode 100644 index 0000000..47fab8d --- /dev/null +++ b/wpa-psk-cli/tests/cli.rs @@ -0,0 +1,34 @@ +use std::process::Command; + +use assert_cmd::prelude::*; + +#[test] +fn valid() { + let mut cmd = Command::cargo_bin("wpa-psk").unwrap(); + cmd.arg("home") + .arg("0123-4567-89") + .assert() + .success() + .stdout("0x150c047b6fad724512a17fa431687048ee503d14c1ea87681d4f241beb04f5ee\n"); +} + +#[test] +fn passphrase_too_short() { + let mut cmd = Command::cargo_bin("wpa-psk").unwrap(); + cmd.arg("bar") + .arg("2short") + .assert() + .failure() + .stderr("passphrase must have at least 8 bytes\n"); +} + +#[test] +fn force() { + let mut cmd = Command::cargo_bin("wpa-psk").unwrap(); + cmd.arg("--force") + .arg("bar") + .arg("2short") + .assert() + .success() + .stdout("0xcb5de4e4d23b2ab0bf5b9ba0fe8132c1e2af3bb52298ec801af8ad520cea3437\n"); +} -- cgit v1.2.3