My first Rust crate

Posted on

Recently I decided to give Rust another try – if only to compare it to Haskell. After reading through the decent documentation, especially the book, I felt prepared to write some remotely useful application: a command-line application to compute the WPA pre-shared key of a Wi-Fi from its SSID and passphrase.

Why? Because I needed to manually connect to some wireless networks with special and sometimes downright invalid SSIDs and passphrases. Of course, the popular wpa_supplicant comes with a similar tool, wpa_passphrase(8). But where’s the fun in that?

So here it is: my very first Rust crate, wpa-psk, on crates.io and docs.rs.

The command-line interface looks like this:

$ wpa-psk "123abcABC.,-" "456defDEF *<:D"
0x8a366e5bc51cd5d8fbbeffacc5f1af23fac30e3ac93cdcc368fafbbf63a1085c

$ wpa-psk --force "bar" "2short"
0xcb5de4e4d23b2ab0bf5b9ba0fe8132c1e2af3bb52298ec801af8ad520cea3437

And the Rust interface looks like this:

let ssid = Ssid::try_from("home")?;
let passphrase = Passphrase::try_from("0123-4567-89")?;
let psk = wpa_psk(&ssid, &passphrase);
assert_eq!(
    bytes_to_hex(&psk),
    "150c047b6fad724512a17fa431687048ee503d14c1ea87681d4f241beb04f5ee"
);

Note the validating newtypes Ssid and Passphrase. I like that. Feels like Haskell :)