]>
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];
44 * Use MD5 for what it is best at...
46 * If salt begins with $ then it is assumed to be on the form
48 * If not the normal UNIX magic $1$ is used.
51 char *crypt_md5(const char *pw
, const char *salt
)
53 const char *magic
= "$1$";
55 static char passwd
[120], *p
;
56 static const char *sp
, *ep
;
57 unsigned char final
[16];
59 SquidMD5_CTX ctx
, ctx1
;
65 while (*salt
&& *salt
!= '$')
69 magiclen
= salt
- magic
;
76 /* Refine the Salt first */
79 /* It stops at the first '$', max 8 chars */
80 for (ep
= sp
; *ep
&& *ep
!= '$' && ep
< (sp
+ 8); ++ep
)
83 /* get the length of the true salt */
88 /* The password first, since that is what is most unknown */
89 SquidMD5Update(&ctx
, (unsigned const char *) pw
, strlen(pw
));
91 /* Then our magic string */
92 SquidMD5Update(&ctx
, (unsigned const char *) magic
, magiclen
);
94 /* Then the raw salt */
95 SquidMD5Update(&ctx
, (unsigned const char *) sp
, sl
);
97 /* Then just as many characters of the MD5(pw,salt,pw) */
99 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
100 SquidMD5Update(&ctx1
, (unsigned const char *) sp
, sl
);
101 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
102 SquidMD5Final(final
, &ctx1
);
103 for (pl
= strlen(pw
); pl
> 0; pl
-= 16)
104 SquidMD5Update(&ctx
, (unsigned const char *) final
, pl
> 16 ? 16 : pl
);
106 /* Don't leave anything around in vm they could use. */
107 memset(final
, 0, sizeof final
);
109 /* Then something really weird... */
110 for (j
= 0, i
= strlen(pw
); i
; i
>>= 1)
112 SquidMD5Update(&ctx
, (unsigned const char *) final
+ j
, 1);
114 SquidMD5Update(&ctx
, (unsigned const char *) pw
+ j
, 1);
116 /* Now make the output string */
117 memset(passwd
, 0, sizeof(passwd
));
118 strncat(passwd
, magic
, magiclen
);
119 strncat(passwd
, sp
, sl
);
122 SquidMD5Final(final
, &ctx
);
125 * and now, just to make sure things don't run too fast
126 * On a 60 Mhz Pentium this takes 34 msec, so you would
127 * need 30 seconds to build a 1000 entry dictionary...
129 for (i
= 0; i
< 1000; ++i
) {
132 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
134 SquidMD5Update(&ctx1
, (unsigned const char *) final
, 16);
137 SquidMD5Update(&ctx1
, (unsigned const char *) sp
, sl
);
140 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
143 SquidMD5Update(&ctx1
, (unsigned const char *) final
, 16);
145 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
146 SquidMD5Final(final
, &ctx1
);
149 p
= passwd
+ strlen(passwd
);
151 l
= (final
[0] << 16) | (final
[6] << 8) | final
[12];
154 l
= (final
[1] << 16) | (final
[7] << 8) | final
[13];
157 l
= (final
[2] << 16) | (final
[8] << 8) | final
[14];
160 l
= (final
[3] << 16) | (final
[9] << 8) | final
[15];
163 l
= (final
[4] << 16) | (final
[10] << 8) | final
[5];
171 /* Don't leave anything around in vm they could use. */
172 memset(final
, 0, sizeof final
);
177 /* Created by Ramon de Carvalho <ramondecarvalho@yahoo.com.br>
178 Refined by Rodrigo Rubira Branco <rodrigo@kernelhacking.com>
180 char *md5sum(const char *s
)
182 static unsigned char digest
[16];
190 SquidMD5Update(&ctx
,(const unsigned char *)s
,strlen(s
));
191 SquidMD5Final(digest
,&ctx
);
193 for (idx
=0; idx
<16; ++idx
)
194 snprintf(&sum
[idx
*2],(33-(idx
*2)),"%02x",digest
[idx
]);
198 /* Don't leave anything around in vm they could use. */
199 memset(digest
, 0, sizeof digest
);