2019-11-04 14:55:43 +00:00
|
|
|
#include <qpdf/SHA2_native.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2013-01-25 13:59:55 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <stdexcept>
|
2019-11-04 14:55:43 +00:00
|
|
|
|
|
|
|
SHA2_native::SHA2_native(int bits) :
|
|
|
|
bits(bits)
|
2012-12-29 13:07:46 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (bits) {
|
|
|
|
case 256:
|
2019-11-04 14:55:43 +00:00
|
|
|
sph_sha256_init(&this->ctx256);
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 384:
|
2019-11-04 14:55:43 +00:00
|
|
|
sph_sha384_init(&this->ctx384);
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 512:
|
2019-11-04 14:55:43 +00:00
|
|
|
sph_sha512_init(&this->ctx512);
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
2019-11-04 14:55:43 +00:00
|
|
|
badBits();
|
|
|
|
break;
|
2012-12-29 13:07:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-11-04 14:55:43 +00:00
|
|
|
SHA2_native::badBits()
|
2012-12-29 13:07:46 +00:00
|
|
|
{
|
2019-11-04 14:55:43 +00:00
|
|
|
throw std::logic_error("SHA2_native has bits != 256, 384, or 512");
|
2012-12-29 13:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-11-04 14:55:43 +00:00
|
|
|
SHA2_native::update(unsigned char const* buf, size_t len)
|
2012-12-29 13:07:46 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (bits) {
|
|
|
|
case 256:
|
2019-11-04 14:55:43 +00:00
|
|
|
sph_sha256(&this->ctx256, buf, len);
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 384:
|
2019-11-04 14:55:43 +00:00
|
|
|
sph_sha384(&this->ctx384, buf, len);
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 512:
|
2019-11-04 14:55:43 +00:00
|
|
|
sph_sha512(&this->ctx512, buf, len);
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
2019-11-04 14:55:43 +00:00
|
|
|
badBits();
|
|
|
|
break;
|
2012-12-29 13:07:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-11-04 14:55:43 +00:00
|
|
|
SHA2_native::finalize()
|
2012-12-29 13:07:46 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (bits) {
|
|
|
|
case 256:
|
2012-12-29 13:07:46 +00:00
|
|
|
sph_sha256_close(&this->ctx256, sha256sum);
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 384:
|
2012-12-29 13:07:46 +00:00
|
|
|
sph_sha384_close(&this->ctx384, sha384sum);
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 512:
|
2012-12-29 13:07:46 +00:00
|
|
|
sph_sha512_close(&this->ctx512, sha512sum);
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
2012-12-29 13:07:46 +00:00
|
|
|
badBits();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2019-11-04 14:55:43 +00:00
|
|
|
SHA2_native::getRawDigest()
|
2012-12-29 13:07:46 +00:00
|
|
|
{
|
|
|
|
std::string result;
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (bits) {
|
|
|
|
case 256:
|
2023-05-21 17:35:09 +00:00
|
|
|
result = std::string(reinterpret_cast<char*>(this->sha256sum), sizeof(this->sha256sum));
|
2012-12-29 13:07:46 +00:00
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 384:
|
2023-05-21 17:35:09 +00:00
|
|
|
result = std::string(reinterpret_cast<char*>(this->sha384sum), sizeof(this->sha384sum));
|
2012-12-29 13:07:46 +00:00
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 512:
|
2023-05-21 17:35:09 +00:00
|
|
|
result = std::string(reinterpret_cast<char*>(this->sha512sum), sizeof(this->sha512sum));
|
2012-12-29 13:07:46 +00:00
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
2012-12-29 13:07:46 +00:00
|
|
|
badBits();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|