|
| 1 | +# Portable C++ Hashing Library |
| 2 | + |
| 3 | +This is a mirror of my library hosted at https://create.stephan-brumme.com/hash-library/ |
| 4 | + |
| 5 | +In a nutshell: |
| 6 | + |
| 7 | +- computes CRC32, MD5, SHA1 and SHA256 (most common member of the SHA2 functions), Keccak and its SHA3 sibling |
| 8 | +- optional HMAC (keyed-hash message authentication code) |
| 9 | +- no external dependencies, small code size |
| 10 | +- can work chunk-wise (for example when reading streams block-by-block) |
| 11 | +- portable: supports Windows and Linux, tested on Little Endian and Big Endian CPUs |
| 12 | +- roughly as fast as Linux core hashing functions |
| 13 | +- open source, zlib license |
| 14 | + |
| 15 | +You can find code examples, benchmarks and much more on my website https://create.stephan-brumme.com/hash-library/ |
| 16 | + |
| 17 | +# How to use |
| 18 | + |
| 19 | +This example computes SHA256 hashes but the API is more or less identical for all hash algorithms: |
| 20 | + |
| 21 | +``` cpp |
| 22 | +// SHA2 test program |
| 23 | +#include "sha256.h" |
| 24 | +#include <iostream> // for std::cout only, not needed for hashing library |
| 25 | + |
| 26 | +int main(int, char**) |
| 27 | +{ |
| 28 | + // create a new hashing object |
| 29 | + SHA256 sha256; |
| 30 | + |
| 31 | + // hashing an std::string |
| 32 | + std::cout << sha256("Hello World") << std::endl; |
| 33 | + // => a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e |
| 34 | + |
| 35 | + // hashing a buffer of bytes |
| 36 | + const char* buffer = "How are you"; |
| 37 | + std::cout << sha256(buffer, 11) << std::endl; |
| 38 | + // => 9c7d5b046878838da72e40ceb3179580958df544b240869b80d0275cc07209cc |
| 39 | + |
| 40 | + // or in a streaming fashion (re-use "How are you") |
| 41 | + SHA256 sha256stream; |
| 42 | + const char* url = "create.stephan-brumme.com"; // 25 bytes |
| 43 | + int step = 5; |
| 44 | + for (int i = 0; i < 25; i += step) |
| 45 | + sha256stream.add(url + i, step); // add five bytes at a time |
| 46 | + std::cout << sha256stream.getHash() << std::endl; |
| 47 | + // => 82aa771f1183c52f973c798c9243a1c73833ea40961c73e55e12430ec77b69f6 |
| 48 | + |
| 49 | + return 0; |
| 50 | +} |
| 51 | +``` |
0 commit comments