]>
git.ipfire.org Git - thirdparty/squid.git/blob - helpers/basic_auth/NCSA/crypt_md5.cc
2 * Shamelessly stolen from linux-pam, and adopted to work with
3 * OpenSSL md5 implementation and any magic string
5 * Origin2: md5_crypt.c,v 1.1.1.1 2000/01/03 17:34:46 gafton Exp
7 * ----------------------------------------------------------------------------
8 * "THE BEER-WARE LICENSE" (Revision 42):
9 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
10 * can do whatever you want with this stuff. If we meet some day, and you think
11 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
12 * ----------------------------------------------------------------------------
14 * Origin: Id: crypt.c,v 1.3 1995/05/30 05:42:22 rgrimes Exp
27 #include "crypt_md5.h"
29 static unsigned char itoa64
[] = /* 0 ... 63 => ascii - 64 */
30 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
32 static void md5to64(char *s
, unsigned long v
, int n
)
35 *s
++ = itoa64
[v
& 0x3f];
43 * Use MD5 for what it is best at...
45 * If salt begins with $ then it is assumed to be on the form
47 * If not the normal UNIX magic $1$ is used.
50 char *crypt_md5(const char *pw
, const char *salt
)
52 const char *magic
= "$1$";
54 static char passwd
[120], *p
;
55 static const char *sp
, *ep
;
56 unsigned char final
[16];
58 SquidMD5_CTX ctx
, ctx1
;
63 while (*salt
&& *salt
!= '$')
67 magiclen
= salt
- magic
;
74 /* Refine the Salt first */
77 /* It stops at the first '$', max 8 chars */
78 for (ep
= sp
; *ep
&& *ep
!= '$' && ep
< (sp
+ 8); ep
++)
81 /* get the length of the true salt */
86 /* The password first, since that is what is most unknown */
87 SquidMD5Update(&ctx
, (unsigned const char *) pw
, strlen(pw
));
89 /* Then our magic string */
90 SquidMD5Update(&ctx
, (unsigned const char *) magic
, magiclen
);
92 /* Then the raw salt */
93 SquidMD5Update(&ctx
, (unsigned const char *) sp
, sl
);
95 /* Then just as many characters of the MD5(pw,salt,pw) */
97 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
98 SquidMD5Update(&ctx1
, (unsigned const char *) sp
, sl
);
99 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
100 SquidMD5Final(final
, &ctx1
);
101 for (pl
= strlen(pw
); pl
> 0; pl
-= 16)
102 SquidMD5Update(&ctx
, (unsigned const char *) final
, pl
> 16 ? 16 : pl
);
104 /* Don't leave anything around in vm they could use. */
105 memset(final
, 0, sizeof final
);
107 /* Then something really weird... */
108 for (j
= 0, i
= strlen(pw
); i
; i
>>= 1)
110 SquidMD5Update(&ctx
, (unsigned const char *) final
+ j
, 1);
112 SquidMD5Update(&ctx
, (unsigned const char *) pw
+ j
, 1);
114 /* Now make the output string */
115 memset(passwd
, 0, sizeof(passwd
));
116 strncat(passwd
, magic
, magiclen
);
117 strncat(passwd
, sp
, sl
);
120 SquidMD5Final(final
, &ctx
);
123 * and now, just to make sure things don't run too fast
124 * On a 60 Mhz Pentium this takes 34 msec, so you would
125 * need 30 seconds to build a 1000 entry dictionary...
127 for (i
= 0; i
< 1000; i
++) {
130 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
132 SquidMD5Update(&ctx1
, (unsigned const char *) final
, 16);
135 SquidMD5Update(&ctx1
, (unsigned const char *) sp
, sl
);
138 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
141 SquidMD5Update(&ctx1
, (unsigned const char *) final
, 16);
143 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
144 SquidMD5Final(final
, &ctx1
);
147 p
= passwd
+ strlen(passwd
);
149 l
= (final
[0] << 16) | (final
[6] << 8) | final
[12];
152 l
= (final
[1] << 16) | (final
[7] << 8) | final
[13];
155 l
= (final
[2] << 16) | (final
[8] << 8) | final
[14];
158 l
= (final
[3] << 16) | (final
[9] << 8) | final
[15];
161 l
= (final
[4] << 16) | (final
[10] << 8) | final
[5];
169 /* Don't leave anything around in vm they could use. */
170 memset(final
, 0, sizeof final
);
175 /* Created by Ramon de Carvalho <ramondecarvalho@yahoo.com.br>
176 Refined by Rodrigo Rubira Branco <rodrigo@kernelhacking.com>
178 char *md5sum(const char *s
)
180 static unsigned char digest
[16];
188 SquidMD5Update(&ctx
,(const unsigned char *)s
,strlen(s
));
189 SquidMD5Final(digest
,&ctx
);
191 for (idx
=0; idx
<16; idx
++)
192 snprintf(&sum
[idx
*2],(33-(idx
*2)),"%02x",digest
[idx
]);
196 /* Don't leave anything around in vm they could use. */
197 memset(digest
, 0, sizeof digest
);