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 
16 
17 #ifndef Crypto_RSADigestEngine_INCLUDED
18 #define Crypto_RSADigestEngine_INCLUDED
19 
20 
21 #include "Poco/Crypto/Crypto.h"
22 #include "Poco/Crypto/RSAKey.h"
23 #include "Poco/DigestEngine.h"
24 #include "Poco/Crypto/DigestEngine.h"
25 #include <istream>
26 #include <ostream>
27 
28 
29 namespace Poco {
30 namespace Crypto {
31 
32 
33 class Crypto_API RSADigestEngine: public Poco::DigestEngine
34 	/// This class implements a Poco::DigestEngine that can be
35 	/// used to compute a secure digital signature.
36 	///
37 	/// First another Poco::Crypto::DigestEngine is created and
38 	/// used to compute a cryptographic hash of the data to be
39 	/// signed. Then, the hash value is encrypted, using
40 	/// the RSA private key.
41 	///
42 	/// To verify a signature, pass it to the verify()
43 	/// member function. It will decrypt the signature
44 	/// using the RSA public key and compare the resulting
45 	/// hash with the actual hash of the data.
46 {
47 public:
48 	enum DigestType
49 	{
50 		DIGEST_MD5,
51 		DIGEST_SHA1
52 	};
53 
54 	//@ deprecated
55 	RSADigestEngine(const RSAKey& key, DigestType digestType = DIGEST_SHA1);
56 		/// Creates the RSADigestEngine with the given RSA key,
57 		/// using the MD5 or SHA-1 hash algorithm.
58 		/// Kept for backward compatibility
59 
60 	RSADigestEngine(const RSAKey& key, const std::string &name);
61 		/// Creates the RSADigestEngine with the given RSA key,
62 		/// using the hash algorithm with the given name
63 		/// (e.g., "MD5", "SHA1", "SHA256", "SHA512", etc.).
64 		/// See the OpenSSL documentation for a list of supported digest algorithms.
65 		///
66 		/// Throws a Poco::NotFoundException if no algorithm with the given name exists.
67 
68 	~RSADigestEngine();
69 		/// Destroys the RSADigestEngine.
70 
71 	std::size_t digestLength() const;
72 		/// Returns the length of the digest in bytes.
73 
74 	void reset();
75 		/// Resets the engine so that a new
76 		/// digest can be computed.
77 
78 	const DigestEngine::Digest& digest();
79 		/// Finishes the computation of the digest
80 		/// (the first time it's called) and
81 		/// returns the message digest.
82 		///
83 		/// Can be called multiple times.
84 
85 	const DigestEngine::Digest& signature();
86 		/// Signs the digest using the RSA algorithm
87 		/// and the private key (the first time it's
88 		/// called) and returns the result.
89 		///
90 		/// Can be called multiple times.
91 
92 	bool verify(const DigestEngine::Digest& signature);
93 		/// Verifies the data against the signature.
94 		///
95 		/// Returns true if the signature can be verified, false otherwise.
96 
97 protected:
98 	void updateImpl(const void* data, std::size_t length);
99 
100 private:
101 	RSAKey _key;
102 	Poco::Crypto::DigestEngine _engine;
103 	Poco::DigestEngine::Digest _digest;
104 	Poco::DigestEngine::Digest _signature;
105 };
106 
107 
108 } } // namespace Poco::Crypto
109 
110 
111 #endif // Crypto_RSADigestEngine_INCLUDED
112