genpass.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Encrypting Passwords
  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 <unistd.h>
  16. #include <crypt.h>
  17. int
  18. main(void)
  19. {
  20. unsigned char ubytes[16];
  21. char salt[20];
  22. const char *const saltchars =
  23. "./0123456789ABCDEFGHIJKLMNOPQRST"
  24. "UVWXYZabcdefghijklmnopqrstuvwxyz";
  25. char *hash;
  26. int i;
  27. /* Retrieve 16 unpredictable bytes from the operating system. */
  28. if (getentropy (ubytes, sizeof ubytes))
  29. {
  30. perror ("getentropy");
  31. return 1;
  32. }
  33. /* Use them to fill in the salt string. */
  34. salt[0] = '$';
  35. salt[1] = '5'; /* SHA-256 */
  36. salt[2] = '$';
  37. for (i = 0; i < 16; i++)
  38. salt[3+i] = saltchars[ubytes[i] & 0x3f];
  39. salt[3+i] = '\0';
  40. /* Read in the user's passphrase and hash it. */
  41. hash = crypt (getpass ("Enter new passphrase: "), salt);
  42. if (!hash || hash[0] == '*')
  43. {
  44. perror ("crypt");
  45. return 1;
  46. }
  47. /* Print the results. */
  48. puts (hash);
  49. return 0;
  50. }