Last active
June 20, 2022 16:30
-
-
Save djds/d50beaac295789a183aa79f7850aec53 to your computer and use it in GitHub Desktop.
Generate `wpa_supplicant.conf` compatible psk hashes using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Copyright (c) 2022 djds <djds@bghost.xyz> | |
# | |
# Permission to use, copy, modify, and distribute this software for any | |
# purpose with or without fee is hereby granted, provided that the above | |
# copyright notice and this permission notice appear in all copies. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
"""Generate `wpa_supplicant.conf` compatible psk hashes using Python""" | |
import sys | |
from hashlib import pbkdf2_hmac | |
def main() -> int: | |
if len(sys.argv) == 1: | |
print( | |
"usage: wpa_passphrase <ssid> [passphrase]\n\n" | |
"If passphrase is left out, it will be read from stdin", | |
file=sys.stderr, | |
) | |
return 1 | |
ssid: str = sys.argv[1] | |
passphrase: str = sys.argv[2] | |
if len(passphrase) not in range(8, 64): | |
print("Passphrase must be 8..63 characters", file=sys.stderr) | |
return 1 | |
psk: str = pbkdf2_hmac( | |
"sha1", | |
passphrase.encode("utf_8"), | |
ssid.encode("utf_8"), | |
4096, | |
dklen=32, | |
).hex() | |
print(f'network={{\n\tssid="{ssid}"\n\tpsk={psk}\n}}') | |
return 0 | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment