#!/usr/bin/env python3 r"""Print a Wi-Fi network's WPA PSK for wpa_supplicant, from the passphrase on stdin. Usage: wifi_psk.py SSID NAME < passphrase prints NAME=<64 hex digits>, a line for networking.wireless.secretsFile. The passphrase never goes on argv, and the box gets only the PSK. wpa_passphrase does the same, but won't read from a pipe (tcgetattr fails). >>> psk('IEEE', 'password') # IEEE 802.11i, H.4.1 test vector 'f42c6fc52df0ebef9ebb4b90b38a5f902e83fe1b135a70e23aed762e9710a12e' """ import hashlib def psk(ssid, passphrase): """WPA PSK: PBKDF2-HMAC-SHA1, salted by the SSID, 4096 rounds, 32 bytes.""" return hashlib.pbkdf2_hmac('sha1', passphrase.encode(), ssid.encode(), 4096, 32).hex() def main(argv, stdin, stdout): [_, ssid, name] = argv passphrase = stdin.read().rstrip('\n') if not 8 <= len(passphrase) <= 63: raise SystemExit('a WPA passphrase is 8 to 63 characters') stdout.write(f'{name}={psk(ssid, passphrase)}\n') if __name__ == '__main__': def _script_io(): from sys import argv, stdin, stdout main(list(argv), stdin, stdout) _script_io()