X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/be4401bfa4a240bbc894e1bfeb1e1e8d63fc7b18..1ce0597f39134fe2948d621d566b7c3c8f278d91:/apt-pkg/contrib/md5.cc diff --git a/apt-pkg/contrib/md5.cc b/apt-pkg/contrib/md5.cc index 671d61d06..44242371a 100644 --- a/apt-pkg/contrib/md5.cc +++ b/apt-pkg/contrib/md5.cc @@ -1,25 +1,23 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: md5.cc,v 1.2 1998/11/01 05:27:36 jgg Exp $ +// $Id: md5.cc,v 1.12 2001/05/13 05:15:03 jgg Exp $ /* ###################################################################### MD5Sum - MD5 Message Digest Algorithm. - 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. + 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, instantiate the class, + and repeatedly call one of the Add() members. When finished the Result + method will return the Hash and finalize the value. + Changed so as no longer to depend on Colin Plumb's `usual.h' header definitions; now uses stuff from dpkg's config.h. - Ian Jackson . @@ -30,37 +28,35 @@ 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. - - Some of the terms may be quite bogus, I don't really know the details of - MD5, just converted the code ;> - JGG + 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 +#include + #include -#include #include +#include // For htonl +#include #include +#include /*}}}*/ // 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); @@ -72,7 +68,7 @@ static void byteSwap(UINT32 *buf, unsigned words) // MD5Transform - Alters an existing MD5 hash /*{{{*/ // --------------------------------------------------------------------- /* The core of the MD5 algorithm, this alters an existing MD5 hash to - reflect the addition of 16 longwords of new data. MD5Update blocks + reflect the addition of 16 longwords of new data. Add blocks the data and converts bytes into longwords for this routine. */ // The four core functions - F1 is optimized somewhat @@ -86,9 +82,9 @@ static void byteSwap(UINT32 *buf, unsigned words) #define MD5STEP(f,w,x,y,z,in,s) \ (w += f(x,y,z) + in, w = (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]; @@ -169,22 +165,6 @@ static void MD5Transform(UINT32 buf[4], UINT32 const in[16]) buf[3] += d; } /*}}}*/ -// hex_digit - Convert a hex character into an integer /*{{{*/ -// --------------------------------------------------------------------- -/* The original version of this could only handle lower case. These routines - do output to lowercase hex but can handle upper okay as well.*/ -static int hex_digit(int c) -{ - - if (c >= '0' && c <= '9') - return c - '0'; - if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - return 0; -} - /*}}}*/ // MD5SumValue::MD5SumValue - Constructs the summation from a string /*{{{*/ // --------------------------------------------------------------------- /* The string form of a MD5 is a 32 character hex number */ @@ -206,26 +186,8 @@ MD5SumValue::MD5SumValue() // --------------------------------------------------------------------- /* Converts the hex string into a set of chars */ bool MD5SumValue::Set(string Str) -{ - // Check for correct length - if (Str.length() != 32) - return false; - - // Check for only hex digits - const char *I = Str.begin(); - for (;I != Str.end(); I++) - if (isxdigit(*I) == 0) - return false; - - // Convert each digit. We store it in the same order as the string - int J = 0; - for (I = Str.begin(); I != Str.end();J++, I += 2) - { - Sum[J] = hex_digit(I[0]) << 4; - Sum[J] += hex_digit(I[1]); - } - - return true; +{ + return Hex2Num(Str,Sum,sizeof(Sum)); } /*}}}*/ // MD5SumValue::Value - Convert the number into a string /*{{{*/ @@ -252,7 +214,7 @@ string MD5SumValue::Value() const /*}}}*/ // MD5SumValue::operator == - Comparitor /*{{{*/ // --------------------------------------------------------------------- -/* Call memcmp on the buffer */ +/* Call memcmp on the buffer */ bool MD5SumValue::operator ==(const MD5SumValue &rhs) const { return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0; @@ -263,8 +225,8 @@ bool MD5SumValue::operator ==(const MD5SumValue &rhs) const /* 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; @@ -276,7 +238,7 @@ MD5Summation::MD5Summation() Done = false; } /*}}}*/ -// MD5Summation::Add - 'Add' a data set to the sum /*{{{*/ +// MD5Summation::Add - 'Add' a data set to the hash /*{{{*/ // --------------------------------------------------------------------- /* */ bool MD5Summation::Add(const unsigned char *data,unsigned long len) @@ -284,16 +246,16 @@ bool MD5Summation::Add(const unsigned char *data,unsigned 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]++; - // Space available in ctx->in (at least 1) + // Space available (at least 1) t = 64 - (t & 0x3f); if (t > len) { @@ -333,8 +295,8 @@ bool MD5Summation::AddFD(int Fd,unsigned long Size) int Res = 0; while (Size != 0) { - Res = read(Fd,Buf,MIN(Size,sizeof(Buf))); - if (Res < 0 || (unsigned)Res != MIN(Size,sizeof(Buf))) + Res = read(Fd,Buf,min(Size,(unsigned long)sizeof(Buf))); + if (Res < 0 || (unsigned)Res != min(Size,(unsigned long)sizeof(Buf))) return false; Size -= Res; Add(Buf,Res); @@ -348,9 +310,9 @@ bool MD5Summation::AddFD(int Fd,unsigned long Size) 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) {