md5_utils.c 7.41 KiB
/*
 * 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.
 * Changed so as no longer to depend on Colin Plumb's `usual.h' header
 * definitions
 *  - Ian Jackson <ian@chiark.greenend.org.uk>.
 * Still in the public domain.
#include <string.h>   /* for memcpy() */
#include "md5_utils.h"
void
byteSwap(UWORD32 *buf, unsigned words) {
  md5byte *p;
  /* Only swap bytes for big endian machines */
  int i = 1;
  if (*(char *)&i == 1)
    return;
  p = (md5byte *)buf;
  do {
    *buf++ = (UWORD32)((unsigned)p[3] << 8 | p[2]) << 16 |
             ((unsigned)p[1] << 8 | p[0]);
    p += 4;
  } while (--words);
 * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
 * initialization constants.
void
MD5Init(struct MD5Context *ctx) {
  ctx->buf[0] = 0x67452301;
  ctx->buf[1] = 0xefcdab89;
  ctx->buf[2] = 0x98badcfe;
  ctx->buf[3] = 0x10325476;
  ctx->bytes[0] = 0;
  ctx->bytes[1] = 0;
 * Update context to reflect the concatenation of another buffer full
 * of bytes.
void
MD5Update(struct MD5Context *ctx, md5byte const *buf, unsigned len) {
  UWORD32 t;
  /* Update byte count */