Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:55

0001 // crc.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file crc.h

0004 /// \brief Classes for CRC-32 and CRC-32C checksum algorithm

0005 
0006 #ifndef CRYPTOPP_CRC32_H
0007 #define CRYPTOPP_CRC32_H
0008 
0009 #include "cryptlib.h"
0010 
0011 NAMESPACE_BEGIN(CryptoPP)
0012 
0013 const word32 CRC32_NEGL = 0xffffffffL;
0014 
0015 #if (CRYPTOPP_LITTLE_ENDIAN)
0016 #define CRC32_INDEX(c) (c & 0xff)
0017 #define CRC32_SHIFTED(c) (c >> 8)
0018 #else
0019 #define CRC32_INDEX(c) (c >> 24)
0020 #define CRC32_SHIFTED(c) (c << 8)
0021 #endif
0022 
0023 /// \brief CRC-32 Checksum Calculation

0024 /// \details Uses CRC polynomial 0xEDB88320

0025 class CRC32 : public HashTransformation
0026 {
0027 public:
0028     CRYPTOPP_CONSTANT(DIGESTSIZE = 4);
0029     CRC32();
0030     void Update(const byte *input, size_t length);
0031     void TruncatedFinal(byte *hash, size_t size);
0032     unsigned int DigestSize() const {return DIGESTSIZE;}
0033     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "CRC32";}
0034     std::string AlgorithmName() const {return StaticAlgorithmName();}
0035 
0036     /// \brief Updates a CRC with additional input

0037     /// \param b the additional input as a byte

0038     void UpdateByte(byte b) {m_crc = m_tab[CRC32_INDEX(m_crc) ^ b] ^ CRC32_SHIFTED(m_crc);}
0039 
0040     /// \brief Retrieves the i-th byte of the CRC

0041     /// \param i the additional input as a byte

0042     /// \return the byte at the i-th position

0043     byte GetCrcByte(size_t i) const {return reinterpret_cast<const byte *>(&m_crc)[i];}
0044 
0045     std::string AlgorithmProvider() const;
0046 
0047 protected:
0048     void Reset() {m_crc = CRC32_NEGL;}
0049 
0050 private:
0051     static const word32 m_tab[256];
0052     word32 m_crc;
0053 };
0054 
0055 /// \brief CRC-32C Checksum Calculation

0056 /// \details Uses CRC polynomial 0x82F63B78

0057 /// \since Crypto++ 5.6.4

0058 class CRC32C : public HashTransformation
0059 {
0060 public:
0061     CRYPTOPP_CONSTANT(DIGESTSIZE = 4);
0062     CRC32C();
0063     void Update(const byte *input, size_t length);
0064     void TruncatedFinal(byte *hash, size_t size);
0065     unsigned int DigestSize() const {return DIGESTSIZE;}
0066     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "CRC32C";}
0067     std::string AlgorithmName() const {return StaticAlgorithmName();}
0068 
0069     /// \brief Updates a CRC with additional input

0070     /// \param b the additional input as a byte

0071     void UpdateByte(byte b) {m_crc = m_tab[CRC32_INDEX(m_crc) ^ b] ^ CRC32_SHIFTED(m_crc);}
0072 
0073     /// \brief Retrieves the i-th byte of the CRC

0074     /// \param i the additional input as a byte

0075     /// \return the byte at the i-th position

0076     byte GetCrcByte(size_t i) const {return reinterpret_cast<const byte *>(&m_crc)[i];}
0077 
0078     std::string AlgorithmProvider() const;
0079 
0080 protected:
0081     void Reset() {m_crc = CRC32_NEGL;}
0082 
0083 private:
0084     static const word32 m_tab[256];
0085     word32 m_crc;
0086 };
0087 
0088 NAMESPACE_END
0089 
0090 #endif