Skip to content

Commit a299c00

Browse files
committed
Refactored sha1 & md5 utils to share implementation and reduce code duplication.
1 parent 4a4d4b3 commit a299c00

File tree

5 files changed

+55
-129
lines changed

5 files changed

+55
-129
lines changed

src/Makefile.am

-2
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,9 @@ UTILS = \
248248
utils/geo_lookup.cc \
249249
utils/https_client.cc \
250250
utils/ip_tree.cc \
251-
utils/md5.cc \
252251
utils/msc_tree.cc \
253252
utils/random.cc \
254253
utils/regex.cc \
255-
utils/sha1.cc \
256254
utils/system.cc \
257255
utils/shared_files.cc
258256

src/utils/md5.cc

-40
This file was deleted.

src/utils/md5.h

+7-16
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,20 @@
1313
*
1414
*/
1515

16-
#include <string>
17-
18-
#include "modsecurity/actions/action.h"
19-
#include "src/actions/transformations/transformation.h"
20-
2116
#ifndef SRC_UTILS_MD5_H_
2217
#define SRC_UTILS_MD5_H_
2318

24-
#include <cstring>
25-
#include <iostream>
19+
#include "src/utils/sha1.h"
20+
#include "mbedtls/md5.h"
21+
#include <string>
2622

27-
namespace modsecurity {
28-
namespace Utils {
23+
namespace modsecurity::Utils {
2924

30-
class Md5 {
31-
public:
32-
Md5() { }
3325

34-
static std::string hexdigest(const std::string& input);
35-
static std::string digest(const std::string& input);
26+
class Md5 : public DigestImpl<mbedtls_md5, 16> {
3627
};
3728

38-
} // namespace Utils
39-
} // namespace modsecurity
29+
30+
} // namespace modsecurity::Utils
4031

4132
#endif // SRC_UTILS_MD5_H_

src/utils/sha1.cc

-60
This file was deleted.

src/utils/sha1.h

+48-11
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,63 @@
1313
*
1414
*/
1515

16-
1716
#ifndef SRC_UTILS_SHA1_H_
1817
#define SRC_UTILS_SHA1_H_
1918

20-
#include <algorithm>
21-
#include <iostream>
2219
#include <string>
20+
#include <cassert>
21+
22+
#include "src/utils/string.h"
23+
#include "mbedtls/sha1.h"
24+
25+
namespace modsecurity::Utils {
26+
2327

24-
namespace modsecurity {
25-
namespace Utils {
28+
using DigestOp = int (*)(const unsigned char *, size_t, unsigned char []);
2629

27-
class Sha1 {
30+
31+
template<DigestOp digestOp, int DigestSize>
32+
class DigestImpl {
2833
public:
29-
Sha1() { }
3034

31-
static std::string hexdigest(const std::string& input);
32-
static std::string digest(const std::string& input);
35+
static std::string digest(const std::string& input) {
36+
return digestHelper(input, [](const auto digest) {
37+
return std::string(digest);
38+
});
39+
}
40+
41+
static void digestReplace(std::string& value) {
42+
digestHelper(value, [&value](const auto digest) mutable {
43+
value = digest;
44+
});
45+
}
46+
47+
static std::string hexdigest(const std::string &input) {
48+
return digestHelper(input, [](const auto digest) {
49+
return utils::string::string_to_hex(digest);
50+
});
51+
}
52+
53+
private:
54+
55+
template<typename ConvertOp>
56+
static auto digestHelper(const std::string &input,
57+
ConvertOp convertOp) -> auto {
58+
char digest[DigestSize];
59+
60+
auto ret = digestOp(reinterpret_cast<const unsigned char *>(input.c_str()),
61+
input.size(), reinterpret_cast<unsigned char *>(digest));
62+
assert(ret == 0);
63+
64+
return convertOp(std::string_view(digest, DigestSize));
65+
}
3366
};
3467

35-
} // namespace Utils
36-
} // namespace modsecurity
68+
69+
class Sha1 : public DigestImpl<mbedtls_sha1, 20> {
70+
};
71+
72+
73+
} // namespace modsecurity::Utils
3774

3875
#endif // SRC_UTILS_SHA1_H_

0 commit comments

Comments
 (0)