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