- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# hmac.h

    00001 // hmac.h - written and placed in the public domain by Wei Dai
    00002
    00003 #ifndef CRYPTOPP_HMAC_H
    00004 #define CRYPTOPP_HMAC_H
    00005
    00006 #include "seckey.h"
    00007 #include "secblock.h"
    00008
    00009 NAMESPACE_BEGIN(CryptoPP)
    00010
    00011 //! _
    00012 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0, INT_MAX>, public MessageAuthenticationCode
    00013 {
    00014 public:
    00015         HMAC_Base() : m_innerHashKeyed(false) {}
    00016         void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
    00017
    00018         void Restart();
    00019         void Update(const byte *input, size_t length);
    00020         void TruncatedFinal(byte *mac, size_t size);
    00021         unsigned int OptimalBlockSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().OptimalBlockSize();}
    00022         unsigned int DigestSize() const {return const_cast<HMAC_Base*>(this)->AccessHash().DigestSize();}
    00023
    00024 protected:
    00025         virtual HashTransformation & AccessHash() =0;
    00026         byte * AccessIpad() {return m_buf;}
    00027         byte * AccessOpad() {return m_buf + AccessHash().BlockSize();}
    00028         byte * AccessInnerHash() {return m_buf + 2*AccessHash().BlockSize();}
    00029
    00030 private:
    00031         void KeyInnerHash();
    00032
    00033         SecByteBlock m_buf;
    00034         bool m_innerHashKeyed;
    00035 };
    00036
    00037 //! <a href="http://www.weidai.com/scan-mirror/mac.html#HMAC">HMAC</a>
    00038 /*! HMAC(K, text) = H(K XOR opad, H(K XOR ipad, text)) */
    00039 template <class T>
    00040 class HMAC : public MessageAuthenticationCodeImpl<HMAC_Base, HMAC<T> >
    00041 {
    00042 public:
    00043         CRYPTOPP_CONSTANT(DIGESTSIZE=T::DIGESTSIZE)
    00044         CRYPTOPP_CONSTANT(BLOCKSIZE=T::BLOCKSIZE)
    00045
    00046         HMAC() {}
    00047         HMAC(const byte *key, size_t length=HMAC_Base::DEFAULT_KEYLENGTH)
    00048                 {this->SetKey(key, length);}
    00049
    00050         static std::string StaticAlgorithmName() {return std::string("HMAC(") + T::StaticAlgorithmName() + ")";}
    00051         std::string AlgorithmName() const {return std::string("HMAC(") + m_hash.AlgorithmName() + ")";}
    00052
    00053 private:
    00054         HashTransformation & AccessHash() {return m_hash;}
    00055
    00056         T m_hash;
    00057 };
    00058
    00059 NAMESPACE_END
    00060
    00061 #endif