crc.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // crc.h - originally written and placed in the public domain by Wei Dai
  2. /// \file crc.h
  3. /// \brief Classes for CRC-32 and CRC-32C checksum algorithm
  4. #ifndef CRYPTOPP_CRC32_H
  5. #define CRYPTOPP_CRC32_H
  6. #include "cryptlib.h"
  7. NAMESPACE_BEGIN(CryptoPP)
  8. const word32 CRC32_NEGL = 0xffffffffL;
  9. #if (CRYPTOPP_LITTLE_ENDIAN)
  10. #define CRC32_INDEX(c) (c & 0xff)
  11. #define CRC32_SHIFTED(c) (c >> 8)
  12. #else
  13. #define CRC32_INDEX(c) (c >> 24)
  14. #define CRC32_SHIFTED(c) (c << 8)
  15. #endif
  16. /// \brief CRC-32 Checksum Calculation
  17. /// \details Uses CRC polynomial 0xEDB88320
  18. class CRC32 : public HashTransformation
  19. {
  20. public:
  21. CRYPTOPP_CONSTANT(DIGESTSIZE = 4);
  22. CRC32();
  23. void Update(const byte *input, size_t length);
  24. void TruncatedFinal(byte *hash, size_t size);
  25. unsigned int DigestSize() const {return DIGESTSIZE;}
  26. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "CRC32";}
  27. std::string AlgorithmName() const {return StaticAlgorithmName();}
  28. /// \brief Updates a CRC with additional input
  29. /// \param b the additional input as a byte
  30. void UpdateByte(byte b) {m_crc = m_tab[CRC32_INDEX(m_crc) ^ b] ^ CRC32_SHIFTED(m_crc);}
  31. /// \brief Retrieves the i-th byte of the CRC
  32. /// \param i the additional input as a byte
  33. /// \return the byte at the i-th position
  34. byte GetCrcByte(size_t i) const {return reinterpret_cast<const byte *>(&m_crc)[i];}
  35. std::string AlgorithmProvider() const;
  36. protected:
  37. void Reset() {m_crc = CRC32_NEGL;}
  38. private:
  39. static const word32 m_tab[256];
  40. word32 m_crc;
  41. };
  42. /// \brief CRC-32C Checksum Calculation
  43. /// \details Uses CRC polynomial 0x82F63B78
  44. /// \since Crypto++ 5.6.4
  45. class CRC32C : public HashTransformation
  46. {
  47. public:
  48. CRYPTOPP_CONSTANT(DIGESTSIZE = 4);
  49. CRC32C();
  50. void Update(const byte *input, size_t length);
  51. void TruncatedFinal(byte *hash, size_t size);
  52. unsigned int DigestSize() const {return DIGESTSIZE;}
  53. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "CRC32C";}
  54. std::string AlgorithmName() const {return StaticAlgorithmName();}
  55. /// \brief Updates a CRC with additional input
  56. /// \param b the additional input as a byte
  57. void UpdateByte(byte b) {m_crc = m_tab[CRC32_INDEX(m_crc) ^ b] ^ CRC32_SHIFTED(m_crc);}
  58. /// \brief Retrieves the i-th byte of the CRC
  59. /// \param i the additional input as a byte
  60. /// \return the byte at the i-th position
  61. byte GetCrcByte(size_t i) const {return reinterpret_cast<const byte *>(&m_crc)[i];}
  62. std::string AlgorithmProvider() const;
  63. protected:
  64. void Reset() {m_crc = CRC32_NEGL;}
  65. private:
  66. static const word32 m_tab[256];
  67. word32 m_crc;
  68. };
  69. NAMESPACE_END
  70. #endif