sha1.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #ifdef __GNUG__
  14. #pragma interface "apt-pkg/sha1.h"
  15. #endif
  16. #include <string>
  17. #include <algorithm>
  18. using std::string;
  19. using std::min;
  20. class SHA1Summation;
  21. class SHA1SumValue
  22. {
  23. friend class SHA1Summation;
  24. unsigned char Sum[20];
  25. public:
  26. // Accessors
  27. bool operator ==(const SHA1SumValue &rhs) const;
  28. string Value() const;
  29. inline void Value(unsigned char S[20])
  30. {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
  31. inline operator string() const {return Value();};
  32. bool Set(string Str);
  33. inline void Set(unsigned char S[20])
  34. {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
  35. SHA1SumValue(string Str);
  36. SHA1SumValue();
  37. };
  38. class SHA1Summation
  39. {
  40. /* assumes 64-bit alignment just in case */
  41. unsigned char Buffer[64] __attribute__((aligned(8)));
  42. unsigned char State[5*4] __attribute__((aligned(8)));
  43. unsigned char Count[2*4] __attribute__((aligned(8)));
  44. bool Done;
  45. public:
  46. bool Add(const unsigned char *inbuf,unsigned long inlen);
  47. inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
  48. bool AddFD(int Fd,unsigned long Size);
  49. inline bool Add(const unsigned char *Beg,const unsigned char *End)
  50. {return Add(Beg,End-Beg);};
  51. SHA1SumValue Result();
  52. SHA1Summation();
  53. };
  54. #endif