]> git.ipfire.org Git - thirdparty/glibc.git/blame - manual/examples/genpass.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / manual / examples / genpass.c
CommitLineData
d9a17c07 1/* Encrypting Passwords
f7a9f785 2 Copyright (C) 1991-2016 Free Software Foundation, Inc.
d9a17c07
RM
3
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.
8
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.
13
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/>.
16*/
17
63f791d3
GK
18#include <stdio.h>
19#include <time.h>
20#include <unistd.h>
21#include <crypt.h>
22
d9a17c07 23int
63f791d3
GK
24main(void)
25{
26 unsigned long seed[2];
27 char salt[] = "$1$........";
d9a17c07 28 const char *const seedchars =
63f791d3
GK
29 "./0123456789ABCDEFGHIJKLMNOPQRST"
30 "UVWXYZabcdefghijklmnopqrstuvwxyz";
31 char *password;
32 int i;
d9a17c07
RM
33
34 /* Generate a (not very) random seed.
63f791d3
GK
35 You should do it better than this... */
36 seed[0] = time(NULL);
37 seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);
d9a17c07 38
63f791d3
GK
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];
d9a17c07 42
63f791d3
GK
43 /* Read in the user's password and encrypt it. */
44 password = crypt(getpass("Password:"), salt);
d9a17c07 45
63f791d3
GK
46 /* Print the results. */
47 puts(password);
48 return 0;
49}