1 // //////////////////////////////////////////////////////////
3 // Copyright (c) 2014,2015 Stephan Brumme. All rights reserved.
4 // see http://create.stephan-brumme.com/disclaimer.html
12 // define fixed size integer types
15 typedef unsigned __int8 uint8_t;
16 typedef unsigned __int32 uint32_t;
17 typedef unsigned __int64 uint64_t;
24 /// compute SHA256 hash
27 std::string myHash = sha256("Hello World"); // std::string
28 std::string myHash2 = sha256("How are you", 11); // arbitrary data, 11 bytes
30 // or in a streaming fashion:
33 while (more data available)
34 sha256.add(pointer to fresh data, number of new bytes);
35 std::string myHash3 = sha256.getHash();
37 class SHA256 //: public Hash
40 /// split into 64 byte blocks (=> 512 bits), hash is 32 bytes long
41 enum { BlockSize = 512 / 8, HashBytes = 32 };
46 /// compute SHA256 of a memory block
47 std::string operator()(const void* data, size_t numBytes);
48 /// compute SHA256 of a string, excluding final zero
49 std::string operator()(const std::string& text);
51 /// add arbitrary number of bytes
52 void add(const void* data, size_t numBytes);
54 /// return latest hash as 64 hex characters
55 std::string getHash();
56 /// return latest hash as bytes
57 void getHash(unsigned char buffer[HashBytes]);
64 void processBlock(const void* data);
65 /// process everything left in the internal buffer
68 /// size of processed data in bytes
70 /// valid bytes in m_buffer
72 /// bytes not processed yet
73 uint8_t m_buffer[BlockSize];
75 enum { HashValues = HashBytes / 4 };
76 /// hash, stored as integers
77 uint32_t m_hash[HashValues];