Created
March 22, 2016 21:48
-
-
Save stokito/74a37a341b4176f8a38e to your computer and use it in GitHub Desktop.
crypt(3) Example Encrypting Passwords https://www.gnu.org/software/libc/manual/html_node/crypt.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <crypt.h> | |
/* | |
To compile: | |
$ gcc check.c -lcrypt -o check | |
$ ./check | |
*/ | |
int | |
main(void) | |
{ | |
/* Hashed form of "GNU libc manual". */ | |
const char *const pass = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/"; | |
char *result; | |
int ok; | |
/* Read in the user’s password and encrypt it, | |
passing the expected password in as the salt. */ | |
result = crypt(getpass("Password:"), pass); | |
/* Test the result. */ | |
ok = strcmp (result, pass) == 0; | |
puts(ok ? "Access granted." : "Access denied."); | |
return ok ? 0 : 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include <crypt.h> | |
/* | |
To compile: | |
$ gcc crypt.c -lcrypt -o crypt | |
$ ./crypt | |
*/ | |
int | |
main(void) | |
{ | |
unsigned long seed[2]; | |
char salt[] = "$1$........"; | |
const char *const seedchars = | |
"./0123456789ABCDEFGHIJKLMNOPQRST" | |
"UVWXYZabcdefghijklmnopqrstuvwxyz"; | |
char *password; | |
int i; | |
/* Generate a (not very) random seed. | |
You should do it better than this... */ | |
seed[0] = time(NULL); | |
seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000); | |
/* Turn it into printable characters from ‘seedchars’. */ | |
for (i = 0; i < 8; i++) | |
salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f]; | |
/* Read in the user’s password and encrypt it. */ | |
password = crypt(getpass("Password:"), salt); | |
/* Print the results. */ | |
puts(password); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment