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