diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-03-01 20:51:28 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2021-03-01 20:51:28 +0100 |
commit | fc9b28a97b9c8124840a82d798b7b1be18ecad43 (patch) | |
tree | 2d226f4a21c2f3a2d737582905ec532331d260b4 /include | |
parent | f4fd89c776c9dccecc661058d272c7b252f6e85c (diff) |
chore: split sha256 in base sha class
Diffstat (limited to 'include')
-rw-r--r-- | include/sha.h | 57 | ||||
-rw-r--r-- | include/sha256.h | 30 |
2 files changed, 64 insertions, 23 deletions
diff --git a/include/sha.h b/include/sha.h new file mode 100644 index 0000000..255c769 --- /dev/null +++ b/include/sha.h @@ -0,0 +1,57 @@ +// This file is part of a crypto library, exam for the Algorithms class. +// All of these files are under MIT license. +// +// SHA declaration document: +// https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf + +#ifndef HMACSHA256_SHA_H_ +#define HMACSHA256_SHA_H_ + +#include<cstdint> +#include<array> + +namespace hmacsha256 { +class SHA { +public: + SHA() { + digest_ = new uint8_t[32]; + } + + ~SHA() { + delete digest_; + } + + // get digest as array of bytes + virtual uint8_t* digest() = 0; + + virtual std::string hexdigest() = 0; +protected: + uint8_t* digest_; + + // work variables: a, b, c, d, e, f, g, h + uint32_t vars_[8]; + + // data message, in bytes + // M, "message to be hashed" + uint8_t message_[64]; + + // length L of the original message + uint32_t message_l_; + + // lenght of bits + uint64_t bit_len_; + + // the constructor + virtual void init(const uint8_t* data, uint32_t length) = 0; + + // padding + virtual void pad() = 0; + + // main function + virtual void transform() = 0; +}; + + +} // namespace hmacsha256 + +#endif // HMACSHA256_SHA_H_ diff --git a/include/sha256.h b/include/sha256.h index c02eb40..921a50f 100644 --- a/include/sha256.h +++ b/include/sha256.h @@ -9,37 +9,21 @@ #include<cstdint> #include<array> +#include "sha.h" namespace hmacsha256 { -class SHA256 { +class SHA256 : public SHA { public: // create a new SHA256 thanks an array of bytes SHA256(const uint8_t*, uint32_t); SHA256(const std::string&); - ~SHA256(); - // get digest as array of bytes - uint8_t* digest(); + uint8_t* digest() override; - std::string hexdigest(); + std::string hexdigest() override; private: - uint8_t* digest_; - - // work variables: a, b, c, d, e, f, g, h - uint32_t vars_[8]; - - // data message, in bytes - // M, "message to be hashed" - uint8_t message_[64]; - - // length L of the original message - uint32_t message_l_; - - // lengh of bits - uint64_t bit_len_; - // first 80 costansts 64bit words, paragraph 4.2.2 std::array<uint32_t, 64> K = { 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5, @@ -61,7 +45,7 @@ private: }; // the constructor - void init(const uint8_t* data, uint32_t length); + void init(const uint8_t* data, uint32_t length) override; // Ch(x, y, z) = (x | y) ^ (~x & z) static uint32_t ch(uint32_t, uint32_t, uint32_t); @@ -90,10 +74,10 @@ private: static uint32_t sigma1(uint32_t); // padding - void pad(); + void pad() override; // main function - void transform(); + void transform() override; // transform bytes in big-endian, because SHA256 wants big endian ordering // Takes 1 parameter, the array of bytes that we want to reorder |