1 /* Encrypting Passwords
2 Copyright (C) 1991-2022 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>.
25 unsigned char ubytes
[16];
27 const char *const saltchars
=
28 "./0123456789ABCDEFGHIJKLMNOPQRST"
29 "UVWXYZabcdefghijklmnopqrstuvwxyz";
33 /* Retrieve 16 unpredictable bytes from the operating system. */
34 if (getentropy (ubytes
, sizeof ubytes
))
36 perror ("getentropy");
40 /* Use them to fill in the salt string. */
42 salt
[1] = '5'; /* SHA-256 */
44 for (i
= 0; i
< 16; i
++)
45 salt
[3+i
] = saltchars
[ubytes
[i
] & 0x3f];
48 /* Read in the user's passphrase and hash it. */
49 hash
= crypt (getpass ("Enter new passphrase: "), salt
);
50 if (!hash
|| hash
[0] == '*')
56 /* Print the results. */