DigestEngine.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // DigestEngine.h
  3. //
  4. // Library: Foundation
  5. // Package: Crypt
  6. // Module: DigestEngine
  7. //
  8. // Definition of class DigestEngine.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_DigestEngine_INCLUDED
  16. #define Foundation_DigestEngine_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include <vector>
  19. namespace Poco {
  20. class Foundation_API DigestEngine
  21. /// This class is an abstract base class
  22. /// for all classes implementing a message
  23. /// digest algorithm, like MD5Engine
  24. /// and SHA1Engine.
  25. /// Call update() repeatedly with data to
  26. /// compute the digest from. When done,
  27. /// call digest() to obtain the message
  28. /// digest.
  29. {
  30. public:
  31. typedef std::vector<unsigned char> Digest;
  32. DigestEngine();
  33. virtual ~DigestEngine();
  34. void update(const void* data, std::size_t length);
  35. void update(char data);
  36. void update(const std::string& data);
  37. /// Updates the digest with the given data.
  38. virtual std::size_t digestLength() const = 0;
  39. /// Returns the length of the digest in bytes.
  40. virtual void reset() = 0;
  41. /// Resets the engine so that a new
  42. /// digest can be computed.
  43. virtual const Digest& digest() = 0;
  44. /// Finishes the computation of the digest and
  45. /// returns the message digest. Resets the engine
  46. /// and can thus only be called once for every digest.
  47. /// The returned reference is valid until the next
  48. /// time digest() is called, or the engine object is destroyed.
  49. static std::string digestToHex(const Digest& bytes);
  50. /// Converts a message digest into a string of hexadecimal numbers.
  51. static Digest digestFromHex(const std::string& digest);
  52. /// Converts a string created by digestToHex back to its Digest presentation
  53. static bool constantTimeEquals(const Digest& d1, const Digest& d2);
  54. /// Compares two Digest values using a constant-time comparison
  55. /// algorithm. This can be used to prevent timing attacks
  56. /// (as discussed in <https://codahale.com/a-lesson-in-timing-attacks/>).
  57. protected:
  58. virtual void updateImpl(const void* data, std::size_t length) = 0;
  59. /// Updates the digest with the given data. Must be implemented
  60. /// by subclasses.
  61. private:
  62. DigestEngine(const DigestEngine&);
  63. DigestEngine& operator = (const DigestEngine&);
  64. };
  65. //
  66. // inlines
  67. //
  68. inline void DigestEngine::update(const void* data, std::size_t length)
  69. {
  70. updateImpl(data, length);
  71. }
  72. inline void DigestEngine::update(char data)
  73. {
  74. updateImpl(&data, 1);
  75. }
  76. inline void DigestEngine::update(const std::string& data)
  77. {
  78. updateImpl(data.data(), data.size());
  79. }
  80. } // namespace Poco
  81. #endif // Foundation_DigestEngine_INCLUDED