sha1.h 1.8 KB

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