pub struct Psk(/* private fields */);Expand description
A WPA pre-shared key, validated at construction.
wpa_supplicant takes the psk field in two distinct forms and encodes them
differently on the control socket: a passphrase is sent as a quoted string,
while a precomputed 256-bit key is sent as raw, unquoted hex. An unquoted
value is always read as a raw key, so a passphrase can never fall back to
hex encoding the way an SSID can. The two constructors let the caller say
which form it means instead of the library guessing from the string’s shape.
Use Psk::passphrase or Psk::raw when the kind is known; parse with
str::parse to get wpa_supplicant.conf semantics (exactly 64 hex digits
is a raw key, anything else a passphrase — unambiguous because a passphrase
is at most 63 characters).
use wifi_ctrl::sta::Psk;
// The constructors say which form is meant.
let psk = Psk::passphrase("correct horse battery")?;
let _key = Psk::raw([0x42; 32]);
// Parsing follows wpa_supplicant.conf: anything but 64 hex digits is a passphrase,
// and passphrases outside 8-63 printable-ASCII characters are rejected.
let _parsed: Psk = "correct horse battery".parse()?;
assert!("short".parse::<Psk>().is_err());
// Debug never reveals key material.
assert_eq!(format!("{psk:?}"), "Psk(<redacted>)");Implementations§
Source§impl Psk
impl Psk
Sourcepub fn passphrase(passphrase: impl Into<String>) -> Result<Self>
pub fn passphrase(passphrase: impl Into<String>) -> Result<Self>
A WPA passphrase: 8-63 printable-ASCII characters (spaces included).
A literal double-quote is rejected even though WPA formally allows it:
the control socket has no escape for one inside a quoted value, so it
has no safe representation. Anything else outside printable ASCII has
no valid representation either. Both yield ClientError::InvalidPsk.
assert!(Psk::passphrase("password123").is_ok());
assert!(Psk::passphrase("2short").is_err());
assert!(Psk::passphrase("pass\"word123").is_err()); // no escape for a quote