sha512.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: sha512.h,v 1.3 2001/05/07 05:05:47 jgg Exp $
  4. /* ######################################################################
  5. SHA512SumValue - Storage for a SHA-512 hash.
  6. SHA512Summation - SHA-512 Secure Hash Algorithm.
  7. This is a C++ interface to a set of SHA512Sum functions, that mirrors
  8. the equivalent MD5 & SHA1 classes.
  9. ##################################################################### */
  10. /*}}}*/
  11. #ifndef APTPKG_SHA512_H
  12. #define APTPKG_SHA512_H
  13. #include <string>
  14. #include <cstring>
  15. #include <algorithm>
  16. #include <stdint.h>
  17. #include "sha2.h"
  18. using std::string;
  19. using std::min;
  20. class SHA512Summation;
  21. class SHA512SumValue
  22. {
  23. friend class SHA512Summation;
  24. unsigned char Sum[64];
  25. public:
  26. // Accessors
  27. bool operator ==(const SHA512SumValue &rhs) const;
  28. string Value() const;
  29. inline void Value(unsigned char S[64])
  30. {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
  31. inline operator string() const {return Value();};
  32. bool Set(string Str);
  33. inline void Set(unsigned char S[64])
  34. {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
  35. SHA512SumValue(string Str);
  36. SHA512SumValue();
  37. };
  38. class SHA512Summation
  39. {
  40. SHA512_CTX ctx;
  41. unsigned char Sum[64];
  42. bool Done;
  43. public:
  44. bool Add(const unsigned char *inbuf,unsigned long inlen);
  45. inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
  46. bool AddFD(int Fd,unsigned long Size);
  47. inline bool Add(const unsigned char *Beg,const unsigned char *End)
  48. {return Add(Beg,End-Beg);};
  49. SHA512SumValue Result();
  50. SHA512Summation();
  51. };
  52. #endif