1 | // |
2 | // DigestEngine.cpp |
3 | // |
4 | // Library: Crypto |
5 | // Package: Digest |
6 | // Module: DigestEngine |
7 | // |
8 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Crypto/DigestEngine.h" |
16 | #include "Poco/Crypto/CryptoException.h" |
17 | |
18 | |
19 | namespace Poco { |
20 | namespace Crypto { |
21 | |
22 | |
23 | DigestEngine::DigestEngine(const std::string& name): |
24 | _name(name), |
25 | _pContext(EVP_MD_CTX_create()) |
26 | { |
27 | const EVP_MD* md = EVP_get_digestbyname(_name.c_str()); |
28 | if (!md) throw OpenSSLException(_name); |
29 | EVP_DigestInit_ex(_pContext, md, NULL); |
30 | } |
31 | |
32 | |
33 | DigestEngine::~DigestEngine() |
34 | { |
35 | EVP_MD_CTX_destroy(_pContext); |
36 | } |
37 | |
38 | int DigestEngine::nid() const |
39 | { |
40 | return EVP_MD_nid(EVP_MD_CTX_md(_pContext)); |
41 | } |
42 | |
43 | std::size_t DigestEngine::digestLength() const |
44 | { |
45 | return EVP_MD_CTX_size(_pContext); |
46 | } |
47 | |
48 | |
49 | void DigestEngine::reset() |
50 | { |
51 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) |
52 | EVP_MD_CTX_free(_pContext); |
53 | _pContext = EVP_MD_CTX_create(); |
54 | #else |
55 | EVP_MD_CTX_cleanup(_pContext); |
56 | #endif |
57 | const EVP_MD* md = EVP_get_digestbyname(_name.c_str()); |
58 | if (!md) throw Poco::NotFoundException(_name); |
59 | EVP_DigestInit_ex(_pContext, md, NULL); |
60 | } |
61 | |
62 | |
63 | const Poco::DigestEngine::Digest& DigestEngine::digest() |
64 | { |
65 | _digest.clear(); |
66 | unsigned len = EVP_MD_CTX_size(_pContext); |
67 | _digest.resize(len); |
68 | EVP_DigestFinal_ex(_pContext, &_digest[0], &len); |
69 | reset(); |
70 | return _digest; |
71 | } |
72 | |
73 | |
74 | void DigestEngine::updateImpl(const void* data, std::size_t length) |
75 | { |
76 | EVP_DigestUpdate(_pContext, data, length); |
77 | } |
78 | |
79 | |
80 | } } // namespace Poco::Crypto |
81 | |