DigestEngine.h 2.4 KB

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