Crypto++ 8.9 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 #if (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 /// \brief Updates a CRC with additional input
37 /// \param b the additional input as a byte
38 void UpdateByte(byte b) {m_crc = m_tab[CRC32_INDEX(m_crc) ^ b] ^ CRC32_SHIFTED(m_crc);}
39
40 /// \brief Retrieves the i-th byte of the CRC
41 /// \param i the additional input as a byte
42 /// \return the byte at the i-th position
43 byte GetCrcByte(size_t i) const {return reinterpret_cast<const byte *>(&m_crc)[i];}
44
45 std::string AlgorithmProvider() const;
46
47 protected:
48 void Reset() {m_crc = CRC32_NEGL;}
49
50 private:
51 static const word32 m_tab[256];
52 word32 m_crc;
53 };
54
55 /// \brief CRC-32C Checksum Calculation
56 /// \details Uses CRC polynomial 0x82F63B78
57 /// \since Crypto++ 5.6.4
58 class CRC32C : public HashTransformation
59 {
60 public:
61 CRYPTOPP_CONSTANT(DIGESTSIZE = 4);
62 CRC32C();
63 void Update(const byte *input, size_t length);
64 void TruncatedFinal(byte *hash, size_t size);
65 unsigned int DigestSize() const {return DIGESTSIZE;}
66 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "CRC32C";}
67 std::string AlgorithmName() const {return StaticAlgorithmName();}
68
69 /// \brief Updates a CRC with additional input
70 /// \param b the additional input as a byte
71 void UpdateByte(byte b) {m_crc = m_tab[CRC32_INDEX(m_crc) ^ b] ^ CRC32_SHIFTED(m_crc);}
72
73 /// \brief Retrieves the i-th byte of the CRC
74 /// \param i the additional input as a byte
75 /// \return the byte at the i-th position
76 byte GetCrcByte(size_t i) const {return reinterpret_cast<const byte *>(&m_crc)[i];}
77
78 std::string AlgorithmProvider() const;
79
80 protected:
81 void Reset() {m_crc = CRC32_NEGL;}
82
83 private:
84 static const word32 m_tab[256];
85 word32 m_crc;
86 };
87
88 NAMESPACE_END
89
90 #endif
CRC32C
CRC-32C Checksum Calculation.
Definition: crc.h:59
CRC32C::TruncatedFinal
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition: crc.cpp:356
CRC32C::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: crc.h:67
CRC32C::UpdateByte
void UpdateByte(byte b)
Updates a CRC with additional input.
Definition: crc.h:71
CRC32C::GetCrcByte
byte GetCrcByte(size_t i) const
Retrieves the i-th byte of the CRC.
Definition: crc.h:76
CRC32C::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: crc.h:65
CRC32C::Update
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: crc.cpp:319
CRC32C::AlgorithmProvider
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition: crc.cpp:301
CRC32
CRC-32 Checksum Calculation.
Definition: crc.h:26
CRC32::DigestSize
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: crc.h:32
CRC32::GetCrcByte
byte GetCrcByte(size_t i) const
Retrieves the i-th byte of the CRC.
Definition: crc.h:43
CRC32::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: crc.h:34
CRC32::TruncatedFinal
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition: crc.cpp:178
CRC32::Update
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: crc.cpp:147
CRC32::AlgorithmProvider
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition: crc.cpp:133
CRC32::UpdateByte
void UpdateByte(byte b)
Updates a CRC with additional input.
Definition: crc.h:38
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1118
word32
unsigned int word32
32-bit unsigned datatype
Definition: config_int.h:72
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
CryptoPP
Crypto++ library namespace.