summaryrefslogtreecommitdiff
path: root/apt-pkg
diff options
context:
space:
mode:
authorMichael Vogt <michael.vogt@ubuntu.com>2011-02-25 18:59:29 +0100
committerMichael Vogt <michael.vogt@ubuntu.com>2011-02-25 18:59:29 +0100
commit7ac56f8ffd5544c6c1f681f79cafbf72d37d0b82 (patch)
treeb14e5e4c1a03be5ac2bd22e41ee74e3634e71923 /apt-pkg
parent84a0890e6ef49b5d41a0b9ff0b5a5fe95cca6f3e (diff)
template based hashsum implementation
Diffstat (limited to 'apt-pkg')
-rw-r--r--apt-pkg/contrib/hashsum_template.h87
-rw-r--r--apt-pkg/contrib/md5.cc57
-rw-r--r--apt-pkg/contrib/md5.h23
-rw-r--r--apt-pkg/contrib/sha1.cc65
-rw-r--r--apt-pkg/contrib/sha1.h23
-rw-r--r--apt-pkg/contrib/sha2.cc132
-rw-r--r--apt-pkg/contrib/sha2.h70
-rw-r--r--apt-pkg/makefile2
8 files changed, 124 insertions, 335 deletions
diff --git a/apt-pkg/contrib/hashsum_template.h b/apt-pkg/contrib/hashsum_template.h
new file mode 100644
index 000000000..7667baf92
--- /dev/null
+++ b/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
diff --git a/apt-pkg/contrib/md5.cc b/apt-pkg/contrib/md5.cc
index c0fa8493d..6c60ffd74 100644
--- a/apt-pkg/contrib/md5.cc
+++ b/apt-pkg/contrib/md5.cc
@@ -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;
}
/*}}}*/
diff --git a/apt-pkg/contrib/md5.h b/apt-pkg/contrib/md5.h
index 96c8975b4..9cc88cfbe 100644
--- a/apt-pkg/contrib/md5.h
+++ b/apt-pkg/contrib/md5.h
@@ -32,28 +32,11 @@
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
{
diff --git a/apt-pkg/contrib/sha1.cc b/apt-pkg/contrib/sha1.cc
index eae52d52f..0b1c16dc3 100644
--- a/apt-pkg/contrib/sha1.cc
+++ b/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 /*{{{*/
// ---------------------------------------------------------------------
/* */
@@ -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;
}
/*}}}*/
diff --git a/apt-pkg/contrib/sha1.h b/apt-pkg/contrib/sha1.h
index 8ddd889f1..e7683fa7b 100644
--- a/apt-pkg/contrib/sha1.h
+++ b/apt-pkg/contrib/sha1.h
@@ -21,28 +21,11 @@
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
{
diff --git a/apt-pkg/contrib/sha2.cc b/apt-pkg/contrib/sha2.cc
index 00d90d6ba..dcdbef6e7 100644
--- a/apt-pkg/contrib/sha2.cc
+++ b/apt-pkg/contrib/sha2.cc
@@ -12,26 +12,22 @@
*/ /*}}}*/
#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 /*{{{*/
// ---------------------------------------------------------------------
/* */
diff --git a/apt-pkg/contrib/sha2.h b/apt-pkg/contrib/sha2.h
index 5148b05c3..2c3fcae12 100644
--- a/apt-pkg/contrib/sha2.h
+++ b/apt-pkg/contrib/sha2.h
@@ -20,38 +20,21 @@
#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
diff --git a/apt-pkg/makefile b/apt-pkg/makefile
index 313aefe7d..b94b88257 100644
--- a/apt-pkg/makefile
+++ b/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\
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