Crypto++ 8.6 Free C++ class library of cryptographic schemes

trunhash.h
Go to the documentation of this file.
1 // trunhash.h - originally written and placed in the public domain by Wei Dai
2
3 /// \file trunhash.h
4 /// \brief Classes for truncated hashes
5
6 #ifndef CRYPTOPP_TRUNHASH_H
7 #define CRYPTOPP_TRUNHASH_H
8
9 #include "cryptlib.h"
10
11 NAMESPACE_BEGIN(CryptoPP)
12
13 /// \brief Null hash
14 /// \details A null hash that conforms to HashTransformation interface
15 class NullHash : public HashTransformation
16 {
17 public:
18 void Update(const byte *input, size_t length)
19 {CRYPTOPP_UNUSED(input);CRYPTOPP_UNUSED(length);}
20 unsigned int DigestSize() const
21 {return 0;}
22 void TruncatedFinal(byte *digest, size_t digestSize)
23 {CRYPTOPP_UNUSED(digest);CRYPTOPP_UNUSED(digestSize);}
24 bool TruncatedVerify(const byte *digest, size_t digestLength)
25 {CRYPTOPP_UNUSED(digest);CRYPTOPP_UNUSED(digestLength);return true;}
26 };
27
28 /// \brief Construct new HashModule with smaller digest size from an existing one
29 /// \tparam T HashTransformation derived class
30 template <class T>
31 class TruncatedHashTemplate : public HashTransformation
32 {
33 public:
34 /// \brief Construct a TruncatedHashTemplate
35 TruncatedHashTemplate(T hm, unsigned int digestSize)
36 : m_hm(hm), m_digestSize(digestSize) {}
37 /// \brief Construct a TruncatedHashTemplate
38 TruncatedHashTemplate(const byte *key, size_t keyLength, unsigned int digestSize)
39 : m_hm(key, keyLength), m_digestSize(digestSize) {}
40 /// \brief Construct a TruncatedHashTemplate
41 TruncatedHashTemplate(size_t digestSize)
42 : m_digestSize(digestSize) {}
43
44 void Restart()
45 {m_hm.Restart();}
46 void Update(const byte *input, size_t length)
47 {m_hm.Update(input, length);}
48 unsigned int DigestSize() const {return m_digestSize;}
49 void TruncatedFinal(byte *digest, size_t digestSize)
50 {m_hm.TruncatedFinal(digest, digestSize);}
51 bool TruncatedVerify(const byte *digest, size_t digestLength)
52 {return m_hm.TruncatedVerify(digest, digestLength);}
53
54 private:
55 T m_hm;
56 unsigned int m_digestSize;
57 };
58
59 typedef TruncatedHashTemplate<HashTransformation &> TruncatedHashModule;
60
61 NAMESPACE_END
62
63 #endif
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1118
NullHash
Null hash.
Definition: trunhash.h:16
NullHash::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: trunhash.h:20
NullHash::TruncatedVerify
bool TruncatedVerify(const byte *digest, size_t digestLength)
Verifies the hash of the current message.
Definition: trunhash.h:24
NullHash::TruncatedFinal
void TruncatedFinal(byte *digest, size_t digestSize)
Computes the hash of the current message.
Definition: trunhash.h:22
NullHash::Update
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: trunhash.h:18
TruncatedHashTemplate
Construct new HashModule with smaller digest size from an existing one.
Definition: trunhash.h:32
TruncatedHashTemplate::Restart
void Restart()
Restart the hash.
Definition: trunhash.h:44
TruncatedHashTemplate::TruncatedHashTemplate
TruncatedHashTemplate(const byte *key, size_t keyLength, unsigned int digestSize)
Construct a TruncatedHashTemplate.
Definition: trunhash.h:38
TruncatedHashTemplate::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: trunhash.h:48
TruncatedHashTemplate::Update
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: trunhash.h:46
TruncatedHashTemplate::TruncatedHashTemplate
TruncatedHashTemplate(size_t digestSize)
Construct a TruncatedHashTemplate.
Definition: trunhash.h:41
TruncatedHashTemplate::TruncatedFinal
void TruncatedFinal(byte *digest, size_t digestSize)
Computes the hash of the current message.
Definition: trunhash.h:49
TruncatedHashTemplate::TruncatedHashTemplate
TruncatedHashTemplate(T hm, unsigned int digestSize)
Construct a TruncatedHashTemplate.
Definition: trunhash.h:35
TruncatedHashTemplate::TruncatedVerify
bool TruncatedVerify(const byte *digest, size_t digestLength)
Verifies the hash of the current message.
Definition: trunhash.h:51
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
CryptoPP
Crypto++ library namespace.