-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathzm_crypto_generics.h
More file actions
104 lines (86 loc) · 2.84 KB
/
Copy pathzm_crypto_generics.h
File metadata and controls
104 lines (86 loc) · 2.84 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ZONEMINDER_SRC_ZM_CRYPTO_GENERICS_H_
#define ZONEMINDER_SRC_ZM_CRYPTO_GENERICS_H_
#include "zm_define.h"
#include "zm_utils.h"
#include <array>
#include <cstring>
namespace zm {
namespace crypto {
namespace impl {
enum class HashAlgorithms { kMD5, kSHA1 };
template <HashAlgorithms Algorithm>
struct HashAlgorithm;
template <>
struct HashAlgorithm<HashAlgorithms::kMD5> {
static constexpr size_t digest_length = 16;
};
template <>
struct HashAlgorithm<HashAlgorithms::kSHA1> {
static constexpr size_t digest_length = 20;
};
template <typename Impl, HashAlgorithms Algorithm>
class GenericHash {
public:
static constexpr size_t DIGEST_LENGTH = HashAlgorithm<Algorithm>::digest_length;
using Digest = std::array<uint8, DIGEST_LENGTH>;
static Digest GetDigestOf(uint8 const *data, size_t len) {
Impl hash;
hash.UpdateData(data, len);
hash.Finalize();
return hash.GetDigest();
}
template <typename... Ts>
static Digest GetDigestOf(Ts &&...pack) {
Impl hash;
UpdateData(hash, std::forward<Ts>(pack)...);
hash.Finalize();
return hash.GetDigest();
}
void UpdateData(const uint8 *data, size_t length) {
static_cast<Impl &>(*this).DoUpdateData(data, length);
}
void UpdateData(const std::string &str) {
UpdateData(reinterpret_cast<const uint8 *>(str.c_str()), str.size());
}
void UpdateData(const char *str) {
UpdateData(reinterpret_cast<const uint8 *>(str), strlen(str));
}
template <typename Container>
void UpdateData(Container const &c) {
UpdateData(zm::data(c), zm::size(c));
}
void Finalize() { static_cast<Impl &>(*this).DoFinalize(); }
const Digest &GetDigest() const { return digest_; }
protected:
Digest digest_ = {};
private:
template <typename T>
static void UpdateData(Impl &hash, T const &data) {
hash.UpdateData(data);
}
template <typename T, typename... TRest>
static void UpdateData(Impl &hash, T const &data, TRest &&...rest) {
hash.UpdateData(data);
UpdateData(hash, std::forward<TRest>(rest)...);
}
};
} // namespace impl
} // namespace crypto
} // namespace zm
#endif // ZONEMINDER_SRC_ZM_CRYPTO_GENERICS_H_