md5.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
#pragma ident "%Z%%M% %I% %E% SMI"
/*
** SQLite uses this code for testing only. It is not a part of
** the SQLite library. This file implements two new TCL commands
** "md5" and "md5file" that compute md5 checksums on arbitrary text
** and on complete files. These commands are used by the "testfixture"
** program to help verify the correct operation of the SQLite library.
**
** The original use of these TCL commands was to test the ROLLBACK
** feature of SQLite. First compute the MD5-checksum of the database.
** Then make some changes but rollback the changes rather than commit
** them. Compute a second MD5-checksum of the file and verify that the
** two checksums are the same. Such is the original use of this code.
** New uses may have been added since this comment was written.
*/
/*
* This code implements the MD5 message-digest algorithm.
* The algorithm is due to Ron Rivest. This code was
* written by Colin Plumb in 1993, no copyright is claimed.
* This code is in the public domain; do with it what you wish.
*
* Equivalent code is available from RSA Data Security, Inc.
* This code has been tested against that, and is equivalent,
* except that you don't need to include two pages of legalese
* with every copy.
*
* To compute the message digest of a chunk of bytes, declare an
* MD5Context structure, pass it to MD5Init, call MD5Update as
* needed on buffers full of bytes, and then call MD5Final, which
* will fill a supplied 16-byte array with the digest.
*/
#include <tcl.h>
#include <string.h>
#include "sqlite.h"
/*
* If compiled on a machine that doesn't have a 32-bit integer,
* you just set "uint32" to the appropriate datatype for an
* unsigned 32-bit integer. For example:
*
* cc -Duint32='unsigned long' md5.c
*
*/
#ifndef uint32
# define uint32 unsigned int
#endif
struct Context {
unsigned char in[64];
};
typedef char MD5Context[88];
/*
* Note: this code is harmless on little-endian machines.
*/
uint32 t;
do {
buf += 4;
} while (--longs);
}
/* The four core functions - F1 is optimized somewhat */
/* #define F1(x, y, z) (x & y | ~x & z) */
#define F1(x, y, z) (z ^ (x & (y ^ z)))
#define F3(x, y, z) (x ^ y ^ z)
#define F4(x, y, z) (y ^ (x | ~z))
/* This is the central step in the MD5 algorithm. */
( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
/*
* The core of the MD5 algorithm, this alters an existing MD5 hash to
* reflect the addition of 16 longwords of new data. MD5Update blocks
* the data and converts bytes into longwords for this routine.
*/
register uint32 a, b, c, d;
a = buf[0];
b = buf[1];
c = buf[2];
d = buf[3];
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
}
/*
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
* initialization constants.
*/
}
/*
* Update context to reflect the concatenation of another buffer full
* of bytes.
*/
static
uint32 t;
/* Update bitcount */
t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
/* Handle any leading odd-sized chunks */
if ( t ) {
t = 64-t;
if (len < t) {
return;
}
buf += t;
len -= t;
}
/* Process data in 64-byte chunks */
while (len >= 64) {
buf += 64;
len -= 64;
}
/* Handle any remaining bytes of data. */
}
/*
* Final wrapup - pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
unsigned count;
unsigned char *p;
/* Compute number of bytes mod 64 */
/* Set the first char of padding to 0x80. This is safe since there is
always at least one byte free */
*p++ = 0x80;
/* Bytes of padding needed to make 64 bytes */
/* Pad out to 56 mod 64 */
if (count < 8) {
/* Two lots of padding: Pad the first block to 64 bytes */
/* Now fill the next block with 56 bytes */
} else {
/* Pad block to 56 bytes */
}
/* Append length in bits and transform */
}
/*
** Convert a digest into base-16. digest should be declared as
** "unsigned char digest[16]" in the calling function. The MD5
** digest is stored in the first 16 bytes. zBuf should
** be "char zBuf[33]".
*/
static char const zEncode[] = "0123456789abcdef";
int i, j;
for(j=i=0; i<16; i++){
int a = digest[i];
}
zBuf[j] = 0;
}
/*
** A TCL command for md5. The argument is the text to be hashed. The
** Result is the hash in base64.
*/
unsigned char digest[16];
if( argc!=2 ){
" TEXT\"", 0);
return TCL_ERROR;
}
return TCL_OK;
}
/*
** A TCL command to take the md5 hash of a file. The argument is the
** name of the file.
*/
unsigned char digest[16];
char zBuf[10240];
if( argc!=2 ){
" FILENAME\"", 0);
return TCL_ERROR;
}
if( in==0 ){
"\" for reading", 0);
return TCL_ERROR;
}
for(;;){
int n;
if( n<=0 ) break;
}
return TCL_OK;
}
/*
** Register the two TCL commands above with the TCL interpreter.
*/
return TCL_OK;
}
/*
** During testing, the special md5sum() aggregate function is available.
** inside SQLite. The following routines implement that function.
*/
MD5Context *p;
int i;
if( argc<1 ) return;
p = sqlite_aggregate_context(context, sizeof(*p));
if( p==0 ) return;
MD5Init(p);
}
for(i=0; i<argc; i++){
if( argv[i] ){
}
}
}
MD5Context *p;
unsigned char digest[16];
char zBuf[33];
p = sqlite_aggregate_context(context, sizeof(*p));
}
}