xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Encrypting Passwords |
| 2 | Copyright (C) 1991-2016 Free Software Foundation, Inc. |
| 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 | |
| 18 | #include <stdio.h> |
| 19 | #include <time.h> |
| 20 | #include <unistd.h> |
| 21 | #include <crypt.h> |
| 22 | |
| 23 | int |
| 24 | main(void) |
| 25 | { |
| 26 | unsigned long seed[2]; |
| 27 | char salt[] = "$1$........"; |
| 28 | const char *const seedchars = |
| 29 | "./0123456789ABCDEFGHIJKLMNOPQRST" |
| 30 | "UVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 31 | char *password; |
| 32 | int i; |
| 33 | |
| 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); |
| 38 | |
| 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]; |
| 42 | |
| 43 | /* Read in the user's password and encrypt it. */ |
| 44 | password = crypt(getpass("Password:"), salt); |
| 45 | |
| 46 | /* Print the results. */ |
| 47 | puts(password); |
| 48 | return 0; |
| 49 | } |