2019-11-04 14:55:43 +00:00
|
|
|
#include <qpdf/Pl_SHA2.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2019-11-04 14:55:43 +00:00
|
|
|
#include <qpdf/QPDFCryptoProvider.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
#include <stdexcept>
|
2019-11-04 14:55:43 +00:00
|
|
|
|
|
|
|
Pl_SHA2::Pl_SHA2(int bits, Pipeline* next) :
|
|
|
|
Pipeline("sha2", next),
|
|
|
|
in_progress(false)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (bits) {
|
2019-11-04 14:55:43 +00:00
|
|
|
resetBits(bits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-05-03 21:43:07 +00:00
|
|
|
Pl_SHA2::write(unsigned char const* buf, size_t len)
|
2019-11-04 14:55:43 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!this->in_progress) {
|
2022-02-08 14:18:08 +00:00
|
|
|
this->in_progress = true;
|
2019-11-04 14:55:43 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 17:19:52 +00:00
|
|
|
// Write in chunks in case len is too big to fit in an int. Assume int is at least 32 bits.
|
2019-11-04 14:55:43 +00:00
|
|
|
static size_t const max_bytes = 1 << 30;
|
|
|
|
size_t bytes_left = len;
|
2022-05-03 21:43:07 +00:00
|
|
|
unsigned char const* data = buf;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (bytes_left > 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left);
|
2019-11-04 14:55:43 +00:00
|
|
|
this->crypto->SHA2_update(data, bytes);
|
2022-02-08 14:18:08 +00:00
|
|
|
bytes_left -= bytes;
|
2019-11-04 14:55:43 +00:00
|
|
|
data += bytes;
|
|
|
|
}
|
|
|
|
|
2024-08-07 16:20:19 +00:00
|
|
|
if (next()) {
|
|
|
|
next()->write(buf, len);
|
2019-11-04 14:55:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_SHA2::finish()
|
|
|
|
{
|
2024-08-07 16:20:19 +00:00
|
|
|
if (next()) {
|
|
|
|
next()->finish();
|
2019-11-04 14:55:43 +00:00
|
|
|
}
|
|
|
|
this->crypto->SHA2_finalize();
|
|
|
|
this->in_progress = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_SHA2::resetBits(int bits)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->in_progress) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::logic_error("bit reset requested for in-progress SHA2 Pipeline");
|
2019-11-04 14:55:43 +00:00
|
|
|
}
|
|
|
|
this->crypto = QPDFCryptoProvider::getImpl();
|
|
|
|
this->crypto->SHA2_init(bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Pl_SHA2::getRawDigest()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->in_progress) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::logic_error("digest requested for in-progress SHA2 Pipeline");
|
2019-11-04 14:55:43 +00:00
|
|
|
}
|
|
|
|
return this->crypto->SHA2_digest();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Pl_SHA2::getHexDigest()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->in_progress) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::logic_error("digest requested for in-progress SHA2 Pipeline");
|
2019-11-04 14:55:43 +00:00
|
|
|
}
|
|
|
|
return QUtil::hex_encode(getRawDigest());
|
|
|
|
}
|