Crypto: Implement SHA1 hashing

This commit is contained in:
Peter Keresztes Schmidt 2021-05-30 22:43:16 +02:00
parent 8b87830e3e
commit 9a983bb321
5 changed files with 37 additions and 1 deletions

View File

@ -48,6 +48,7 @@ using Hash = openssl::GenericHashImpl<Algorithm>;
namespace zm {
namespace crypto {
using MD5 = impl::Hash<impl::HashAlgorithms::kMD5>;
using SHA1 = impl::Hash<impl::HashAlgorithms::kSHA1>;
}
}

View File

@ -28,7 +28,8 @@ namespace crypto {
namespace impl {
enum class HashAlgorithms {
kMD5
kMD5,
kSHA1
};
template<HashAlgorithms Algorithm>
@ -39,6 +40,11 @@ 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:

View File

@ -37,6 +37,11 @@ struct HashAlgorithmMapper<HashAlgorithms::kMD5> {
static constexpr gnutls_digest_algorithm_t algorithm = GNUTLS_DIG_MD5;
};
template<>
struct HashAlgorithmMapper<HashAlgorithms::kSHA1> {
static constexpr gnutls_digest_algorithm_t algorithm = GNUTLS_DIG_SHA1;
};
template<HashAlgorithms Algorithm>
class GenericHashImpl : public GenericHash<GenericHashImpl<Algorithm>, Algorithm> {
public:

View File

@ -48,6 +48,20 @@ struct HashAlgorithmMapper<HashAlgorithms::kMD5> {
#endif
};
template<>
struct HashAlgorithmMapper<HashAlgorithms::kSHA1> {
// TODO: Remove conditional once Jessie and CentOS 7 are deprecated
// This is needed since GCC 4.8 is faulty (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60199)
#if defined(__GNUC__) && __GNUC__ < 5
static HashCreator hash_creator() {
static constexpr HashCreator creator = EVP_sha1;
return creator;
}
#else
static constexpr HashCreator hash_creator = EVP_sha1;
#endif
};
template<HashAlgorithms Algorithm>
class GenericHashImpl : public GenericHash<GenericHashImpl<Algorithm>, Algorithm> {
public:

View File

@ -125,3 +125,13 @@ TEST_CASE("zm::crypto::MD5::GetDigestOf") {
0x7d, 0x3e});
}
}
TEST_CASE("zm::crypto::SHA1::GetDigestOf") {
using namespace zm::crypto;
std::array<uint8, 3> data = {'a', 'b', 'c'};
SHA1::Digest digest = SHA1::GetDigestOf(data);
REQUIRE(digest == SHA1::Digest{0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50,
0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d});
}