wpa_passphrase.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * WPA Supplicant - ASCII passphrase to WPA PSK tool
  3. * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/sha1.h"
  11. int main(int argc, char *argv[])
  12. {
  13. unsigned char psk[32];
  14. int i;
  15. char *ssid, *passphrase, buf[64], *pos;
  16. if (argc < 2) {
  17. printf("usage: wpa_passphrase <ssid> [passphrase]\n"
  18. "\nIf passphrase is left out, it will be read from "
  19. "stdin\n");
  20. return 1;
  21. }
  22. ssid = argv[1];
  23. if (argc > 2) {
  24. passphrase = argv[2];
  25. } else {
  26. printf("# reading passphrase from stdin\n");
  27. if (fgets(buf, sizeof(buf), stdin) == NULL) {
  28. printf("Failed to read passphrase\n");
  29. return 1;
  30. }
  31. buf[sizeof(buf) - 1] = '\0';
  32. pos = buf;
  33. while (*pos != '\0') {
  34. if (*pos == '\r' || *pos == '\n') {
  35. *pos = '\0';
  36. break;
  37. }
  38. pos++;
  39. }
  40. passphrase = buf;
  41. }
  42. if (os_strlen(passphrase) < 8 || os_strlen(passphrase) > 63) {
  43. printf("Passphrase must be 8..63 characters\n");
  44. return 1;
  45. }
  46. pbkdf2_sha1(passphrase, (u8 *) ssid, os_strlen(ssid), 4096, psk, 32);
  47. printf("network={\n");
  48. printf("\tssid=\"%s\"\n", ssid);
  49. printf("\t#psk=\"%s\"\n", passphrase);
  50. printf("\tpsk=");
  51. for (i = 0; i < 32; i++)
  52. printf("%02x", psk[i]);
  53. printf("\n");
  54. printf("}\n");
  55. return 0;
  56. }