sha1.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: sha1.h,v 1.3 2001/05/07 05:05:47 jgg Exp $
  4. /* ######################################################################
  5. SHA1SumValue - Storage for a SHA-1 hash.
  6. SHA1Summation - SHA-1 Secure Hash Algorithm.
  7. This is a C++ interface to a set of SHA1Sum functions, that mirrors
  8. the equivalent MD5 classes.
  9. ##################################################################### */
  10. /*}}}*/
  11. #ifndef APTPKG_SHA1_H
  12. #define APTPKG_SHA1_H
  13. #include <string>
  14. #include <algorithm>
  15. using std::string;
  16. using std::min;
  17. class SHA1Summation;
  18. class SHA1SumValue
  19. {
  20. friend class SHA1Summation;
  21. unsigned char Sum[20];
  22. public:
  23. // Accessors
  24. bool operator ==(const SHA1SumValue &rhs) const;
  25. string Value() const;
  26. inline void Value(unsigned char S[20])
  27. {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
  28. inline operator string() const {return Value();};
  29. bool Set(string Str);
  30. inline void Set(unsigned char S[20])
  31. {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
  32. SHA1SumValue(string Str);
  33. SHA1SumValue();
  34. };
  35. class SHA1Summation
  36. {
  37. /* assumes 64-bit alignment just in case */
  38. unsigned char Buffer[64] __attribute__((aligned(8)));
  39. unsigned char State[5*4] __attribute__((aligned(8)));
  40. unsigned char Count[2*4] __attribute__((aligned(8)));
  41. bool Done;
  42. public:
  43. bool Add(const unsigned char *inbuf,unsigned long inlen);
  44. inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
  45. bool AddFD(int Fd,unsigned long Size);
  46. inline bool Add(const unsigned char *Beg,const unsigned char *End)
  47. {return Add(Beg,End-Beg);};
  48. SHA1SumValue Result();
  49. SHA1Summation();
  50. };
  51. #endif