/* * Copyright (C) 2010 Christian Franke * * Compile this program with something like: * cc -o crypt crypt.c -lcrypt * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA * */ #define _XOPEN_SOURCE 500 #include #include #include #include #include #include #include #include #include static const char salt_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"; int main(int argc, char* argv[]) { char salt[4]; char *passwd; char *getpass_ret; int i; int fd = open("/dev/random", O_RDONLY); if (fd < 0) { perror("Error getting random source for salt"); exit(EXIT_FAILURE); } for (i = 0; i < 4; ++i) { ssize_t red; do { red = read(fd, salt + i, sizeof(salt[0])); if (red < 0) { perror("Error getting random bytes for salt"); exit(EXIT_FAILURE); } } while (red < sizeof(salt[0])); salt[i] = salt_chars[salt[i] % (sizeof(salt_chars) / sizeof(salt_chars[0]))]; } close(fd); getpass_ret = getpass("Enter password: "); if (!getpass_ret) { perror("Error getting password"); exit(EXIT_FAILURE); } passwd = strdup(getpass_ret); getpass_ret = getpass("Reenter password: "); if (!getpass_ret) { perror("Error getting password"); exit(EXIT_FAILURE); } if (strcmp(passwd, getpass_ret)) { fprintf(stderr, "Error: the passwords don't match.\n"); exit(EXIT_FAILURE); } memset(passwd, 0, strlen(passwd)); free(passwd); passwd = crypt(getpass_ret, salt); if (!passwd) { perror("Error calculating crypt-pw"); exit(EXIT_FAILURE); } printf("CRYPT-PW %s\n", passwd); memset(passwd, 0, strlen(passwd)); memset(getpass_ret, 0, strlen(getpass_ret)); exit(EXIT_SUCCESS); }