sha256.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include <stdint.h>
  19. using std::string;
  20. using std::min;
  21. class SHA256Summation;
  22. class SHA256SumValue
  23. {
  24. friend class SHA256Summation;
  25. unsigned char Sum[32];
  26. public:
  27. // Accessors
  28. bool operator ==(const SHA256SumValue &rhs) const;
  29. string Value() const;
  30. inline void Value(unsigned char S[32])
  31. {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
  32. inline operator string() const {return Value();};
  33. bool Set(string Str);
  34. inline void Set(unsigned char S[32])
  35. {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
  36. SHA256SumValue(string Str);
  37. SHA256SumValue();
  38. };
  39. struct sha256_ctx {
  40. uint32_t count[2];
  41. uint32_t state[8];
  42. uint8_t buf[128];
  43. };
  44. class SHA256Summation
  45. {
  46. struct sha256_ctx Sum;
  47. bool Done;
  48. public:
  49. bool Add(const unsigned char *inbuf,unsigned long inlen);
  50. inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
  51. bool AddFD(int Fd,unsigned long Size);
  52. inline bool Add(const unsigned char *Beg,const unsigned char *End)
  53. {return Add(Beg,End-Beg);};
  54. SHA256SumValue Result();
  55. SHA256Summation();
  56. };
  57. #endif