Crypto++  5.6.3
Free C++ class library of cryptographic schemes
crc.h
Go to the documentation of this file.
1 // crc.h - written and placed in the public domain by Wei Dai
2 
3 //! \file
4 //! \headerfile crc.h
5 //! \brief Classes for CRC-32 and CRC-32C checksum algorithm
6 
7 #ifndef CRYPTOPP_CRC32_H
8 #define CRYPTOPP_CRC32_H
9 
10 #include "cryptlib.h"
11 
12 NAMESPACE_BEGIN(CryptoPP)
13 
14 const word32 CRC32_NEGL = 0xffffffffL;
15 
16 #ifdef IS_LITTLE_ENDIAN
17 #define CRC32_INDEX(c) (c & 0xff)
18 #define CRC32_SHIFTED(c) (c >> 8)
19 #else
20 #define CRC32_INDEX(c) (c >> 24)
21 #define CRC32_SHIFTED(c) (c << 8)
22 #endif
23 
24 //! \brief CRC-32 Checksum Calculation
25 //! \details Uses CRC polynomial 0xEDB88320
26 class CRC32 : public HashTransformation
27 {
28 public:
29  CRYPTOPP_CONSTANT(DIGESTSIZE = 4)
30  CRC32();
31  void Update(const byte *input, size_t length);
32  void TruncatedFinal(byte *hash, size_t size);
33  unsigned int DigestSize() const {return DIGESTSIZE;}
34  static const char * StaticAlgorithmName() {return "CRC32";}
35  std::string AlgorithmName() const {return StaticAlgorithmName();}
36 
37  void UpdateByte(byte b) {m_crc = m_tab[CRC32_INDEX(m_crc) ^ b] ^ CRC32_SHIFTED(m_crc);}
38  byte GetCrcByte(size_t i) const {return ((byte *)&(m_crc))[i];}
39 
40 protected:
41  void Reset() {m_crc = CRC32_NEGL;}
42 
43 private:
44  static const word32 m_tab[256];
45  word32 m_crc;
46 };
47 
48 //! \brief CRC-32C Checksum Calculation
49 //! \details Uses CRC polynomial 0x82F63B78
50 class CRC32C : public HashTransformation
51 {
52 public:
53  CRYPTOPP_CONSTANT(DIGESTSIZE = 4)
54  CRC32C();
55  void Update(const byte *input, size_t length);
56  void TruncatedFinal(byte *hash, size_t size);
57  unsigned int DigestSize() const {return DIGESTSIZE;}
58  static const char * StaticAlgorithmName() {return "CRC32C";}
59  std::string AlgorithmName() const {return StaticAlgorithmName();}
60 
61  void UpdateByte(byte b) {m_crc = m_tab[CRC32_INDEX(m_crc) ^ b] ^ CRC32_SHIFTED(m_crc);}
62  byte GetCrcByte(size_t i) const {return ((byte *)&(m_crc))[i];}
63 
64 protected:
65  void Reset() {m_crc = CRC32_NEGL;}
66 
67 private:
68  static const word32 m_tab[256];
69  word32 m_crc;
70 };
71 
72 NAMESPACE_END
73 
74 #endif
Abstract base classes that provide a uniform interface to this library.
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition: crc.cpp:324
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: crc.h:33
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: crc.cpp:284
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition: crc.cpp:156
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: crc.h:35
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: crc.cpp:132
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:922
Crypto++ library namespace.
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: crc.h:57
CRC-32C Checksum Calculation.
Definition: crc.h:50
CRC-32 Checksum Calculation.
Definition: crc.h:26
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: crc.h:59