Update copyright notices with scripts/update-copyrights
[glibc.git] / manual / examples / genpass.c
blob79f9d0d2c43258a4822ce9eba6b592f880075bcd
1 /* Encrypting Passwords
2 Copyright (C) 1991-2014 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, if not, see <http://www.gnu.org/licenses/>.
18 #include <stdio.h>
19 #include <time.h>
20 #include <unistd.h>
21 #include <crypt.h>
23 int
24 main(void)
26 unsigned long seed[2];
27 char salt[] = "$1$........";
28 const char *const seedchars =
29 "./0123456789ABCDEFGHIJKLMNOPQRST"
30 "UVWXYZabcdefghijklmnopqrstuvwxyz";
31 char *password;
32 int i;
34 /* Generate a (not very) random seed.
35 You should do it better than this... */
36 seed[0] = time(NULL);
37 seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);
39 /* Turn it into printable characters from `seedchars'. */
40 for (i = 0; i < 8; i++)
41 salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];
43 /* Read in the user's password and encrypt it. */
44 password = crypt(getpass("Password:"), salt);
46 /* Print the results. */
47 puts(password);
48 return 0;