// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
-// $Id: md5.cc,v 1.3 1999/08/02 03:07:47 jgg Exp $
+// $Id: md5.cc,v 1.12 2001/05/13 05:15:03 jgg Exp $
/* ######################################################################
MD5Sum - MD5 Message Digest Algorithm.
Still in the public domain.
The classes use arrays of char that are a specific size. We cast those
- arrays to UINT32's and go from there. This allows us to advoid using
- config.h in a public header or internally newing memory.
+ arrays to uint8_t's and go from there. This allows us to advoid using
+ the uncommon inttypes.h in a public header or internally newing memory.
+ In theory if C9x becomes nicely accepted
##################################################################### */
/*}}}*/
// Include Files /*{{{*/
-#ifdef __GNUG__
-#pragma implementation "apt-pkg/md5.h"
-#endif
+#include <config.h>
#include <apt-pkg/md5.h>
-#include <apt-pkg/strutl.h>
+#include <stdint.h>
#include <string.h>
-#include <system.h>
-#include <unistd.h>
-#include <config.h>
/*}}}*/
// byteSwap - Swap bytes in a buffer /*{{{*/
// ---------------------------------------------------------------------
-/* This byteswap function will swap byte in a buffer of data */
+/* Swap n 32 bit longs in given buffer */
#ifdef WORDS_BIGENDIAN
-static void byteSwap(UINT32 *buf, unsigned words)
+static void byteSwap(uint32_t *buf, unsigned words)
{
- unsigned char *p = (unsigned char *)buf;
+ uint8_t *p = (uint8_t *)buf;
do
{
- *buf++ = (UINT32)((unsigned)p[3] << 8 | p[2]) << 16 |
+ *buf++ = (uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
((unsigned)p[1] << 8 | p[0]);
p += 4;
} while (--words);
#define MD5STEP(f,w,x,y,z,in,s) \
(w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
-static void MD5Transform(UINT32 buf[4], UINT32 const in[16])
+static void MD5Transform(uint32_t buf[4], uint32_t const in[16])
{
- register UINT32 a, b, c, d;
+ register uint32_t a, b, c, d;
a = buf[0];
b = buf[1];
buf[3] += d;
}
/*}}}*/
-// MD5SumValue::MD5SumValue - Constructs the summation from a string /*{{{*/
-// ---------------------------------------------------------------------
-/* The string form of a MD5 is a 32 character hex number */
-MD5SumValue::MD5SumValue(string Str)
-{
- memset(Sum,0,sizeof(Sum));
- Set(Str);
-}
- /*}}}*/
-// MD5SumValue::MD5SumValue - Default constructor /*{{{*/
-// ---------------------------------------------------------------------
-/* Sets the value to 0 */
-MD5SumValue::MD5SumValue()
-{
- memset(Sum,0,sizeof(Sum));
-}
- /*}}}*/
-// MD5SumValue::Set - Set the sum from a string /*{{{*/
-// ---------------------------------------------------------------------
-/* Converts the hex string into a set of chars */
-bool MD5SumValue::Set(string Str)
-{
- return Hex2Num(Str.begin(),Str.end(),Sum,sizeof(Sum));
-}
- /*}}}*/
-// MD5SumValue::Value - Convert the number into a string /*{{{*/
-// ---------------------------------------------------------------------
-/* Converts the set of chars into a hex string in lower case */
-string MD5SumValue::Value() const
-{
- char Conv[16] = {'0','1','2','3','4','5','6','7','8','9','a','b',
- 'c','d','e','f'};
- char Result[33];
- Result[32] = 0;
-
- // Convert each char into two letters
- int J = 0;
- int I = 0;
- for (; I != 32; J++, I += 2)
- {
- Result[I] = Conv[Sum[J] >> 4];
- Result[I + 1] = Conv[Sum[J] & 0xF];
- }
-
- return string(Result);
-}
- /*}}}*/
-// MD5SumValue::operator == - Comparitor /*{{{*/
-// ---------------------------------------------------------------------
-/* Call memcmp on the buffer */
-bool MD5SumValue::operator ==(const MD5SumValue &rhs) const
-{
- return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
-}
- /*}}}*/
// MD5Summation::MD5Summation - Initialize the summer /*{{{*/
// ---------------------------------------------------------------------
/* This assigns the deep magic initial values */
MD5Summation::MD5Summation()
{
- UINT32 *buf = (UINT32 *)Buf;
- UINT32 *bytes = (UINT32 *)Bytes;
+ uint32_t *buf = (uint32_t *)Buf;
+ uint32_t *bytes = (uint32_t *)Bytes;
buf[0] = 0x67452301;
buf[1] = 0xefcdab89;
// MD5Summation::Add - 'Add' a data set to the hash /*{{{*/
// ---------------------------------------------------------------------
/* */
-bool MD5Summation::Add(const unsigned char *data,unsigned long len)
+bool MD5Summation::Add(const unsigned char *data,unsigned long long len)
{
if (Done == true)
return false;
- UINT32 *buf = (UINT32 *)Buf;
- UINT32 *bytes = (UINT32 *)Bytes;
- UINT32 *in = (UINT32 *)In;
+ uint32_t *buf = (uint32_t *)Buf;
+ uint32_t *bytes = (uint32_t *)Bytes;
+ uint32_t *in = (uint32_t *)In;
// Update byte count and carry (this could be done with a long long?)
- UINT32 t = bytes[0];
+ uint32_t t = bytes[0];
if ((bytes[0] = t + len) < t)
bytes[1]++;
return true;
}
/*}}}*/
-// MD5Summation::AddFD - Add the contents of a FD to the hash /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool MD5Summation::AddFD(int Fd,unsigned long Size)
-{
- unsigned char Buf[64*64];
- int Res = 0;
- while (Size != 0)
- {
- Res = read(Fd,Buf,MIN(Size,sizeof(Buf)));
- if (Res < 0 || (unsigned)Res != MIN(Size,sizeof(Buf)))
- return false;
- Size -= Res;
- Add(Buf,Res);
- }
- return true;
-}
- /*}}}*/
// MD5Summation::Result - Returns the value of the sum /*{{{*/
// ---------------------------------------------------------------------
/* Because this must add in the last bytes of the series it prevents anyone
from calling add after. */
MD5SumValue MD5Summation::Result()
{
- UINT32 *buf = (UINT32 *)Buf;
- UINT32 *bytes = (UINT32 *)Bytes;
- UINT32 *in = (UINT32 *)In;
+ uint32_t *buf = (uint32_t *)Buf;
+ uint32_t *bytes = (uint32_t *)Bytes;
+ uint32_t *in = (uint32_t *)In;
if (Done == false)
{
}
MD5SumValue V;
- memcpy(V.Sum,buf,16);
+ V.Set((unsigned char *)buf);
return V;
}
/*}}}*/