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 * ----------------------------------------------------------------------------
18 static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
19 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
22 to64(char *s, unsigned int v, int n)
26 *s++ = itoa64[v&0x3f];
32 libshadow_md5_crypt(const char *pw, const char *salt)
34 static char *magic = "$1$"; /*
35 * This string is magic for
36 * this algorithm. Having
37 * it this way, we can get
40 static char passwd[120], *p;
41 static const char *sp,*ep;
42 unsigned char final[16];
44 struct MD5Context ctx,ctx1;
47 /* Refine the Salt first */
50 /* If it starts with the magic string, then skip that */
51 if(!strncmp(sp,magic,strlen(magic)))
54 /* It stops at the first '$', max 8 chars */
55 for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++)
58 /* get the length of the true salt */
63 /* The password first, since that is what is most unknown */
64 MD5Update(&ctx,pw,strlen(pw));
66 /* Then our magic string */
67 MD5Update(&ctx,magic,strlen(magic));
69 /* Then the raw salt */
70 MD5Update(&ctx,sp,sl);
72 /* Then just as many characters of the MD5(pw,salt,pw) */
74 MD5Update(&ctx1,pw,strlen(pw));
75 MD5Update(&ctx1,sp,sl);
76 MD5Update(&ctx1,pw,strlen(pw));
77 MD5Final(final,&ctx1);
78 for(pl = strlen(pw); pl > 0; pl -= 16)
79 MD5Update(&ctx,final,pl>16 ? 16 : pl);
81 /* Don't leave anything around in vm they could use. */
82 memset(final,0,sizeof final);
84 /* Then something really weird... */
85 for (j=0,i = strlen(pw); i ; i >>= 1)
87 MD5Update(&ctx, final+j, 1);
89 MD5Update(&ctx, pw+j, 1);
91 /* Now make the output string */
93 strncat(passwd,sp,sl);
99 * and now, just to make sure things don't run too fast
100 * On a 60 Mhz Pentium this takes 34 msec, so you would
101 * need 30 seconds to build a 1000 entry dictionary...
103 for(i=0;i<1000;i++) {
106 MD5Update(&ctx1,pw,strlen(pw));
108 MD5Update(&ctx1,final,16);
111 MD5Update(&ctx1,sp,sl);
114 MD5Update(&ctx1,pw,strlen(pw));
117 MD5Update(&ctx1,final,16);
119 MD5Update(&ctx1,pw,strlen(pw));
120 MD5Final(final,&ctx1);
123 p = passwd + strlen(passwd);
125 l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4;
126 l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4;
127 l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4;
128 l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4;
129 l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4;
130 l = final[11] ; to64(p,l,2); p += 2;
133 /* Don't leave anything around in vm they could use. */
134 memset(final,0,sizeof final);
141 char pass[256], salt[10], rand[8];
142 int fd = open("/dev/urandom", O_RDONLY);
146 fprintf(stderr, "unable to open /dev/urandom: %m\n");
149 while (fgets(pass, sizeof(pass), stdin))
151 char *c = strchr(pass, '\n');
154 if (read(fd, rand, sizeof(rand)) != sizeof(rand))
156 fprintf(stderr, "Error reading /dev/urandom: %m\n");
160 to64(salt+4*n, *(uint32 *)(rand+4*n), 4);
162 printf("%s\n", libshadow_md5_crypt(pass, salt));