]> git.saurik.com Git - apt.git/commitdiff
template based hashsum implementation
authorMichael Vogt <michael.vogt@ubuntu.com>
Fri, 25 Feb 2011 17:59:29 +0000 (18:59 +0100)
committerMichael Vogt <michael.vogt@ubuntu.com>
Fri, 25 Feb 2011 17:59:29 +0000 (18:59 +0100)
apt-pkg/contrib/hashsum_template.h [new file with mode: 0644]
apt-pkg/contrib/md5.cc
apt-pkg/contrib/md5.h
apt-pkg/contrib/sha1.cc
apt-pkg/contrib/sha1.h
apt-pkg/contrib/sha2.cc
apt-pkg/contrib/sha2.h
apt-pkg/makefile

diff --git a/apt-pkg/contrib/hashsum_template.h b/apt-pkg/contrib/hashsum_template.h
new file mode 100644 (file)
index 0000000..7667baf
--- /dev/null
@@ -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
index c0fa8493dff2e161099c57ad2ac7377a0006139f..6c60ffd743a69bfad91b7ce66031be367e34a831 100644 (file)
@@ -165,61 +165,6 @@ static void MD5Transform(uint32_t buf[4], uint32_t const in[16])
    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                  /*{{{*/
 // ---------------------------------------------------------------------
 /* This assigns the deep magic initial values */
@@ -353,7 +298,7 @@ MD5SumValue MD5Summation::Result()
    }
    
    MD5SumValue V;
-   memcpy(V.Sum,buf,16);
+   V.Set((char *)buf);
    return V;
 }
                                                                        /*}}}*/
index 96c8975b46f6cc165b257f889bd1bb2f56ac5168..9cc88cfbe6811b7adfc484cb99c260eea712b027 100644 (file)
 using std::string;
 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
 {
index eae52d52f6341ea22abdb3ee3493bd48c3407316..0b1c16dc3af3ae111cc30715500fbdd006a1911b 100644 (file)
@@ -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                           /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -290,11 +229,13 @@ SHA1SumValue SHA1Summation::Result()
 
    // Transfer over the result
    SHA1SumValue Value;
+   char res[20];
    for (unsigned i = 0; i < 20; i++)
    {
-      Value.Sum[i] = (unsigned char)
+      res[i] = (unsigned char)
         ((state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
    }
+   Value.Set(res);
    return Value;
 }
                                                                        /*}}}*/
index 8ddd889f18a22a70b2ac9015e4772fbc195ce3a1..e7683fa7b034527d8742421971e25755dc087917 100644 (file)
 using std::string;
 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
 {
index 00d90d6ba548d22e354cec1fdd2e1ae2a120dba2..dcdbef6e757a7877c7b00652ec10b86b77be8a03 100644 (file)
  */                                                                    /*}}}*/
 
 #ifdef __GNUG__
-#pragma implementation "apt-pkg/2.h"
+#pragma implementation "apt-pkg/sha2.h"
 #endif
 
 #include <apt-pkg/sha2.h>
 #include <apt-pkg/strutl.h>
 
+
+
+
 SHA512Summation::SHA512Summation()                                     /*{{{*/
 {
    SHA512_Init(&ctx);
    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()                               /*{{{*/
 {
    if (!Done) {
@@ -44,63 +40,14 @@ SHA512SumValue SHA512Summation::Result()                            /*{{{*/
    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      /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -151,63 +98,6 @@ SHA256SumValue SHA256Summation::Result()                            /*{{{*/
    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      /*{{{*/
 // ---------------------------------------------------------------------
 /* */
index 5148b05c3be2f2259dc05736bfadc952dd1b8276..2c3fcae12c337a21b3b4c9fbbd6acf359172fd9f 100644 (file)
 #include <stdint.h>
 
 #include "sha2_internal.h"
+#include "hashsum_template.h"
 
 using std::string;
 using std::min;
 
-// SHA512
 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;
 
    public:
@@ -61,39 +44,15 @@ class SHA512Summation
    bool AddFD(int Fd,unsigned long Size);
    inline bool Add(const unsigned char *Beg,const unsigned char *End) 
                   {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;
 
    public:
@@ -103,9 +62,10 @@ class SHA256Summation
    bool AddFD(int Fd,unsigned long Size);
    inline bool Add(const unsigned char *Beg,const unsigned char *End) 
                   {return Add(Beg,End-Beg);};
-   SHA256SumValue Result();
+   SHA512SumValue Result();
    
-   SHA256Summation();
+   SHA512Summation();
 };
 
+
 #endif
index 313aefe7d44f4512d24f1a8359fc6a662698b72b..b94b882578b9b5fc64be23f338b58c099fdc070d 100644 (file)
@@ -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\
          md5.h crc-16.h cdromutl.h strutl.h sptr.h sha1.h sha2.h \
          sha2_internal.h \
-          hashes.h \
+          hashes.h hashsum_template.h\
          macros.h weakptr.h
 
 # Source code for the core main library