diff options
author | Determinant <[email protected]> | 2019-04-07 14:06:36 -0400 |
---|---|---|
committer | Determinant <[email protected]> | 2019-04-07 14:06:36 -0400 |
commit | 8a0d8fbaf59be7acc536b2a753caa27aa1fbae24 (patch) | |
tree | aea98bd6c3e8d09450d7ae0df1462f4a9d515066 | |
parent | 07d197a2d049aedb77536edeb3b20e51cc8c9588 (diff) |
use sha1 as checksum
-rw-r--r-- | include/salticidae/crypto.h | 42 | ||||
-rw-r--r-- | include/salticidae/msg.h | 14 |
2 files changed, 49 insertions, 7 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 diff --git a/include/salticidae/msg.h b/include/salticidae/msg.h index 42a9bc2..03eb6dd 100644 --- a/include/salticidae/msg.h +++ b/include/salticidae/msg.h @@ -192,19 +192,19 @@ class MsgBase { #ifndef SALTICIDAE_NOCHECKSUM uint32_t get_checksum() const { - static thread_local class SHA256 sha256; + static thread_local class SHA1 sha1; uint32_t res; bytearray_t tmp; #ifndef SALTICIDAE_NOCHECK if (no_payload) throw std::runtime_error("payload not available"); #endif - sha256.reset(); - sha256.update(payload); - sha256.digest(tmp); - sha256.reset(); - sha256.update(tmp); - sha256.digest(tmp); + sha1.reset(); + sha1.update(payload); + sha1.digest(tmp); + //sha256.reset(); + //sha256.update(tmp); + //sha256.digest(tmp); memmove(&res, &*tmp.begin(), 4); return res; } |