summaryrefslogtreecommitdiff
path: root/wpa-psk-cli/src/bin/wpa-psk.rs
blob: 3a4ca6b728f4f44708771ce7ea11550e0f65963a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! A command-line utility to compute the WPA-PSK of a Wi-Fi SSID and passphrase.

#![forbid(unsafe_code)]
#![deny(missing_docs)]

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<dyn std::error::Error>> {
    #[cfg(target_os = "openbsd")]
    pledge::pledge![Stdio, Stdio]?;

    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::CommandFactory;
    Args::command().debug_assert()
}
Generated by cgit. See skreutz.com for my tech blog and contact information.