AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
Acore::Impl::GenericHMAC< HashCreator, DigestLength > Class Template Reference

#include "HMAC.h"

Public Types

using Digest = std::array< uint8, DIGEST_LENGTH >
 

Public Member Functions

 GenericHMAC (uint8 const *seed, std::size_t len)
 
template<typename Container >
 GenericHMAC (Container const &container)
 
 GenericHMAC (GenericHMAC const &right)
 
 GenericHMAC (GenericHMAC &&right) noexcept
 
 ~GenericHMAC ()
 
GenericHMACoperator= (GenericHMAC const &right)
 
GenericHMACoperator= (GenericHMAC &&right) noexcept
 
void UpdateData (uint8 const *data, std::size_t len)
 
void UpdateData (std::string_view str)
 
void UpdateData (std::string const &str)
 
void UpdateData (char const *str)
 
template<typename Container >
void UpdateData (Container const &c)
 
void Finalize ()
 
Digest const & GetDigest () const
 

Static Public Member Functions

template<typename Container >
static Digest GetDigestOf (Container const &seed, uint8 const *data, std::size_t len)
 
template<typename Container , typename... Ts>
static auto GetDigestOf (Container const &seed, Ts &&... pack) -> std::enable_if_t<!(std::is_integral_v< std::decay_t< Ts > >||...), Digest >
 

Static Public Attributes

static constexpr std::size_t DIGEST_LENGTH = DigestLength
 

Private Attributes

EVP_MD_CTX * _ctx {}
 
EVP_PKEY * _key {}
 
Digest _digest {}
 

Detailed Description

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
class Acore::Impl::GenericHMAC< HashCreator, DigestLength >

Member Typedef Documentation

◆ Digest

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
using Acore::Impl::GenericHMAC< HashCreator, DigestLength >::Digest = std::array<uint8, DIGEST_LENGTH>

Constructor & Destructor Documentation

◆ GenericHMAC() [1/4]

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
Acore::Impl::GenericHMAC< HashCreator, DigestLength >::GenericHMAC ( uint8 const *  seed,
std::size_t  len 
)
inline
57 : _ctx(GenericHashImpl::MakeCTX()), _key(EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, nullptr, seed, len))
58 {
59 int result = EVP_DigestSignInit(_ctx, nullptr, HashCreator(), nullptr, _key);
60 ASSERT(result == 1);
61 }
#define ASSERT
Definition: Errors.h:68
static EVP_MD_CTX * MakeCTX() noexcept
Definition: CryptoHash.h:37
EVP_PKEY * _key
Definition: HMAC.h:132
EVP_MD_CTX * _ctx
Definition: HMAC.h:131

References Acore::Impl::GenericHMAC< HashCreator, DigestLength >::_ctx, Acore::Impl::GenericHMAC< HashCreator, DigestLength >::_key, and ASSERT.

◆ GenericHMAC() [2/4]

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
template<typename Container >
Acore::Impl::GenericHMAC< HashCreator, DigestLength >::GenericHMAC ( Container const &  container)
inline
64: GenericHMAC(std::data(container), std::size(container)) {}
GenericHMAC(uint8 const *seed, std::size_t len)
Definition: HMAC.h:57

◆ GenericHMAC() [3/4]

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
Acore::Impl::GenericHMAC< HashCreator, DigestLength >::GenericHMAC ( GenericHMAC< HashCreator, DigestLength > const &  right)
inline
67 {
68 *this = right;
69 }

◆ GenericHMAC() [4/4]

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
Acore::Impl::GenericHMAC< HashCreator, DigestLength >::GenericHMAC ( GenericHMAC< HashCreator, DigestLength > &&  right)
inlinenoexcept
72 {
73 *this = std::move(right);
74 }

◆ ~GenericHMAC()

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
Acore::Impl::GenericHMAC< HashCreator, DigestLength >::~GenericHMAC ( )
inline

Member Function Documentation

◆ Finalize()

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
void Acore::Impl::GenericHMAC< HashCreator, DigestLength >::Finalize ( )
inline
122 {
123 std::size_t length = DIGEST_LENGTH;
124 int result = EVP_DigestSignFinal(_ctx, _digest.data(), &length);
125 ASSERT(result == 1);
126 ASSERT(length == DIGEST_LENGTH);
127 }
Digest _digest
Definition: HMAC.h:133
static constexpr std::size_t DIGEST_LENGTH
Definition: HMAC.h:36

References Acore::Impl::GenericHMAC< HashCreator, DigestLength >::_ctx, Acore::Impl::GenericHMAC< HashCreator, DigestLength >::_digest, ASSERT, and Acore::Impl::GenericHMAC< HashCreator, DigestLength >::DIGEST_LENGTH.

Referenced by Acore::Impl::GenericHMAC< HashCreator, DigestLength >::GetDigestOf().

◆ GetDigest()

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
Digest const & Acore::Impl::GenericHMAC< HashCreator, DigestLength >::GetDigest ( ) const
inline

◆ GetDigestOf() [1/2]

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
template<typename Container , typename... Ts>
static auto Acore::Impl::GenericHMAC< HashCreator, DigestLength >::GetDigestOf ( Container const &  seed,
Ts &&...  pack 
) -> std::enable_if_t<!(std::is_integral_v<std::decay_t<Ts>> || ...), Digest>
inlinestatic
50 {
51 GenericHMAC hash(seed);
52 (hash.UpdateData(std::forward<Ts>(pack)), ...);
53 hash.Finalize();
54 return hash.GetDigest();
55 }

References Acore::Impl::GenericHMAC< HashCreator, DigestLength >::Finalize(), Acore::Impl::GenericHMAC< HashCreator, DigestLength >::GetDigest(), and Acore::Impl::GenericHMAC< HashCreator, DigestLength >::UpdateData().

◆ GetDigestOf() [2/2]

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
template<typename Container >
static Digest Acore::Impl::GenericHMAC< HashCreator, DigestLength >::GetDigestOf ( Container const &  seed,
uint8 const *  data,
std::size_t  len 
)
inlinestatic

◆ operator=() [1/2]

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
GenericHMAC & Acore::Impl::GenericHMAC< HashCreator, DigestLength >::operator= ( GenericHMAC< HashCreator, DigestLength > &&  right)
inlinenoexcept
98 {
99 if (this == &right)
100 return *this;
101
102 _ctx = std::exchange(right._ctx, GenericHashImpl::MakeCTX());
103 _key = std::exchange(right._key, EVP_PKEY_new());
104 _digest = std::exchange(right._digest, Digest{});
105 return *this;
106 }
std::array< uint8, DIGEST_LENGTH > Digest
Definition: HMAC.h:37

References Acore::Impl::GenericHMAC< HashCreator, DigestLength >::_ctx, Acore::Impl::GenericHMAC< HashCreator, DigestLength >::_digest, Acore::Impl::GenericHMAC< HashCreator, DigestLength >::_key, and Acore::Impl::GenericHashImpl::MakeCTX().

◆ operator=() [2/2]

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
GenericHMAC & Acore::Impl::GenericHMAC< HashCreator, DigestLength >::operator= ( GenericHMAC< HashCreator, DigestLength > const &  right)
inline
85 {
86 if (this == &right)
87 return *this;
88
89 int result = EVP_MD_CTX_copy_ex(_ctx, right._ctx);
90 ASSERT(result == 1);
91 _key = right._key; // EVP_PKEY uses reference counting internally, just copy the pointer
92 EVP_PKEY_up_ref(_key); // Bump reference count for PKEY, as every instance of this class holds two references to PKEY and destructor decrements it twice
93 _digest = right._digest;
94 return *this;
95 }

References Acore::Impl::GenericHMAC< HashCreator, DigestLength >::_ctx, Acore::Impl::GenericHMAC< HashCreator, DigestLength >::_digest, Acore::Impl::GenericHMAC< HashCreator, DigestLength >::_key, and ASSERT.

◆ UpdateData() [1/5]

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
void Acore::Impl::GenericHMAC< HashCreator, DigestLength >::UpdateData ( char const *  str)
inline
116{ UpdateData(std::string_view(str)); } /* explicit overload to avoid using the container template */
Definition: UpdateData.h:52

References Acore::Impl::GenericHMAC< HashCreator, DigestLength >::UpdateData().

Referenced by Acore::Impl::GenericHMAC< HashCreator, DigestLength >::UpdateData().

◆ UpdateData() [2/5]

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
template<typename Container >
void Acore::Impl::GenericHMAC< HashCreator, DigestLength >::UpdateData ( Container const &  c)
inline

◆ UpdateData() [3/5]

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
void Acore::Impl::GenericHMAC< HashCreator, DigestLength >::UpdateData ( std::string const &  str)
inline
115{ UpdateData(std::string_view(str)); } /* explicit overload to avoid using the container template */

References Acore::Impl::GenericHMAC< HashCreator, DigestLength >::UpdateData().

Referenced by Acore::Impl::GenericHMAC< HashCreator, DigestLength >::UpdateData().

◆ UpdateData() [4/5]

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
void Acore::Impl::GenericHMAC< HashCreator, DigestLength >::UpdateData ( std::string_view  str)
inline
114{ UpdateData(reinterpret_cast<uint8 const*>(str.data()), str.size()); }
std::uint8_t uint8
Definition: Define.h:109

References Acore::Impl::GenericHMAC< HashCreator, DigestLength >::UpdateData().

Referenced by Acore::Impl::GenericHMAC< HashCreator, DigestLength >::UpdateData().

◆ UpdateData() [5/5]

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
void Acore::Impl::GenericHMAC< HashCreator, DigestLength >::UpdateData ( uint8 const *  data,
std::size_t  len 
)
inline
109 {
110 int result = EVP_DigestSignUpdate(_ctx, data, len);
111 ASSERT(result == 1);
112 }

References Acore::Impl::GenericHMAC< HashCreator, DigestLength >::_ctx, and ASSERT.

Referenced by Acore::Impl::GenericHMAC< HashCreator, DigestLength >::GetDigestOf().

Member Data Documentation

◆ _ctx

◆ _digest

◆ _key

◆ DIGEST_LENGTH

template<GenericHashImpl::HashCreator HashCreator, std::size_t DigestLength>
constexpr std::size_t Acore::Impl::GenericHMAC< HashCreator, DigestLength >::DIGEST_LENGTH = DigestLength
staticconstexpr