diff --git a/src/zm_crypt.h b/src/zm_crypt.h index 8803c0e54..b495dd536 100644 --- a/src/zm_crypt.h +++ b/src/zm_crypt.h @@ -48,6 +48,7 @@ using Hash = openssl::GenericHashImpl; namespace zm { namespace crypto { using MD5 = impl::Hash; +using SHA1 = impl::Hash; } } diff --git a/src/zm_crypto_generics.h b/src/zm_crypto_generics.h index 4ceb92022..ce5fb090f 100644 --- a/src/zm_crypto_generics.h +++ b/src/zm_crypto_generics.h @@ -28,7 +28,8 @@ namespace crypto { namespace impl { enum class HashAlgorithms { - kMD5 + kMD5, + kSHA1 }; template @@ -39,6 +40,11 @@ struct HashAlgorithm { static constexpr size_t digest_length = 16; }; +template<> +struct HashAlgorithm { + static constexpr size_t digest_length = 20; +}; + template class GenericHash { public: diff --git a/src/zm_crypto_gnutls.h b/src/zm_crypto_gnutls.h index e727144a0..d2c51f35d 100644 --- a/src/zm_crypto_gnutls.h +++ b/src/zm_crypto_gnutls.h @@ -37,6 +37,11 @@ struct HashAlgorithmMapper { static constexpr gnutls_digest_algorithm_t algorithm = GNUTLS_DIG_MD5; }; +template<> +struct HashAlgorithmMapper { + static constexpr gnutls_digest_algorithm_t algorithm = GNUTLS_DIG_SHA1; +}; + template class GenericHashImpl : public GenericHash, Algorithm> { public: diff --git a/src/zm_crypto_openssl.h b/src/zm_crypto_openssl.h index 87f96eedd..667142531 100644 --- a/src/zm_crypto_openssl.h +++ b/src/zm_crypto_openssl.h @@ -48,6 +48,20 @@ struct HashAlgorithmMapper { #endif }; +template<> +struct HashAlgorithmMapper { +// 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 class GenericHashImpl : public GenericHash, Algorithm> { public: diff --git a/tests/zm_crypt.cpp b/tests/zm_crypt.cpp index ba432d695..94004527c 100644 --- a/tests/zm_crypt.cpp +++ b/tests/zm_crypt.cpp @@ -125,3 +125,13 @@ TEST_CASE("zm::crypto::MD5::GetDigestOf") { 0x7d, 0x3e}); } } + +TEST_CASE("zm::crypto::SHA1::GetDigestOf") { + using namespace zm::crypto; + std::array 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}); +}