/* * 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 "fcint.h" struct MD5Context { … }; static void MD5Init(struct MD5Context *ctx); static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len); static void MD5Final(unsigned char digest[16], struct MD5Context *ctx); static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]); #ifndef WORDS_BIGENDIAN #define byteReverse(buf, len) … #else /* * Note: this code is harmless on little-endian machines. */ void byteReverse(unsigned char *buf, unsigned longs) { FcChar32 t; do { t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 | ((unsigned) buf[1] << 8 | buf[0]); *(FcChar32 *) buf = t; buf += 4; } while (--longs); } #endif /* * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious * initialization constants. */ static void MD5Init(struct MD5Context *ctx) { … } /* * Update context to reflect the concatenation of another buffer full * of bytes. */ static void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len) { … } /* * Final wrapup - pad to 64-byte boundary with the bit pattern * 1 0* (64-bit count of bits processed, MSB-first) */ static void MD5Final(unsigned char digest[16], struct MD5Context *ctx) { … } /* The four core functions - F1 is optimized somewhat */ /* #define F1(x, y, z) (x & y | ~x & z) */ #define F1(x, y, z) … #define F2(x, y, z) … #define F3(x, y, z) … #define F4(x, y, z) … /* This is the central step in the MD5 algorithm. */ #define MD5STEP(f, w, x, y, z, data, s) … /* * 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. */ static void MD5Transform(FcChar32 buf[4], FcChar32 in[16]) { … }