diff options
Diffstat (limited to 'include/salticidae/crypto.h')
-rw-r--r-- | include/salticidae/crypto.h | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/include/salticidae/crypto.h b/include/salticidae/crypto.h index 79e9377..772cce1 100644 --- a/include/salticidae/crypto.h +++ b/include/salticidae/crypto.h @@ -72,6 +72,48 @@ class SHA256 { } }; +class SHA1 { + SHA_CTX ctx; + + public: + SHA1() { reset(); } + + void reset() { + if (!SHA1_Init(&ctx)) + throw std::runtime_error("openssl SHA1 init error"); + } + + template<typename T> + void update(const T &data) { + update(reinterpret_cast<const uint8_t *>(&*data.begin()), data.size()); + } + + void update(const bytearray_t::const_iterator &it, size_t length) { + update(&*it, length); + } + + void update(const uint8_t *ptr, size_t length) { + if (!SHA1_Update(&ctx, ptr, length)) + throw std::runtime_error("openssl SHA1 update error"); + } + + void _digest(bytearray_t &md) { + if (!SHA1_Final(&*md.begin(), &ctx)) + throw std::runtime_error("openssl SHA1 error"); + } + + void digest(bytearray_t &md) { + md.resize(32); + _digest(md); + } + + bytearray_t digest() { + bytearray_t md(32); + _digest(md); + return std::move(md); + } +}; + } #endif |