Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // Sha256.h
- //
- // Implements hashing function for SHA-256 hashing algorithm
- //
- // Note: Do NOT use for cryptographic purposes, this might not be a secure implementation as it was
- // not designed to be secure, but rather to be fast and simple.
- //
- // Example:
- // Engine::Sha256 sha256; // <- Construct SHA-256 hash object
- // sha256.Update("Hello World", 11); // <- Update SHA-256 hash with data
- // std::array<uint8_t, 32> digest = sha256.Digest(); // <- Get SHA-256 hash
- // std::string sha = sha256.ToString(digest); // <- Get SHA-256 hash as string
- // std::cout << sha << std::endl; // <- Prints out SHA-256 hash
- //
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- #ifndef __SHA256__H__
- #define __SHA256__H__
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // Header section
- #include <array>
- #include <string>
- #include "Engine.h"
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // Class & Structures definition
- namespace Engine
- {
- /// <summary>
- /// Class for computing SHA-256 hash
- /// </summary>
- class Sha256
- {
- private:
- /// <summary>Helper to hold date input into hash</summary>
- uint8_t mData[64];
- /// <summary>Length of data in bytes</summary>
- uint32_t mBlockLength;
- /// <summary>Length of data in bits</summary>
- uint64_t mBitLength;
- /// <summary>Holds current SHA-256 state</summary>
- uint32_t mState[8];
- /// <summary>Hashing key</summary>
- static constexpr std::array<uint32_t, 64> mKey =
- {
- 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,
- 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
- 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,
- 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
- 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,
- 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
- 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,
- 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
- 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,
- 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
- 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,
- 0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
- 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,
- 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
- 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,
- 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
- };
- public:
- /// <summary>
- /// Default constructor
- /// </summary>
- ENGINE_API Sha256();
- /// <summary>
- /// Add data to hash (and re-calculate hash)
- /// </summary>
- /// <param name="data">Inpute data</param>
- /// <param name="length">Length of data in bytes</param>
- ENGINE_API void Update(const uint8_t* data, size_t length);
- /// <summary>
- /// Add data to hash (and re-calculate hash) - C string variant
- /// </summary>
- /// <param name="data">Inpute data</param>
- /// <param name="length">Length of data in bytes</param>
- ENGINE_API void Update(const char* data, size_t length);
- /// <summary>
- /// Add data to hash (and re-calculate hash) - C++ String variant
- /// </summary>
- /// <param name="data">Inpute data</param>
- ENGINE_API void Update(const std::string& data);
- /// <summary>
- /// Get SHA-256 hash
- /// </summary>
- /// <returns>SHA-256 hash</returns>
- ENGINE_API std::array<uint8_t, 32> Digest();
- /// <summary>
- /// Get string representing SHA-256 hash (hexadecimal)
- /// </summary>
- /// <param name="digest">SHA-256 to get string of</param>
- /// <returns>String representation of SHA-256 hash (hexadecimal)</returns>
- ENGINE_API static std::string ToString(const std::array<uint8_t, 32>& digest);
- private:
- /// <summary>
- /// Rotate bits right
- /// </summary>
- /// <param name="value">Input value</param>
- /// <param name="count">No. of bits to rotate right</param>
- /// <returns>Value rotated right by count bits</returns>
- static uint32_t RotateRight(uint32_t value, uint32_t count);
- /// <summary>
- /// Choose function - select input F or G based on mask
- /// </summary>
- /// <param name="e">Mask</param>
- /// <param name="f">Input F</param>
- /// <param name="g">Input G</param>
- /// <returns>Combination of Input F and G based on mask</returns>
- static uint32_t Choose(uint32_t e, uint32_t f, uint32_t g);
- /// <summary>
- /// Bit majority function - median bit operator (n-th bit is 1 when majority of inputs is 1, otherwise 0)
- /// </summary>
- /// <param name="a">Input A</param>
- /// <param name="b">Input B</param>
- /// <param name="c">Input C</param>
- /// <returns>Bit majority of A, B and C</returns>
- static uint32_t Majority(uint32_t a, uint32_t b, uint32_t c);
- /// <summary>
- /// Sigma-0 function for SHA
- /// </summary>
- /// <param name="value">Input value</param>
- /// <returns>Sigma-0 of input value</returns>
- static uint32_t Sigma0(uint32_t value);
- /// <summary>
- /// Sigma-1 function for SHA
- /// </summary>
- /// <param name="value">Input value</param>
- /// <returns>Sigma-1 of input value</returns>
- static uint32_t Sigma1(uint32_t value);
- /// <summary>
- /// Transformation function
- /// </summary>
- void Transform();
- /// <summary>
- /// Padding function
- /// </summary>
- void Pad();
- /// <summary>
- /// Revert hash
- ///
- /// SHA generally uses big-endian ordering, revert all the bytes
- /// </summary>
- /// <param name="hash">Hash array to revert</param>
- void Revert(std::array<uint8_t, 32>& hash);
- };
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // EOH
- #endif // __SHA256__H__
Advertisement