-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Hasher.hpp
62 lines (49 loc) · 1.69 KB
/
Hasher.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma once
#include <utility>
template<typename __HashTraits>
class Hasher {
public:
static constexpr size_t BlockSizeValue = __HashTraits::BlockSize;
static constexpr size_t DigestSizeValue = __HashTraits::DigestSize;
using DigestType = typename __HashTraits::DigestType;
private:
using ContextType = typename __HashTraits::ContextType;
ContextType _Ctx;
public:
Hasher(__HashTraits) :
_Ctx(__HashTraits::ContextCreate()) {}
template<typename... __Ts>
Hasher(__HashTraits, __Ts&&... Args) :
_Ctx(__HashTraits::ContextCreate(std::forward<__Ts>(Args)...)) {}
Hasher(const Hasher<__HashTraits>& Other) :
_Ctx(__HashTraits::ContextCopy(Other._Ctx)) {}
Hasher(Hasher<__HashTraits>&& Other) noexcept :
_Ctx(std::move(Other._Ctx)) {}
Hasher<__HashTraits>& operator=(const Hasher<__HashTraits>& Other) {
ContextType t = __HashTraits::ContextCopy(Other._Ctx);
__HashTraits::ContextDestroy(_Ctx);
_Ctx = std::move(t);
return *this;
}
Hasher<__HashTraits>& operator=(Hasher<__HashTraits>&& Other) noexcept {
_Ctx = std::move(Other._Ctx);
return *this;
}
constexpr size_t BlockSize() const noexcept {
return BlockSizeValue;
}
constexpr size_t DigestSize() const noexcept {
return DigestSizeValue;
}
void Update(const void* lpBuffer, size_t cbBuffer) noexcept {
__HashTraits::ContextUpdate(_Ctx, lpBuffer, cbBuffer);
}
DigestType Evaluate() const noexcept {
DigestType Digest;
__HashTraits::ContextEvaluate(_Ctx, Digest);
return Digest;
}
~Hasher() {
__HashTraits::ContextDestroy(_Ctx);
}
};