]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | #include <stdio.h> | |
3 | #include <fcntl.h> | |
4 | #include <string.h> | |
5 | #include <stdlib.h> | |
6 | #include <sys/time.h> | |
7 | #include <time.h> | |
8 | #include <sys/types.h> | |
9 | ||
10 | #define _XOPEN_SOURCE | |
11 | #define MD5_CRYPT_ENAB yes | |
12 | #include <unistd.h> | |
13 | ||
14 | ||
15 | extern char *crypt (__const char *__key, __const char *__salt); | |
16 | ||
17 | char *crypt_make_salt (void) | |
18 | { | |
19 | struct timeval tv; | |
20 | static char result[40]; | |
21 | ||
22 | result[0] = '\0'; | |
23 | strcpy (result, "$1$"); /* magic for the new MD5 crypt() */ | |
24 | ||
25 | gettimeofday (&tv, (struct timezone *) 0); | |
26 | strcat (result, l64a (tv.tv_usec)); | |
27 | strcat (result, l64a (tv.tv_sec + getpid () + clock ())); | |
28 | ||
29 | if (strlen (result) > 3 + 8) result[11] = '\0'; | |
30 | ||
31 | return result; | |
32 | } | |
33 | ||
34 | char *pw_encrypt (const char *clear, const char *salt) | |
35 | { | |
36 | static char cipher[128]; | |
37 | char *cp = crypt (clear, salt); | |
38 | strcpy (cipher, cp); | |
39 | return cipher; | |
40 | } | |
41 | ||
42 | ||
43 | int main () | |
44 | { | |
45 | /* for new password, we generate salt | |
46 | * for check we use encrypted password as salt | |
47 | * char *crpasswd_or_newsalt = crypt_make_salt(); | |
48 | */ | |
49 | ||
50 | const char* msg = "Enter password:"; | |
51 | ||
52 | char *clear = NULL; | |
53 | // clear = "enterclearpasswordhere"; | |
54 | // or simply get it | |
55 | if ( !(clear = getpass(msg)) || strlen(clear) == 0 ) | |
56 | { | |
57 | fprintf (stderr, ("You entered no password \n")); | |
58 | return 1; | |
59 | } | |
60 | else | |
61 | { | |
62 | char *crpasswd_or_newsalt = "$1$RG.pRvZh$Q0WZ8clsqtMUBRLFckoQg1"; | |
63 | char *cipher = pw_encrypt (clear, crpasswd_or_newsalt); | |
64 | ||
65 | if (strcmp (cipher, crpasswd_or_newsalt) != 0) | |
66 | { | |
67 | fprintf (stderr, (crpasswd_or_newsalt)); | |
68 | fprintf (stderr, ("\n")); | |
69 | fprintf (stderr, ("Incorrect password. Result is:\n")); | |
70 | fprintf (stderr, (cipher)); | |
71 | fprintf (stderr, ("\n")); | |
72 | return 1; | |
73 | } | |
74 | else | |
75 | { | |
76 | fprintf (stderr, ("\n")); | |
77 | fprintf (stderr, (crpasswd_or_newsalt)); | |
78 | fprintf (stderr, ("\n")); | |
79 | fprintf (stderr, ("Good password\n")); | |
80 | fprintf (stderr, (cipher)); | |
81 | fprintf (stderr, ("\n")); | |
82 | return 0; | |
83 | } | |
84 | } | |
85 | } | |
86 |