Parcourir la source

template based hashsum implementation

Michael Vogt il y a 15 ans
Parent
commit
7ac56f8ffd

+ 87 - 0
apt-pkg/contrib/hashsum_template.h

@@ -0,0 +1,87 @@
+// -*- mode: cpp; mode: fold -*-
+// Description                                                          /*{{{*/
+// $Id: hashsum_template.h,v 1.3 2001/05/07 05:05:47 jgg Exp $
+/* ######################################################################
+
+   HashSumValueTemplate - Generic Storage for a hash value
+   
+   ##################################################################### */
+                                                                        /*}}}*/
+#ifndef APTPKG_HASHSUM_TEMPLATE_H
+#define APTPKG_HASHSUM_TEMPLATE_H
+
+#include <string>
+#include <cstring>
+#include <algorithm>
+#include <stdint.h>
+
+using std::string;
+using std::min;
+
+template<int N>
+class HashSumValue
+{
+   unsigned char Sum[N/8];
+   
+   public:
+
+   // Accessors
+   bool operator ==(const HashSumValue &rhs) const
+   {
+      return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
+   }; 
+
+   string Value() const
+   {
+      char Conv[16] =
+      { '0','1','2','3','4','5','6','7','8','9','a','b',
+        'c','d','e','f'
+      };
+      char Result[((N/8)*2)+1];
+      Result[(N/8)*2] = 0;
+      
+      // Convert each char into two letters
+      int J = 0;
+      int I = 0;
+      for (; I != (N/8)*2; J++,I += 2)
+      {
+         Result[I] = Conv[Sum[J] >> 4];
+         Result[I + 1] = Conv[Sum[J] & 0xF];
+      }
+      return string(Result);
+   };
+   
+   inline void Value(unsigned char S[N/8])
+   {
+      for (int I = 0; I != sizeof(Sum); I++) 
+         S[I] = Sum[I];
+   };
+
+   inline operator string() const 
+   {
+      return Value();
+   };
+
+   bool Set(string Str) 
+   {
+      return Hex2Num(Str,Sum,sizeof(Sum));
+   };
+
+   inline void Set(unsigned char S[N/8]) 
+   {
+      for (int I = 0; I != sizeof(Sum); I++) 
+         Sum[I] = S[I];
+   };
+
+   HashSumValue(string Str) 
+   {
+         memset(Sum,0,sizeof(Sum));
+         Set(Str);
+   }
+   HashSumValue()
+   {
+      memset(Sum,0,sizeof(Sum));
+   }
+};
+
+#endif

+ 1 - 56
apt-pkg/contrib/md5.cc

@@ -165,61 +165,6 @@ static void MD5Transform(uint32_t buf[4], uint32_t const in[16])
    buf[3] += d;
    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,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			/*{{{*/
 // MD5Summation::MD5Summation - Initialize the summer			/*{{{*/
 // ---------------------------------------------------------------------
 // ---------------------------------------------------------------------
 /* This assigns the deep magic initial values */
 /* This assigns the deep magic initial values */
@@ -353,7 +298,7 @@ MD5SumValue MD5Summation::Result()
    }
    }
    
    
    MD5SumValue V;
    MD5SumValue V;
-   memcpy(V.Sum,buf,16);
+   V.Set((char *)buf);
    return V;
    return V;
 }
 }
 									/*}}}*/
 									/*}}}*/

+ 3 - 20
apt-pkg/contrib/md5.h

@@ -32,28 +32,11 @@
 using std::string;
 using std::string;
 using std::min;
 using std::min;
 
 
-class MD5Summation;
+#include "hashsum_template.h"
 
 
-class MD5SumValue
-{
-   friend class MD5Summation;
-   unsigned char Sum[4*4];
-   
-   public:
-
-   // Accessors
-   bool operator ==(const MD5SumValue &rhs) const; 
-   string Value() const;
-   inline void Value(unsigned char S[16]) 
-         {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
-   inline operator string() const {return Value();};
-   bool Set(string Str);
-   inline void Set(unsigned char S[16]) 
-         {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
+class MD5Summation;
 
 
-   MD5SumValue(string Str);
-   MD5SumValue();
-};
+typedef HashSumValue<128> MD5SumValue;
 
 
 class MD5Summation
 class MD5Summation
 {
 {

+ 3 - 62
apt-pkg/contrib/sha1.cc

@@ -178,67 +178,6 @@ static void SHA1Transform(uint32_t state[5],uint8_t const buffer[64])
 }
 }
 									/*}}}*/
 									/*}}}*/
 
 
-// SHA1SumValue::SHA1SumValue - Constructs the summation from a string  /*{{{*/
-// ---------------------------------------------------------------------
-/* The string form of a SHA1 is a 40 character hex number */
-SHA1SumValue::SHA1SumValue(string Str)
-{
-   memset(Sum,0,sizeof(Sum));
-   Set(Str);
-}
-
-									/*}}} */
-// SHA1SumValue::SHA1SumValue - Default constructor                     /*{{{*/
-// ---------------------------------------------------------------------
-/* Sets the value to 0 */
-SHA1SumValue::SHA1SumValue()
-{
-   memset(Sum,0,sizeof(Sum));
-}
-
-									/*}}} */
-// SHA1SumValue::Set - Set the sum from a string                        /*{{{*/
-// ---------------------------------------------------------------------
-/* Converts the hex string into a set of chars */
-bool SHA1SumValue::Set(string Str)
-{
-   return Hex2Num(Str,Sum,sizeof(Sum));
-}
-
-									/*}}} */
-// SHA1SumValue::Value - Convert the number into a string               /*{{{*/
-// ---------------------------------------------------------------------
-/* Converts the set of chars into a hex string in lower case */
-string SHA1SumValue::Value() const
-{
-   char Conv[16] =
-      { '0','1','2','3','4','5','6','7','8','9','a','b',
-      'c','d','e','f'
-   };
-   char Result[41];
-   Result[40] = 0;
-
-   // Convert each char into two letters
-   int J = 0;
-   int I = 0;
-   for (; I != 40; J++,I += 2)
-   {
-      Result[I] = Conv[Sum[J] >> 4];
-      Result[I + 1] = Conv[Sum[J] & 0xF];
-   }
-
-   return string(Result);
-}
-
-									/*}}} */
-// SHA1SumValue::operator == - Comparator                               /*{{{*/
-// ---------------------------------------------------------------------
-/* Call memcmp on the buffer */
-bool SHA1SumValue::operator == (const SHA1SumValue & rhs) const
-{
-   return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
-}
-									/*}}}*/
 // SHA1Summation::SHA1Summation - Constructor                           /*{{{*/
 // SHA1Summation::SHA1Summation - Constructor                           /*{{{*/
 // ---------------------------------------------------------------------
 // ---------------------------------------------------------------------
 /* */
 /* */
@@ -290,11 +229,13 @@ SHA1SumValue SHA1Summation::Result()
 
 
    // Transfer over the result
    // Transfer over the result
    SHA1SumValue Value;
    SHA1SumValue Value;
+   char res[20];
    for (unsigned i = 0; i < 20; i++)
    for (unsigned i = 0; i < 20; i++)
    {
    {
-      Value.Sum[i] = (unsigned char)
+      res[i] = (unsigned char)
 	 ((state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
 	 ((state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
    }
    }
+   Value.Set(res);
    return Value;
    return Value;
 }
 }
 									/*}}}*/
 									/*}}}*/

+ 3 - 20
apt-pkg/contrib/sha1.h

@@ -21,28 +21,11 @@
 using std::string;
 using std::string;
 using std::min;
 using std::min;
 
 
-class SHA1Summation;
+#include "hashsum_template.h"
 
 
-class SHA1SumValue
-{
-   friend class SHA1Summation;
-   unsigned char Sum[20];
-   
-   public:
-
-   // Accessors
-   bool operator ==(const SHA1SumValue &rhs) const; 
-   string Value() const;
-   inline void Value(unsigned char S[20])
-         {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
-   inline operator string() const {return Value();};
-   bool Set(string Str);
-   inline void Set(unsigned char S[20]) 
-         {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
+class SHA1Summation;
 
 
-   SHA1SumValue(string Str);
-   SHA1SumValue();
-};
+typedef  HashSumValue<160> SHA1SumValue;
 
 
 class SHA1Summation
 class SHA1Summation
 {
 {

+ 11 - 121
apt-pkg/contrib/sha2.cc

@@ -12,26 +12,22 @@
  */									/*}}}*/
  */									/*}}}*/
 
 
 #ifdef __GNUG__
 #ifdef __GNUG__
-#pragma implementation "apt-pkg/2.h"
+#pragma implementation "apt-pkg/sha2.h"
 #endif
 #endif
 
 
 #include <apt-pkg/sha2.h>
 #include <apt-pkg/sha2.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/strutl.h>
 
 
+
+
+
 SHA512Summation::SHA512Summation()					/*{{{*/
 SHA512Summation::SHA512Summation()					/*{{{*/
 {
 {
    SHA512_Init(&ctx);
    SHA512_Init(&ctx);
    Done = false;
    Done = false;
 }
 }
-									/*}}}*/
-bool SHA512Summation::Add(const unsigned char *inbuf,unsigned long len) /*{{{*/
-{
-   if (Done) 
-      return false;
-   SHA512_Update(&ctx, inbuf, len);
-   return true;
-}
-									/*}}}*/
+         								/*}}}*/
+
 SHA512SumValue SHA512Summation::Result()				/*{{{*/
 SHA512SumValue SHA512Summation::Result()				/*{{{*/
 {
 {
    if (!Done) {
    if (!Done) {
@@ -44,63 +40,14 @@ SHA512SumValue SHA512Summation::Result()				/*{{{*/
    return res;
    return res;
 }
 }
 									/*}}}*/
 									/*}}}*/
-// SHA512SumValue::SHA512SumValue - Constructs the sum from a string   /*{{{*/
-// ---------------------------------------------------------------------
-/* The string form of a SHA512 is a 64 character hex number */
-SHA512SumValue::SHA512SumValue(string Str)
-{
-   memset(Sum,0,sizeof(Sum));
-   Set(Str);
-}
-                                                                       /*}}}*/
-// SHA512SumValue::SHA512SumValue - Default constructor                /*{{{*/
-// ---------------------------------------------------------------------
-/* Sets the value to 0 */
-SHA512SumValue::SHA512SumValue()
-{
-   memset(Sum,0,sizeof(Sum));
-}
-                                                                       /*}}}*/
-// SHA512SumValue::Set - Set the sum from a string                     /*{{{*/
-// ---------------------------------------------------------------------
-/* Converts the hex string into a set of chars */
-bool SHA512SumValue::Set(string Str)
-{
-   return Hex2Num(Str,Sum,sizeof(Sum));
-}
-                                                                       /*}}}*/
-// SHA512SumValue::Value - Convert the number into a string            /*{{{*/
-// ---------------------------------------------------------------------
-/* Converts the set of chars into a hex string in lower case */
-string SHA512SumValue::Value() const
+bool SHA512Summation::Add(const unsigned char *inbuf,unsigned long len) /*{{{*/
 {
 {
-   char Conv[16] =
-      { '0','1','2','3','4','5','6','7','8','9','a','b',
-      'c','d','e','f'
-   };
-   char Result[129];
-   Result[128] = 0;
-
-   // Convert each char into two letters
-   int J = 0;
-   int I = 0;
-   for (; I != 128; J++,I += 2)
-   {
-      Result[I] = Conv[Sum[J] >> 4];
-      Result[I + 1] = Conv[Sum[J] & 0xF];
-   }
-
-   return string(Result);
+   if (Done) 
+      return false;
+   SHA512_Update(&ctx, inbuf, len);
+   return true;
 }
 }
 									/*}}}*/
 									/*}}}*/
-// SHA512SumValue::operator == - Comparator                            /*{{{*/
-// ---------------------------------------------------------------------
-/* Call memcmp on the buffer */
-bool SHA512SumValue::operator == (const SHA512SumValue & rhs) const
-{
-   return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
-}
-                                                                       /*}}}*/
 // SHA512Summation::AddFD - Add content of file into the checksum      /*{{{*/
 // SHA512Summation::AddFD - Add content of file into the checksum      /*{{{*/
 // ---------------------------------------------------------------------
 // ---------------------------------------------------------------------
 /* */
 /* */
@@ -151,63 +98,6 @@ SHA256SumValue SHA256Summation::Result()				/*{{{*/
    return res;
    return res;
 }
 }
 									/*}}}*/
 									/*}}}*/
-// SHA256SumValue::SHA256SumValue - Constructs the sum from a string   /*{{{*/
-// ---------------------------------------------------------------------
-/* The string form of a SHA512 is a 64 character hex number */
-SHA256SumValue::SHA256SumValue(string Str)
-{
-   memset(Sum,0,sizeof(Sum));
-   Set(Str);
-}
-                                                                       /*}}}*/
-// SHA256SumValue::SHA256SumValue - Default constructor                /*{{{*/
-// ---------------------------------------------------------------------
-/* Sets the value to 0 */
-SHA256SumValue::SHA256SumValue()
-{
-   memset(Sum,0,sizeof(Sum));
-}
-                                                                       /*}}}*/
-// SHA256SumValue::Set - Set the sum from a string                     /*{{{*/
-// ---------------------------------------------------------------------
-/* Converts the hex string into a set of chars */
-bool SHA256SumValue::Set(string Str)
-{
-   return Hex2Num(Str,Sum,sizeof(Sum));
-}
-                                                                       /*}}}*/
-// SHA256SumValue::Value - Convert the number into a string            /*{{{*/
-// ---------------------------------------------------------------------
-/* Converts the set of chars into a hex string in lower case */
-string SHA256SumValue::Value() const
-{
-   char Conv[16] =
-      { '0','1','2','3','4','5','6','7','8','9','a','b',
-      'c','d','e','f'
-   };
-   char Result[129];
-   Result[128] = 0;
-
-   // Convert each char into two letters
-   int J = 0;
-   int I = 0;
-   for (; I != 128; J++,I += 2)
-   {
-      Result[I] = Conv[Sum[J] >> 4];
-      Result[I + 1] = Conv[Sum[J] & 0xF];
-   }
-
-   return string(Result);
-}
-									/*}}}*/
-// SHA256SumValue::operator == - Comparator                            /*{{{*/
-// ---------------------------------------------------------------------
-/* Call memcmp on the buffer */
-bool SHA256SumValue::operator == (const SHA256SumValue & rhs) const
-{
-   return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
-}
-                                                                       /*}}}*/
 // SHA256Summation::AddFD - Add content of file into the checksum      /*{{{*/
 // SHA256Summation::AddFD - Add content of file into the checksum      /*{{{*/
 // ---------------------------------------------------------------------
 // ---------------------------------------------------------------------
 /* */
 /* */

+ 15 - 55
apt-pkg/contrib/sha2.h

@@ -20,38 +20,21 @@
 #include <stdint.h>
 #include <stdint.h>
 
 
 #include "sha2_internal.h"
 #include "sha2_internal.h"
+#include "hashsum_template.h"
 
 
 using std::string;
 using std::string;
 using std::min;
 using std::min;
 
 
-// SHA512
 class SHA512Summation;
 class SHA512Summation;
+class SHA256Summation;
 
 
-class SHA512SumValue
-{
-   friend class SHA512Summation;
-   unsigned char Sum[64];
-   
-   public:
-
-   // Accessors
-   bool operator ==(const SHA512SumValue &rhs) const; 
-   string Value() const;
-   inline void Value(unsigned char S[64])
-         {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
-   inline operator string() const {return Value();};
-   bool Set(string Str);
-   inline void Set(unsigned char S[64]) 
-         {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
-
-   SHA512SumValue(string Str);
-   SHA512SumValue();
-};
+typedef HashSumValue<512> SHA512SumValue;
+typedef HashSumValue<256> SHA256SumValue;
 
 
-class SHA512Summation
+class SHA256Summation
 {
 {
-   SHA512_CTX ctx;
-   unsigned char Sum[64];
+   SHA256_CTX ctx;
+   unsigned char Sum[32];
    bool Done;
    bool Done;
 
 
    public:
    public:
@@ -61,39 +44,15 @@ class SHA512Summation
    bool AddFD(int Fd,unsigned long Size);
    bool AddFD(int Fd,unsigned long Size);
    inline bool Add(const unsigned char *Beg,const unsigned char *End) 
    inline bool Add(const unsigned char *Beg,const unsigned char *End) 
                   {return Add(Beg,End-Beg);};
                   {return Add(Beg,End-Beg);};
-   SHA512SumValue Result();
-   
-   SHA512Summation();
-};
-
-// SHA256
-class SHA256Summation;
-
-class SHA256SumValue
-{
-   friend class SHA256Summation;
-   unsigned char Sum[32];
+   SHA256SumValue Result();
    
    
-   public:
-
-   // Accessors
-   bool operator ==(const SHA256SumValue &rhs) const; 
-   string Value() const;
-   inline void Value(unsigned char S[32])
-         {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
-   inline operator string() const {return Value();};
-   bool Set(string Str);
-   inline void Set(unsigned char S[32]) 
-         {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
-
-   SHA256SumValue(string Str);
-   SHA256SumValue();
+   SHA256Summation();
 };
 };
 
 
-class SHA256Summation
+class SHA512Summation
 {
 {
-   SHA256_CTX ctx;
-   unsigned char Sum[32];
+   SHA512_CTX ctx;
+   unsigned char Sum[64];
    bool Done;
    bool Done;
 
 
    public:
    public:
@@ -103,9 +62,10 @@ class SHA256Summation
    bool AddFD(int Fd,unsigned long Size);
    bool AddFD(int Fd,unsigned long Size);
    inline bool Add(const unsigned char *Beg,const unsigned char *End) 
    inline bool Add(const unsigned char *Beg,const unsigned char *End) 
                   {return Add(Beg,End-Beg);};
                   {return Add(Beg,End-Beg);};
-   SHA256SumValue Result();
+   SHA512SumValue Result();
    
    
-   SHA256Summation();
+   SHA512Summation();
 };
 };
 
 
+
 #endif
 #endif

+ 1 - 1
apt-pkg/makefile

@@ -28,7 +28,7 @@ SOURCE = contrib/mmap.cc contrib/error.cc contrib/strutl.cc \
 HEADERS = mmap.h error.h configuration.h fileutl.h  cmndline.h netrc.h\
 HEADERS = mmap.h error.h configuration.h fileutl.h  cmndline.h netrc.h\
 	  md5.h crc-16.h cdromutl.h strutl.h sptr.h sha1.h sha2.h \
 	  md5.h crc-16.h cdromutl.h strutl.h sptr.h sha1.h sha2.h \
 	  sha2_internal.h \
 	  sha2_internal.h \
-          hashes.h \
+          hashes.h hashsum_template.h\
 	  macros.h weakptr.h
 	  macros.h weakptr.h
 
 
 # Source code for the core main library
 # Source code for the core main library