AArch64: Add SVE memcpy
[glibc.git] / manual / examples / genpass.c
blob6a8ccf93c73e8f01cad38cb5c0fb0c6b4c8684d5
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/>.
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <crypt.h>
22 int
23 main(void)
25 unsigned char ubytes[16];
26 char salt[20];
27 const char *const saltchars =
28 "./0123456789ABCDEFGHIJKLMNOPQRST"
29 "UVWXYZabcdefghijklmnopqrstuvwxyz";
30 char *hash;
31 int i;
33 /* Retrieve 16 unpredictable bytes from the operating system. */
34 if (getentropy (ubytes, sizeof ubytes))
36 perror ("getentropy");
37 return 1;
40 /* Use them to fill in the salt string. */
41 salt[0] = '$';
42 salt[1] = '5'; /* SHA-256 */
43 salt[2] = '$';
44 for (i = 0; i < 16; i++)
45 salt[3+i] = saltchars[ubytes[i] & 0x3f];
46 salt[3+i] = '\0';
48 /* Read in the user's passphrase and hash it. */
49 hash = crypt (getpass ("Enter new passphrase: "), salt);
50 if (!hash || hash[0] == '*')
52 perror ("crypt");
53 return 1;
56 /* Print the results. */
57 puts (hash);
58 return 0;