Jun 2, 2011 at 3:28pm UTC
Hello
Could anyone please explain to me the meaning of each line of the program below I marked with question marks (???). I copied it from a website. It encrypts and decrypts a password entered by the user using the crypt.h file.
The following program takes and sets a password entered by the user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <crypt.h>
int
main(void )
{
unsigned long seed[2];
char salt[] = "$1$........" ;
const char *const seedchars =
"./0123456789ABCDEFGHIJKLMNOPQRST"
"UVWXYZabcdefghijklmnopqrstuvwxyz" ;
char *password;
int i;
seed[0] = time(NULL); // ??????????????
seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000); // ??????????????
for (i = 0; i < 8; i++)
salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f]; // ??????????????
password = crypt(getpass("Password:" ), salt); // ??????????????
puts(password);
return 0;
}
And the following program verifies the password entered by the user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <crypt.h>
int
main(void )
{
const char *const pass = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/" ;
char *result;
int ok;
result = crypt(getpass("Password:" ), pass); // ??????????????
ok = strcmp (result, pass) == 0;
puts(ok ? "Access granted." : "Access denied." );
return ok ? 0 : 1;
}
Many thanks in advace
Last edited on Jun 2, 2011 at 3:54pm UTC
Jun 2, 2011 at 4:35pm UTC
<crypt.h>
is provided by the standard C library.
It uses very complex algorithms such as hash functions
http://en.wikipedia.org/wiki/Hash_function
The lines:
1 2
seed[0] = time(NULL);
seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);
Seem to initialize
salt which is passed to
crypt()
I am not certain about the implementation of
crypt()
however seeing the bitwise operations in advance I would assume it is very complex and precise.
Last edited on Jun 2, 2011 at 4:35pm UTC
Jun 3, 2011 at 10:43am UTC
Thanks johnnystarr for your reply
Last edited on Jun 3, 2011 at 10:44am UTC