X-Git-Url: http://git.tdb.fi/?p=libs%2Fcrypto.git;a=blobdiff_plain;f=source%2Fsha2.h;fp=source%2Fsha2.h;h=ed5c4fe36d0a210dd8c24e740565eaa888c17941;hp=0000000000000000000000000000000000000000;hb=6511a62c3cec0a64bbdd7ac8e3588d39c164af09;hpb=cb6ee39a05f6903a0ef521c81e6f42d05289b1a1 diff --git a/source/sha2.h b/source/sha2.h new file mode 100644 index 0000000..ed5c4fe --- /dev/null +++ b/source/sha2.h @@ -0,0 +1,77 @@ +#ifndef MSP_CRYPTO_SHA2_H_ +#define MSP_CRYPTO_SHA2_H_ + +#include +#include "blockhash.h" + +namespace Msp { +namespace Crypto { + +template +class SHA2: public BlockHash +{ +private: + typedef C Constants; + typedef typename Constants::WordType WordType; + + typename Constants::WordType buffer[8]; + UInt64 processed_bytes; + +public: + SHA2(); + SHA2(const char *, unsigned); + SHA2(const std::string &); +private: + void init(); + +public: + virtual unsigned get_digest_size() const { return Constants::DIGEST_SIZE; } + + virtual unsigned get_digest(char *, unsigned) const; + +private: + virtual void process_block(const char *); +}; + + +struct SHA2_256Constants +{ + typedef UInt32 WordType; + + enum + { + WORD_SIZE = sizeof(WordType), + BLOCK_SIZE = 64, // 512 bits + DIGEST_SIZE = 32, // 256 bits + N_ROUNDS = 64 + }; + + static const WordType initial[8]; + static const WordType round_constants[N_ROUNDS]; + static const unsigned sigma_constants[12]; +}; + +struct SHA2_512Constants +{ + typedef UInt64 WordType; + + enum + { + WORD_SIZE = sizeof(WordType), + BLOCK_SIZE = 128, // 1024 bits + DIGEST_SIZE = 64, // 512 bits + N_ROUNDS = 80 + }; + + static const WordType initial[8]; + static const WordType round_constants[N_ROUNDS]; + static const unsigned sigma_constants[12]; +}; + +typedef SHA2 SHA256; +typedef SHA2 SHA512; + +} // namespace Crypto +} // namespace Msp + +#endif