mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-13 21:36:00 +03:00
Refactored sha1 & md5 utils to share implementation and reduce code duplication.
This commit is contained in:
parent
fedec96a7e
commit
7023c0a8b4
@ -248,11 +248,9 @@ UTILS = \
|
||||
utils/geo_lookup.cc \
|
||||
utils/https_client.cc \
|
||||
utils/ip_tree.cc \
|
||||
utils/md5.cc \
|
||||
utils/msc_tree.cc \
|
||||
utils/random.cc \
|
||||
utils/regex.cc \
|
||||
utils/sha1.cc \
|
||||
utils/system.cc \
|
||||
utils/shared_files.cc
|
||||
|
||||
|
@ -1,40 +0,0 @@
|
||||
|
||||
|
||||
#include "src/utils/md5.h"
|
||||
#include "mbedtls/md5.h"
|
||||
|
||||
namespace modsecurity {
|
||||
namespace Utils {
|
||||
|
||||
|
||||
std::string Md5::hexdigest(const std::string& input) {
|
||||
unsigned char digest[16];
|
||||
|
||||
mbedtls_md5(reinterpret_cast<const unsigned char *>(input.c_str()),
|
||||
input.size(), digest);
|
||||
|
||||
char buf[33];
|
||||
for (int i = 0; i < 16; i++) {
|
||||
sprintf(buf+i*2, "%02x", digest[i]);
|
||||
}
|
||||
|
||||
return std::string(buf, 32);
|
||||
}
|
||||
|
||||
|
||||
std::string Md5::digest(const std::string& input) {
|
||||
unsigned char output[16];
|
||||
std::string ret;
|
||||
|
||||
mbedtls_md5(reinterpret_cast<const unsigned char *>(input.c_str()),
|
||||
input.size(), output);
|
||||
|
||||
ret.assign(reinterpret_cast<const char *>(output), 16);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Utils
|
||||
} // namespace modsecurity
|
||||
|
@ -13,29 +13,20 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "modsecurity/actions/action.h"
|
||||
#include "src/actions/transformations/transformation.h"
|
||||
|
||||
#ifndef SRC_UTILS_MD5_H_
|
||||
#define SRC_UTILS_MD5_H_
|
||||
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include "src/utils/sha1.h"
|
||||
#include "mbedtls/md5.h"
|
||||
#include <string>
|
||||
|
||||
namespace modsecurity {
|
||||
namespace Utils {
|
||||
namespace modsecurity::Utils {
|
||||
|
||||
class Md5 {
|
||||
public:
|
||||
Md5() { }
|
||||
|
||||
static std::string hexdigest(const std::string& input);
|
||||
static std::string digest(const std::string& input);
|
||||
class Md5 : public DigestImpl<&mbedtls_md5, 16> {
|
||||
};
|
||||
|
||||
} // namespace Utils
|
||||
} // namespace modsecurity
|
||||
|
||||
} // namespace modsecurity::Utils
|
||||
|
||||
#endif // SRC_UTILS_MD5_H_
|
@ -1,60 +0,0 @@
|
||||
/*
|
||||
* ModSecurity, http://www.modsecurity.org/
|
||||
* Copyright (c) 2015 - 2021 Trustwave Holdings, Inc. (http://www.trustwave.com/)
|
||||
*
|
||||
* You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* If any of the files related to licensing are missing or if you have any
|
||||
* other questions related to licensing please contact Trustwave Holdings, Inc.
|
||||
* directly using the email address security@modsecurity.org.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "src/utils/sha1.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
namespace modsecurity {
|
||||
namespace Utils {
|
||||
|
||||
|
||||
std::string Sha1::hexdigest(const std::string& input) {
|
||||
unsigned char digest[20] = { 0 };
|
||||
static const char* const lut = "0123456789abcdef";
|
||||
|
||||
mbedtls_sha1(reinterpret_cast<const unsigned char *>(input.c_str()),
|
||||
input.size(), digest);
|
||||
std::string a;
|
||||
|
||||
for (int i = 0; i < 20; i++) {
|
||||
const unsigned char c = digest[i];
|
||||
a.push_back(lut[c >> 4]);
|
||||
a.push_back(lut[c & 15]);
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
std::string Sha1::digest(const std::string& input) {
|
||||
unsigned char output[20];
|
||||
std::string ret;
|
||||
|
||||
mbedtls_sha1(reinterpret_cast<const unsigned char *>(input.c_str()),
|
||||
input.size(), output);
|
||||
|
||||
ret.assign(reinterpret_cast<const char *>(output), 20);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Utils
|
||||
} // namespace modsecurity
|
||||
|
@ -13,26 +13,63 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef SRC_UTILS_SHA1_H_
|
||||
#define SRC_UTILS_SHA1_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
|
||||
namespace modsecurity {
|
||||
namespace Utils {
|
||||
#include "src/utils/string.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
|
||||
class Sha1 {
|
||||
namespace modsecurity::Utils {
|
||||
|
||||
|
||||
using DigestOp = int (*)(const unsigned char *, size_t, unsigned char []);
|
||||
|
||||
|
||||
template<DigestOp digestOp, int DigestSize>
|
||||
class DigestImpl {
|
||||
public:
|
||||
Sha1() { }
|
||||
|
||||
static std::string hexdigest(const std::string& input);
|
||||
static std::string digest(const std::string& input);
|
||||
static std::string digest(const std::string& input) {
|
||||
return digestHelper(input, [](const auto digest) {
|
||||
return std::string(digest);
|
||||
});
|
||||
}
|
||||
|
||||
static void digestReplace(std::string& value) {
|
||||
digestHelper(value, [&value](const auto digest) mutable {
|
||||
value = digest;
|
||||
});
|
||||
}
|
||||
|
||||
static std::string hexdigest(const std::string &input) {
|
||||
return digestHelper(input, [](const auto digest) {
|
||||
return utils::string::string_to_hex(digest);
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
template<typename ConvertOp>
|
||||
static auto digestHelper(const std::string &input,
|
||||
ConvertOp convertOp) -> auto {
|
||||
char digest[DigestSize];
|
||||
|
||||
auto ret = digestOp(reinterpret_cast<const unsigned char *>(input.c_str()),
|
||||
input.size(), reinterpret_cast<unsigned char *>(digest));
|
||||
assert(ret == 0);
|
||||
|
||||
return convertOp(std::string_view(digest, DigestSize));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Utils
|
||||
} // namespace modsecurity
|
||||
|
||||
class Sha1 : public DigestImpl<&mbedtls_sha1, 20> {
|
||||
};
|
||||
|
||||
|
||||
} // namespace modsecurity::Utils
|
||||
|
||||
#endif // SRC_UTILS_SHA1_H_
|
||||
|
Loading…
x
Reference in New Issue
Block a user