Giter Site home page Giter Site logo

stbrumme / hash-library Goto Github PK

View Code? Open in Web Editor NEW
477.0 14.0 136.0 13 KB

Portable C++ hashing library

Home Page: https://create.stephan-brumme.com/hash-library/

License: zlib License

C++ 100.00%
sha1 sha256 keccak md5 crc32 sha3 hmac portable endian-independent

hash-library's Introduction

Portable C++ Hashing Library

This is a mirror of my library hosted at https://create.stephan-brumme.com/hash-library/

In a nutshell:

  • computes CRC32, MD5, SHA1 and SHA256 (most common member of the SHA2 functions), Keccak and its SHA3 sibling
  • optional HMAC (keyed-hash message authentication code)
  • no external dependencies, small code size
  • can work chunk-wise (for example when reading streams block-by-block)
  • portable: supports Windows and Linux, tested on Little Endian and Big Endian CPUs
  • roughly as fast as Linux core hashing functions
  • open source, zlib license

You can find code examples, benchmarks and much more on my website https://create.stephan-brumme.com/hash-library/

How to use

This example computes SHA256 hashes but the API is more or less identical for all hash algorithms:

// SHA2 test program
#include "sha256.h"
#include <iostream> // for std::cout only, not needed for hashing library

int main(int, char**)
{
  // create a new hashing object
  SHA256 sha256;

  // hashing an std::string
  std::cout << sha256("Hello World") << std::endl;
  // => a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e

  // hashing a buffer of bytes
  const char* buffer = "How are you";
  std::cout << sha256(buffer, 11) << std::endl;
  // => 9c7d5b046878838da72e40ceb3179580958df544b240869b80d0275cc07209cc

  // or in a streaming fashion (re-use "How are you")
  SHA256 sha256stream;
  const char* url = "create.stephan-brumme.com"; // 25 bytes
  int step = 5;
  for (int i = 0; i < 25; i += step)
    sha256stream.add(url + i, step); // add five bytes at a time
  std::cout << sha256stream.getHash() << std::endl;
  // => 82aa771f1183c52f973c798c9243a1c73833ea40961c73e55e12430ec77b69f6

  return 0;
}

hash-library's People

Contributors

stbrumme avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hash-library's Issues

License mismatch

The LICENSE file in the repository describes a Zlib license, but the license inside the actual source code is maximally restrictive. When the author reserves all rights, it means that nobody can use the code in any way, not for commercial use, not for personal. They can't modify it or redistribute.

Consider copy-pasting contents of LICENSE into all source files.

SHA3-512 bug

Hexadecimal string 72a5f501151ca974002f34 returns the wrong hash value. Actually any string which starts with 72a5f501151ca974 returns a wrong hash. Maybe related to 64-bit data type?

C language port of the library

This is not an issue per se, but I couldn't think of another way to approach this.
I was looking for a "small" (i.e. not libopenssl) hashing library in C, but didn't find anything. This library is perfect, but is, obviously, in C++.
So, I have ported the code back to C with as few changes as I could. I have not yet ported the tests, but running digest against a set of random files yields the same checksums from both the original and C version which gives me some confidence that I didn't introduce any glaring bugs.
The code retains all copyright notices and statements that Stephan is the original author/owner.
I was considering posting this to github, and realize that the license allows for this but wanted to get your blessing before doing so. If you were interested in adding the repo to github.com/stbrumme, that would also be great.

OSX has no endian.h

It seems OSX has no endian.h, but it also looks like it is not needed.

Possible solution to make sha256.cpp compile on OSX:

Replace

hash-library/sha256.cpp

Lines 9 to 12 in a8a88f8

// big endian architectures need #define __BYTE_ORDER __BIG_ENDIAN
#ifndef _MSC_VER
#include <endian.h>
#endif

with

#if !defined(_MSC_VER) && !defined(__APPLE__)
#include <endian.h>
#endif

Keccak::getHash() is broken if no new data is added

Calling Keccak::getHash() repeatedly produces different hashes, SHA3 could be affected, too

int main(int argc, char *argv[]) {
    Keccak k;
    std::string temp("hello world");
    k.add(&temp[0], temp.size());
    std::cout << k.getHash() << std::endl;
    std::cout << k.getHash() << std::endl;
    std::cout << k.getHash() << std::endl;

    return 0;
}

returns

47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad
c82cad59277af8d880d6ccbbddf0bbed8359fdfaa7491960e1f2397011bf5ae5
d165ecff4927241ce0fa7184b3ad53b4116f7fea6a8b2e1f509e75d087bcfead

Add hmac<SHA512>

Is there a way you could add the funcs in the SHA3 class, that would allow to use it with hmac ?

No gethash(char[]) function in SHA3

Hello, thanks for this library! I didnt see a getHash(unsigned char buffer[HashBytes]) function in SHA3, wasn't sure why. Can you please let me know? Thanks!

digest.cpp is producing different hash for keccak256

I have build it from source using command
g++ -O3 digest.cpp crc32.cpp md5.cpp sha1.cpp sha256.cpp keccak.cpp sha3.cpp -o digest

Calculate the keccak256 for string 5aaeb6053f3e94c9b9a09f33669435e7ef1beaed
Actual hash: Keccak/256: d2a614d1f92994cd6f3e75fd233d568867fcdc638ab04a4dac5a719d7792ee6d

Expected hash: Keccak/256: d385650ce8fdc6db7ee3a091d34814dbc4ce18219ffae52182efff4034d707e5

I can see proper value at "Keccak and SHA3 Live Test" but when I try to use digest it produces different value.

There is a memory leak

There is a memory leak, please add delete[] buffer; to line 64 (before return)

hash-library/digest.cpp

Lines 60 to 65 in f77b564

file.open(filename.c_str(), std::ios::in | std::ios::binary);
if (!file)
{
std::cerr << "Can't open '" << filename << "'" << std::endl;
return 2;
}

and the issue is fixed.
Best regards from the Cppcheck team ;-)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.