You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

72 lines
1.8 KiB

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