testpass.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Verify a passphrase.
  2. Copyright (C) 1991-2019 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <crypt.h>
  18. /* @samp{GNU's Not Unix} hashed using SHA-256, MD5, and DES. */
  19. static const char hash_sha[] =
  20. "$5$DQ2z5NHf1jNJnChB$kV3ZTR0aUaosujPhLzR84Llo3BsspNSe4/tsp7VoEn6";
  21. static const char hash_md5[] = "$1$A3TxDv41$rtXVTUXl2LkeSV0UU5xxs1";
  22. static const char hash_des[] = "FgkTuF98w5DaI";
  23. int
  24. main(void)
  25. {
  26. char *phrase;
  27. int status = 0;
  28. /* Prompt for a passphrase. */
  29. phrase = getpass ("Enter passphrase: ");
  30. /* Compare against the stored hashes. Any input that begins with
  31. @samp{GNU's No} will match the DES hash, but the other two will
  32. only match @samp{GNU's Not Unix}. */
  33. if (strcmp (crypt (phrase, hash_sha), hash_sha))
  34. {
  35. puts ("SHA: not ok");
  36. status = 1;
  37. }
  38. else
  39. puts ("SHA: ok");
  40. if (strcmp (crypt (phrase, hash_md5), hash_md5))
  41. {
  42. puts ("MD5: not ok");
  43. status = 1;
  44. }
  45. else
  46. puts ("MD5: ok");
  47. if (strcmp (crypt (phrase, hash_des), hash_des))
  48. {
  49. puts ("DES: not ok");
  50. status = 1;
  51. }
  52. else
  53. puts ("DES: ok");
  54. return status;
  55. }