sha256.h 1.8 KB

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