sha256.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: sha1.h,v 1.3 2001/05/07 05:05:47 jgg Exp $
  4. /* ######################################################################
  5. SHA256SumValue - Storage for a SHA-256 hash.
  6. SHA256Summation - SHA-256 Secure Hash Algorithm.
  7. This is a C++ interface to a set of SHA256Sum functions, that mirrors
  8. the equivalent MD5 & SHA1 classes.
  9. ##################################################################### */
  10. /*}}}*/
  11. #ifndef APTPKG_SHA256_H
  12. #define APTPKG_SHA256_H
  13. #ifdef __GNUG__
  14. #pragma interface "apt-pkg/sha256.h"
  15. #endif
  16. #include <string>
  17. #include <algorithm>
  18. using std::string;
  19. using std::min;
  20. class SHA256Summation;
  21. class SHA256SumValue
  22. {
  23. friend class SHA256Summation;
  24. unsigned char Sum[32];
  25. public:
  26. // Accessors
  27. bool operator ==(const SHA256SumValue &rhs) const;
  28. string Value() const;
  29. inline void Value(unsigned char S[32])
  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[32])
  34. {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
  35. SHA256SumValue(string Str);
  36. SHA256SumValue();
  37. };
  38. struct sha256_ctx {
  39. uint32_t count[2];
  40. uint32_t state[8];
  41. uint8_t buf[128];
  42. };
  43. class SHA256Summation
  44. {
  45. struct sha256_ctx Sum;
  46. bool Done;
  47. public:
  48. bool Add(const unsigned char *inbuf,unsigned long inlen);
  49. inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
  50. bool AddFD(int Fd,unsigned long Size);
  51. inline bool Add(const unsigned char *Beg,const unsigned char *End)
  52. {return Add(Beg,End-Beg);};
  53. SHA256SumValue Result();
  54. SHA256Summation();
  55. };
  56. #endif