RSADigestEngine.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // RSADigestEngine.h
  3. //
  4. // Library: Crypto
  5. // Package: RSA
  6. // Module: RSADigestEngine
  7. //
  8. // Definition of the RSADigestEngine class.
  9. //
  10. // Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Crypto_RSADigestEngine_INCLUDED
  16. #define Crypto_RSADigestEngine_INCLUDED
  17. #include "Poco/Crypto/Crypto.h"
  18. #include "Poco/Crypto/RSAKey.h"
  19. #include "Poco/DigestEngine.h"
  20. #include "Poco/Crypto/DigestEngine.h"
  21. #include <istream>
  22. #include <ostream>
  23. namespace Poco {
  24. namespace Crypto {
  25. class Crypto_API RSADigestEngine: public Poco::DigestEngine
  26. /// This class implements a Poco::DigestEngine that can be
  27. /// used to compute a secure digital signature.
  28. ///
  29. /// First another Poco::Crypto::DigestEngine is created and
  30. /// used to compute a cryptographic hash of the data to be
  31. /// signed. Then, the hash value is encrypted, using
  32. /// the RSA private key.
  33. ///
  34. /// To verify a signature, pass it to the verify()
  35. /// member function. It will decrypt the signature
  36. /// using the RSA public key and compare the resulting
  37. /// hash with the actual hash of the data.
  38. {
  39. public:
  40. enum DigestType
  41. {
  42. DIGEST_MD5,
  43. DIGEST_SHA1
  44. };
  45. //@ deprecated
  46. RSADigestEngine(const RSAKey& key, DigestType digestType = DIGEST_SHA1);
  47. /// Creates the RSADigestEngine with the given RSA key,
  48. /// using the MD5 or SHA-1 hash algorithm.
  49. /// Kept for backward compatibility
  50. RSADigestEngine(const RSAKey& key, const std::string &name);
  51. /// Creates the RSADigestEngine with the given RSA key,
  52. /// using the hash algorithm with the given name
  53. /// (e.g., "MD5", "SHA1", "SHA256", "SHA512", etc.).
  54. /// See the OpenSSL documentation for a list of supported digest algorithms.
  55. ///
  56. /// Throws a Poco::NotFoundException if no algorithm with the given name exists.
  57. ~RSADigestEngine();
  58. /// Destroys the RSADigestEngine.
  59. std::size_t digestLength() const;
  60. /// Returns the length of the digest in bytes.
  61. void reset();
  62. /// Resets the engine so that a new
  63. /// digest can be computed.
  64. const DigestEngine::Digest& digest();
  65. /// Finishes the computation of the digest
  66. /// (the first time it's called) and
  67. /// returns the message digest.
  68. ///
  69. /// Can be called multiple times.
  70. const DigestEngine::Digest& signature();
  71. /// Signs the digest using the RSA algorithm
  72. /// and the private key (the first time it's
  73. /// called) and returns the result.
  74. ///
  75. /// Can be called multiple times.
  76. bool verify(const DigestEngine::Digest& signature);
  77. /// Verifies the data against the signature.
  78. ///
  79. /// Returns true if the signature can be verified, false otherwise.
  80. protected:
  81. void updateImpl(const void* data, std::size_t length);
  82. private:
  83. RSAKey _key;
  84. Poco::Crypto::DigestEngine _engine;
  85. Poco::DigestEngine::Digest _digest;
  86. Poco::DigestEngine::Digest _signature;
  87. };
  88. } } // namespace Poco::Crypto
  89. #endif // Crypto_RSADigestEngine_INCLUDED