sha.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #include <util/generic/ptr.h>
  3. #include <util/generic/strbuf.h>
  4. #include <util/system/types.h>
  5. #include <array>
  6. struct SHAstate_st;
  7. struct SHA256state_st;
  8. namespace NOpenSsl::NSha1 {
  9. constexpr size_t DIGEST_LENGTH = 20;
  10. using TDigest = std::array<ui8, DIGEST_LENGTH>;
  11. // not fragmented input
  12. TDigest Calc(const void* data, size_t dataSize);
  13. inline TDigest Calc(TStringBuf s) {
  14. return Calc(s.data(), s.length());
  15. }
  16. // fragmented input
  17. class TCalcer {
  18. public:
  19. TCalcer();
  20. ~TCalcer();
  21. void Update(const void* data, size_t dataSize);
  22. void Update(TStringBuf s) {
  23. Update(s.data(), s.length());
  24. }
  25. template <typename T>
  26. void UpdateWithPodValue(const T& value) {
  27. Update(&value, sizeof(value));
  28. }
  29. TDigest Final();
  30. private:
  31. THolder<SHAstate_st> Context;
  32. };
  33. }
  34. namespace NOpenSsl::NSha256 {
  35. constexpr size_t DIGEST_LENGTH = 32;
  36. using TDigest = std::array<ui8, DIGEST_LENGTH>;
  37. // not fragmented input
  38. TDigest Calc(const void* data, size_t dataSize);
  39. inline TDigest Calc(TStringBuf s) {
  40. return Calc(s.data(), s.length());
  41. }
  42. // fragmented input
  43. class TCalcer {
  44. public:
  45. TCalcer();
  46. ~TCalcer();
  47. void Update(const void* data, size_t dataSize);
  48. void Update(TStringBuf s) {
  49. Update(s.data(), s.length());
  50. }
  51. template <typename T>
  52. void UpdateWithPodValue(const T& value) {
  53. Update(&value, sizeof(value));
  54. }
  55. TDigest Final();
  56. private:
  57. THolder<SHA256state_st> Context;
  58. };
  59. }
  60. namespace NOpenSsl::NSha224 {
  61. constexpr size_t DIGEST_LENGTH = 28;
  62. using TDigest = std::array<ui8, DIGEST_LENGTH>;
  63. // not fragmented input
  64. TDigest Calc(const void* data, size_t dataSize);
  65. inline TDigest Calc(TStringBuf s) {
  66. return Calc(s.data(), s.length());
  67. }
  68. // fragmented input
  69. class TCalcer {
  70. public:
  71. TCalcer();
  72. ~TCalcer();
  73. void Update(const void* data, size_t dataSize);
  74. void Update(TStringBuf s) {
  75. Update(s.data(), s.length());
  76. }
  77. template <typename T>
  78. void UpdateWithPodValue(const T& value) {
  79. Update(&value, sizeof(value));
  80. }
  81. TDigest Final();
  82. private:
  83. THolder<SHA256state_st> Context;
  84. };
  85. }