sha2.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: sha512.h,v 1.3 2001/05/07 05:05:47 jgg Exp $
  4. /* ######################################################################
  5. SHA{512,256}SumValue - Storage for a SHA-{512,256} hash.
  6. SHA{512,256}Summation - SHA-{512,256} Secure Hash Algorithm.
  7. This is a C++ interface to a set of SHA{512,256}Sum functions, that mirrors
  8. the equivalent MD5 & SHA1 classes.
  9. ##################################################################### */
  10. /*}}}*/
  11. #ifndef APTPKG_SHA2_H
  12. #define APTPKG_SHA2_H
  13. #include <string>
  14. #include <cstring>
  15. #include <algorithm>
  16. #include <stdint.h>
  17. #include "sha2_internal.h"
  18. using std::string;
  19. using std::min;
  20. // SHA512
  21. class SHA512Summation;
  22. class SHA512SumValue
  23. {
  24. friend class SHA512Summation;
  25. unsigned char Sum[64];
  26. public:
  27. // Accessors
  28. bool operator ==(const SHA512SumValue &rhs) const;
  29. string Value() const;
  30. inline void Value(unsigned char S[64])
  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[64])
  35. {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
  36. SHA512SumValue(string Str);
  37. SHA512SumValue();
  38. };
  39. class SHA512Summation
  40. {
  41. SHA512_CTX ctx;
  42. unsigned char Sum[64];
  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. SHA512SumValue Result();
  51. SHA512Summation();
  52. };
  53. // SHA256
  54. class SHA256Summation;
  55. class SHA256SumValue
  56. {
  57. friend class SHA256Summation;
  58. unsigned char Sum[32];
  59. public:
  60. // Accessors
  61. bool operator ==(const SHA256SumValue &rhs) const;
  62. string Value() const;
  63. inline void Value(unsigned char S[32])
  64. {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
  65. inline operator string() const {return Value();};
  66. bool Set(string Str);
  67. inline void Set(unsigned char S[32])
  68. {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
  69. SHA256SumValue(string Str);
  70. SHA256SumValue();
  71. };
  72. class SHA256Summation
  73. {
  74. SHA256_CTX ctx;
  75. unsigned char Sum[32];
  76. bool Done;
  77. public:
  78. bool Add(const unsigned char *inbuf,unsigned long inlen);
  79. inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
  80. bool AddFD(int Fd,unsigned long Size);
  81. inline bool Add(const unsigned char *Beg,const unsigned char *End)
  82. {return Add(Beg,End-Beg);};
  83. SHA256SumValue Result();
  84. SHA256Summation();
  85. };
  86. #endif