Hasher.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #pragma once
  2. #include <utility>
  3. template<typename __HashTraits>
  4. class Hasher {
  5. public:
  6. static constexpr size_t BlockSizeValue = __HashTraits::BlockSize;
  7. static constexpr size_t DigestSizeValue = __HashTraits::DigestSize;
  8. using DigestType = typename __HashTraits::DigestType;
  9. private:
  10. using ContextType = typename __HashTraits::ContextType;
  11. ContextType _Ctx;
  12. public:
  13. Hasher(__HashTraits) :
  14. _Ctx(__HashTraits::ContextCreate()) {}
  15. template<typename... __Ts>
  16. Hasher(__HashTraits, __Ts&&... Args) :
  17. _Ctx(__HashTraits::ContextCreate(std::forward<__Ts>(Args)...)) {}
  18. Hasher(const Hasher<__HashTraits>& Other) :
  19. _Ctx(__HashTraits::ContextCopy(Other._Ctx)) {}
  20. Hasher(Hasher<__HashTraits>&& Other) noexcept :
  21. _Ctx(std::move(Other._Ctx)) {}
  22. Hasher<__HashTraits>& operator=(const Hasher<__HashTraits>& Other) {
  23. ContextType t = __HashTraits::ContextCopy(Other._Ctx);
  24. __HashTraits::ContextDestroy(_Ctx);
  25. _Ctx = std::move(t);
  26. return *this;
  27. }
  28. Hasher<__HashTraits>& operator=(Hasher<__HashTraits>&& Other) noexcept {
  29. _Ctx = std::move(Other._Ctx);
  30. return *this;
  31. }
  32. constexpr size_t BlockSize() const noexcept {
  33. return BlockSizeValue;
  34. }
  35. constexpr size_t DigestSize() const noexcept {
  36. return DigestSizeValue;
  37. }
  38. void Update(const void* lpBuffer, size_t cbBuffer) noexcept {
  39. __HashTraits::ContextUpdate(_Ctx, lpBuffer, cbBuffer);
  40. }
  41. DigestType Evaluate() const noexcept {
  42. DigestType Digest;
  43. __HashTraits::ContextEvaluate(_Ctx, Digest);
  44. return Digest;
  45. }
  46. ~Hasher() {
  47. __HashTraits::ContextDestroy(_Ctx);
  48. }
  49. };