4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
13 ** This file contains code to implement an MD5 extension to TCL.
19 #include "tclsqlite.h"
22 * This code implements the MD5 message-digest algorithm.
23 * The algorithm is due to Ron Rivest. This code was
24 * written by Colin Plumb in 1993, no copyright is claimed.
25 * This code is in the public domain; do with it what you wish.
27 * Equivalent code is available from RSA Data Security, Inc.
28 * This code has been tested against that, and is equivalent,
29 * except that you don't need to include two pages of legalese
32 * To compute the message digest of a chunk of bytes, declare an
33 * MD5Context structure, pass it to MD5Init, call MD5Update as
34 * needed on buffers full of bytes, and then call MD5Final, which
35 * will fill a supplied 16-byte array with the digest.
39 * If compiled on a machine that doesn't have a 32-bit integer,
40 * you just set "uint32" to the appropriate datatype for an
41 * unsigned 32-bit integer. For example:
43 * cc -Duint32='unsigned long' md5.c
47 # define uint32 unsigned int
56 typedef struct MD5Context MD5Context
;
59 * Note: this code is harmless on little-endian machines.
61 static void byteReverse (unsigned char *buf
, unsigned longs
){
64 t
= (uint32
)((unsigned)buf
[3]<<8 | buf
[2]) << 16 |
65 ((unsigned)buf
[1]<<8 | buf
[0]);
70 /* The four core functions - F1 is optimized somewhat */
72 /* #define F1(x, y, z) (x & y | ~x & z) */
73 #define F1(x, y, z) (z ^ (x & (y ^ z)))
74 #define F2(x, y, z) F1(z, x, y)
75 #define F3(x, y, z) (x ^ y ^ z)
76 #define F4(x, y, z) (y ^ (x | ~z))
78 /* This is the central step in the MD5 algorithm. */
79 #define MD5STEP(f, w, x, y, z, data, s) \
80 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
83 * The core of the MD5 algorithm, this alters an existing MD5 hash to
84 * reflect the addition of 16 longwords of new data. MD5Update blocks
85 * the data and converts bytes into longwords for this routine.
87 static void MD5Transform(uint32 buf
[4], const uint32 in
[16]){
88 register uint32 a
, b
, c
, d
;
95 MD5STEP(F1
, a
, b
, c
, d
, in
[ 0]+0xd76aa478, 7);
96 MD5STEP(F1
, d
, a
, b
, c
, in
[ 1]+0xe8c7b756, 12);
97 MD5STEP(F1
, c
, d
, a
, b
, in
[ 2]+0x242070db, 17);
98 MD5STEP(F1
, b
, c
, d
, a
, in
[ 3]+0xc1bdceee, 22);
99 MD5STEP(F1
, a
, b
, c
, d
, in
[ 4]+0xf57c0faf, 7);
100 MD5STEP(F1
, d
, a
, b
, c
, in
[ 5]+0x4787c62a, 12);
101 MD5STEP(F1
, c
, d
, a
, b
, in
[ 6]+0xa8304613, 17);
102 MD5STEP(F1
, b
, c
, d
, a
, in
[ 7]+0xfd469501, 22);
103 MD5STEP(F1
, a
, b
, c
, d
, in
[ 8]+0x698098d8, 7);
104 MD5STEP(F1
, d
, a
, b
, c
, in
[ 9]+0x8b44f7af, 12);
105 MD5STEP(F1
, c
, d
, a
, b
, in
[10]+0xffff5bb1, 17);
106 MD5STEP(F1
, b
, c
, d
, a
, in
[11]+0x895cd7be, 22);
107 MD5STEP(F1
, a
, b
, c
, d
, in
[12]+0x6b901122, 7);
108 MD5STEP(F1
, d
, a
, b
, c
, in
[13]+0xfd987193, 12);
109 MD5STEP(F1
, c
, d
, a
, b
, in
[14]+0xa679438e, 17);
110 MD5STEP(F1
, b
, c
, d
, a
, in
[15]+0x49b40821, 22);
112 MD5STEP(F2
, a
, b
, c
, d
, in
[ 1]+0xf61e2562, 5);
113 MD5STEP(F2
, d
, a
, b
, c
, in
[ 6]+0xc040b340, 9);
114 MD5STEP(F2
, c
, d
, a
, b
, in
[11]+0x265e5a51, 14);
115 MD5STEP(F2
, b
, c
, d
, a
, in
[ 0]+0xe9b6c7aa, 20);
116 MD5STEP(F2
, a
, b
, c
, d
, in
[ 5]+0xd62f105d, 5);
117 MD5STEP(F2
, d
, a
, b
, c
, in
[10]+0x02441453, 9);
118 MD5STEP(F2
, c
, d
, a
, b
, in
[15]+0xd8a1e681, 14);
119 MD5STEP(F2
, b
, c
, d
, a
, in
[ 4]+0xe7d3fbc8, 20);
120 MD5STEP(F2
, a
, b
, c
, d
, in
[ 9]+0x21e1cde6, 5);
121 MD5STEP(F2
, d
, a
, b
, c
, in
[14]+0xc33707d6, 9);
122 MD5STEP(F2
, c
, d
, a
, b
, in
[ 3]+0xf4d50d87, 14);
123 MD5STEP(F2
, b
, c
, d
, a
, in
[ 8]+0x455a14ed, 20);
124 MD5STEP(F2
, a
, b
, c
, d
, in
[13]+0xa9e3e905, 5);
125 MD5STEP(F2
, d
, a
, b
, c
, in
[ 2]+0xfcefa3f8, 9);
126 MD5STEP(F2
, c
, d
, a
, b
, in
[ 7]+0x676f02d9, 14);
127 MD5STEP(F2
, b
, c
, d
, a
, in
[12]+0x8d2a4c8a, 20);
129 MD5STEP(F3
, a
, b
, c
, d
, in
[ 5]+0xfffa3942, 4);
130 MD5STEP(F3
, d
, a
, b
, c
, in
[ 8]+0x8771f681, 11);
131 MD5STEP(F3
, c
, d
, a
, b
, in
[11]+0x6d9d6122, 16);
132 MD5STEP(F3
, b
, c
, d
, a
, in
[14]+0xfde5380c, 23);
133 MD5STEP(F3
, a
, b
, c
, d
, in
[ 1]+0xa4beea44, 4);
134 MD5STEP(F3
, d
, a
, b
, c
, in
[ 4]+0x4bdecfa9, 11);
135 MD5STEP(F3
, c
, d
, a
, b
, in
[ 7]+0xf6bb4b60, 16);
136 MD5STEP(F3
, b
, c
, d
, a
, in
[10]+0xbebfbc70, 23);
137 MD5STEP(F3
, a
, b
, c
, d
, in
[13]+0x289b7ec6, 4);
138 MD5STEP(F3
, d
, a
, b
, c
, in
[ 0]+0xeaa127fa, 11);
139 MD5STEP(F3
, c
, d
, a
, b
, in
[ 3]+0xd4ef3085, 16);
140 MD5STEP(F3
, b
, c
, d
, a
, in
[ 6]+0x04881d05, 23);
141 MD5STEP(F3
, a
, b
, c
, d
, in
[ 9]+0xd9d4d039, 4);
142 MD5STEP(F3
, d
, a
, b
, c
, in
[12]+0xe6db99e5, 11);
143 MD5STEP(F3
, c
, d
, a
, b
, in
[15]+0x1fa27cf8, 16);
144 MD5STEP(F3
, b
, c
, d
, a
, in
[ 2]+0xc4ac5665, 23);
146 MD5STEP(F4
, a
, b
, c
, d
, in
[ 0]+0xf4292244, 6);
147 MD5STEP(F4
, d
, a
, b
, c
, in
[ 7]+0x432aff97, 10);
148 MD5STEP(F4
, c
, d
, a
, b
, in
[14]+0xab9423a7, 15);
149 MD5STEP(F4
, b
, c
, d
, a
, in
[ 5]+0xfc93a039, 21);
150 MD5STEP(F4
, a
, b
, c
, d
, in
[12]+0x655b59c3, 6);
151 MD5STEP(F4
, d
, a
, b
, c
, in
[ 3]+0x8f0ccc92, 10);
152 MD5STEP(F4
, c
, d
, a
, b
, in
[10]+0xffeff47d, 15);
153 MD5STEP(F4
, b
, c
, d
, a
, in
[ 1]+0x85845dd1, 21);
154 MD5STEP(F4
, a
, b
, c
, d
, in
[ 8]+0x6fa87e4f, 6);
155 MD5STEP(F4
, d
, a
, b
, c
, in
[15]+0xfe2ce6e0, 10);
156 MD5STEP(F4
, c
, d
, a
, b
, in
[ 6]+0xa3014314, 15);
157 MD5STEP(F4
, b
, c
, d
, a
, in
[13]+0x4e0811a1, 21);
158 MD5STEP(F4
, a
, b
, c
, d
, in
[ 4]+0xf7537e82, 6);
159 MD5STEP(F4
, d
, a
, b
, c
, in
[11]+0xbd3af235, 10);
160 MD5STEP(F4
, c
, d
, a
, b
, in
[ 2]+0x2ad7d2bb, 15);
161 MD5STEP(F4
, b
, c
, d
, a
, in
[ 9]+0xeb86d391, 21);
170 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
171 * initialization constants.
173 static void MD5Init(MD5Context
*ctx
){
175 ctx
->buf
[0] = 0x67452301;
176 ctx
->buf
[1] = 0xefcdab89;
177 ctx
->buf
[2] = 0x98badcfe;
178 ctx
->buf
[3] = 0x10325476;
184 * Update context to reflect the concatenation of another buffer full
188 void MD5Update(MD5Context
*ctx
, const unsigned char *buf
, unsigned int len
){
191 /* Update bitcount */
194 if ((ctx
->bits
[0] = t
+ ((uint32
)len
<< 3)) < t
)
195 ctx
->bits
[1]++; /* Carry from low to high */
196 ctx
->bits
[1] += len
>> 29;
198 t
= (t
>> 3) & 0x3f; /* Bytes already in shsInfo->data */
200 /* Handle any leading odd-sized chunks */
203 unsigned char *p
= (unsigned char *)ctx
->in
+ t
;
211 byteReverse(ctx
->in
, 16);
212 MD5Transform(ctx
->buf
, (uint32
*)ctx
->in
);
217 /* Process data in 64-byte chunks */
220 memcpy(ctx
->in
, buf
, 64);
221 byteReverse(ctx
->in
, 16);
222 MD5Transform(ctx
->buf
, (uint32
*)ctx
->in
);
227 /* Handle any remaining bytes of data. */
229 memcpy(ctx
->in
, buf
, len
);
233 * Final wrapup - pad to 64-byte boundary with the bit pattern
234 * 1 0* (64-bit count of bits processed, MSB-first)
236 static void MD5Final(unsigned char digest
[16], MD5Context
*ctx
){
240 /* Compute number of bytes mod 64 */
241 count
= (ctx
->bits
[0] >> 3) & 0x3F;
243 /* Set the first char of padding to 0x80. This is safe since there is
244 always at least one byte free */
248 /* Bytes of padding needed to make 64 bytes */
249 count
= 64 - 1 - count
;
251 /* Pad out to 56 mod 64 */
253 /* Two lots of padding: Pad the first block to 64 bytes */
255 byteReverse(ctx
->in
, 16);
256 MD5Transform(ctx
->buf
, (uint32
*)ctx
->in
);
258 /* Now fill the next block with 56 bytes */
259 memset(ctx
->in
, 0, 56);
261 /* Pad block to 56 bytes */
262 memset(p
, 0, count
-8);
264 byteReverse(ctx
->in
, 14);
266 /* Append length in bits and transform */
267 memcpy(ctx
->in
+ 14*4, ctx
->bits
, 8);
269 MD5Transform(ctx
->buf
, (uint32
*)ctx
->in
);
270 byteReverse((unsigned char *)ctx
->buf
, 4);
271 memcpy(digest
, ctx
->buf
, 16);
275 ** Convert a 128-bit MD5 digest into a 32-digit base-16 number.
277 static void MD5DigestToBase16(unsigned char *digest
, char *zBuf
){
278 static char const zEncode
[] = "0123456789abcdef";
281 for(j
=i
=0; i
<16; i
++){
283 zBuf
[j
++] = zEncode
[(a
>>4)&0xf];
284 zBuf
[j
++] = zEncode
[a
& 0xf];
291 ** Convert a 128-bit MD5 digest into sequences of eight 5-digit integers
292 ** each representing 16 bits of the digest and separated from each
293 ** other by a "-" character.
295 static void MD5DigestToBase10x8(unsigned char digest
[16], char zDigest
[50]){
298 for(i
=j
=0; i
<16; i
+=2){
299 x
= digest
[i
]*256 + digest
[i
+1];
300 if( i
>0 ) zDigest
[j
++] = '-';
301 sqlite3_snprintf(50-j
, &zDigest
[j
], "%05u", x
);
308 ** A TCL command for md5. The argument is the text to be hashed. The
309 ** Result is the hash in base64.
311 static int SQLITE_TCLAPI
md5_cmd(
318 unsigned char digest
[16];
320 void (*converter
)(unsigned char*, char*);
323 Tcl_AppendResult(interp
,"wrong # args: should be \"", argv
[0],
324 " TEXT\"", (char*)0);
328 MD5Update(&ctx
, (unsigned char*)argv
[1], (unsigned)strlen(argv
[1]));
329 MD5Final(digest
, &ctx
);
330 converter
= (void(*)(unsigned char*,char*))cd
;
331 converter(digest
, zBuf
);
332 Tcl_AppendResult(interp
, zBuf
, (char*)0);
337 ** A TCL command to take the md5 hash of a file. The argument is the
340 static int SQLITE_TCLAPI
md5file_cmd(
350 void (*converter
)(unsigned char*, char*);
351 unsigned char digest
[16];
354 if( argc
!=2 && argc
!=4 ){
355 Tcl_AppendResult(interp
,"wrong # args: should be \"", argv
[0],
356 " FILENAME [OFFSET AMT]\"", (char*)0);
360 ofst
= atoi(argv
[2]);
366 in
= fopen(argv
[1],"rb");
368 Tcl_AppendResult(interp
,"unable to open file \"", argv
[1],
369 "\" for reading", (char*)0);
372 fseek(in
, ofst
, SEEK_SET
);
376 n
= (int)fread(zBuf
, 1, sizeof(zBuf
)<=amt
? sizeof(zBuf
) : amt
, in
);
378 MD5Update(&ctx
, (unsigned char*)zBuf
, (unsigned)n
);
382 MD5Final(digest
, &ctx
);
383 converter
= (void(*)(unsigned char*,char*))cd
;
384 converter(digest
, zBuf
);
385 Tcl_AppendResult(interp
, zBuf
, (char*)0);
390 ** Register the four new TCL commands for generating MD5 checksums
391 ** with the TCL interpreter.
393 int Md5_Init(Tcl_Interp
*interp
){
394 Tcl_CreateCommand(interp
, "md5", (Tcl_CmdProc
*)md5_cmd
,
395 MD5DigestToBase16
, 0);
396 Tcl_CreateCommand(interp
, "md5-10x8", (Tcl_CmdProc
*)md5_cmd
,
397 MD5DigestToBase10x8
, 0);
398 Tcl_CreateCommand(interp
, "md5file", (Tcl_CmdProc
*)md5file_cmd
,
399 MD5DigestToBase16
, 0);
400 Tcl_CreateCommand(interp
, "md5file-10x8", (Tcl_CmdProc
*)md5file_cmd
,
401 MD5DigestToBase10x8
, 0);
406 ** During testing, the special md5sum() aggregate function is available.
407 ** inside SQLite. The following routines implement that function.
409 static void md5step(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
){
413 p
= sqlite3_aggregate_context(context
, sizeof(*p
));
418 for(i
=0; i
<argc
; i
++){
419 const char *zData
= (char*)sqlite3_value_text(argv
[i
]);
421 MD5Update(p
, (unsigned char*)zData
, (int)strlen(zData
));
425 static void md5finalize(sqlite3_context
*context
){
427 unsigned char digest
[16];
429 p
= sqlite3_aggregate_context(context
, sizeof(*p
));
431 MD5DigestToBase16(digest
, zBuf
);
432 sqlite3_result_text(context
, zBuf
, -1, SQLITE_TRANSIENT
);
437 const sqlite3_api_routines
*pThunk
439 int rc
= sqlite3_create_function(db
, "md5sum", -1, SQLITE_UTF8
, 0, 0,
440 md5step
, md5finalize
);
441 sqlite3_overload_function(db
, "md5sum", -1); /* To exercise this API */