2 * md5crypt based on lib/md5crypt.c from Linux shadow package.
3 * adapted by Martin Mares <mj@ucw.cz> in June 2004
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
20 static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
21 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
24 to64(char *s, unsigned int v, int n)
28 *s++ = itoa64[v&0x3f];
34 libshadow_md5_crypt(const char *pw, const char *salt)
36 static char *magic = "$1$"; /*
37 * This string is magic for
38 * this algorithm. Having
39 * it this way, we can get
42 static char passwd[120], *p;
43 static const char *sp,*ep;
44 unsigned char final[16];
46 struct MD5Context ctx,ctx1;
49 /* Refine the Salt first */
52 /* If it starts with the magic string, then skip that */
53 if(!strncmp(sp,magic,strlen(magic)))
56 /* It stops at the first '$', max 8 chars */
57 for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++)
60 /* get the length of the true salt */
65 /* The password first, since that is what is most unknown */
66 MD5Update(&ctx,pw,strlen(pw));
68 /* Then our magic string */
69 MD5Update(&ctx,magic,strlen(magic));
71 /* Then the raw salt */
72 MD5Update(&ctx,sp,sl);
74 /* Then just as many characters of the MD5(pw,salt,pw) */
76 MD5Update(&ctx1,pw,strlen(pw));
77 MD5Update(&ctx1,sp,sl);
78 MD5Update(&ctx1,pw,strlen(pw));
79 MD5Final(final,&ctx1);
80 for(pl = strlen(pw); pl > 0; pl -= 16)
81 MD5Update(&ctx,final,pl>16 ? 16 : pl);
83 /* Don't leave anything around in vm they could use. */
84 memset(final,0,sizeof final);
86 /* Then something really weird... */
87 for (j=0,i = strlen(pw); i ; i >>= 1)
89 MD5Update(&ctx, final+j, 1);
91 MD5Update(&ctx, pw+j, 1);
93 /* Now make the output string */
95 strncat(passwd,sp,sl);
101 * and now, just to make sure things don't run too fast
102 * On a 60 Mhz Pentium this takes 34 msec, so you would
103 * need 30 seconds to build a 1000 entry dictionary...
105 for(i=0;i<1000;i++) {
108 MD5Update(&ctx1,pw,strlen(pw));
110 MD5Update(&ctx1,final,16);
113 MD5Update(&ctx1,sp,sl);
116 MD5Update(&ctx1,pw,strlen(pw));
119 MD5Update(&ctx1,final,16);
121 MD5Update(&ctx1,pw,strlen(pw));
122 MD5Final(final,&ctx1);
125 p = passwd + strlen(passwd);
127 l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4;
128 l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4;
129 l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4;
130 l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4;
131 l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4;
132 l = final[11] ; to64(p,l,2); p += 2;
135 /* Don't leave anything around in vm they could use. */
136 memset(final,0,sizeof final);
143 char pass[256], salt[10], rand[8];
144 int fd = open("/dev/urandom", O_RDONLY);
148 fprintf(stderr, "unable to open /dev/urandom: %m\n");
151 while (fgets(pass, sizeof(pass), stdin))
153 char *c = strchr(pass, '\n');
156 if (read(fd, rand, sizeof(rand)) != sizeof(rand))
158 fprintf(stderr, "Error reading /dev/urandom: %m\n");
162 to64(salt+4*n, *(uint32 *)(rand+4*n), 4);
164 printf("%s\n", libshadow_md5_crypt(pass, salt));