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 <cstdio>
|
|
|
|
#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
|
|
|
|
Pl_SHA2::write(unsigned char* buf, size_t len)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Write in chunks in case len is too big to fit in an int.
|
|
|
|
// Assume int is at least 32 bits.
|
|
|
|
static size_t const max_bytes = 1 << 30;
|
|
|
|
size_t bytes_left = len;
|
|
|
|
unsigned char* 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;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->getNext(true)) {
|
2019-11-04 14:55:43 +00:00
|
|
|
this->getNext()->write(buf, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_SHA2::finish()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->getNext(true)) {
|
2019-11-04 14:55:43 +00:00
|
|
|
this->getNext()->finish();
|
|
|
|
}
|
|
|
|
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) {
|
2022-02-08 14:18:08 +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) {
|
2022-02-08 14:18:08 +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) {
|
2022-02-08 14:18:08 +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());
|
|
|
|
}
|