Giter Site home page Giter Site logo

hash's Introduction

Hash AppVeyor_badge GithubAction_badge

C++14 header-only easy-to-use hash library

What is included in this library:

Name Variants Website
BLAKE1 224, 256, 384, 512 https://131002.net/blake/
BLAKE2 BLAKE2b, BLAKE2s https://blake2.net/
CRC-32 http://create.stephan-brumme.com/crc32/
Fowler–Noll–Vo (FNV) FNV32_0, FNV32_1, FNV32_1a http://www.isthe.com/chongo/tech/comp/fnv/index.html
FNV64_0, FNV64_1, FNV64_1a
HAS-160 https://www.tta.or.kr/eng/new/standardization/eng_ttastddesc.jsp?stdno=TTAS.KO-12.0011/R2
HAS-V (unfinished) https://link.springer.com/chapter/10.1007%2F3-540-44983-3_15
MD2 https://tools.ietf.org/html/rfc1319
MD4 https://tools.ietf.org/html/rfc1320
MD5 https://tools.ietf.org/html/rfc1321
RIPEMD 128, 160, 256, 320 https://homes.esat.kuleuven.be/~bosselae/ripemd160.html
SipHash https://github.com/veorq/SipHash
SHA-1 https://tools.ietf.org/html/rfc3174
SHA-2 224, 256, 384, 512, 512-224, 512-256 https://tools.ietf.org/html/rfc6234
SHA-3 224, 256, 384, 512, SHAKE-128, SHAKE-256 https://keccak.team/index.html
SHA-3 derived functions CSHAKE-128, CSHAKE-256 http://csrc.nist.gov/groups/ST/hash/derived-functions.html
TupleHash-128, TupleHash-256
SM3 https://tools.ietf.org/html/draft-sca-cfrg-sm3-02
Tiger Tiger1-128, Tiger1-160, Tiger1-192 https://www.cs.technion.ac.il/~biham/Reports/Tiger/
Tiger2-128, Tiger2-160, Tiger2-192
WHIRLPOOL http://www.larc.usp.br/~pbarreto/WhirlpoolPage.html

If you are concerned about security, state-of-the-art performance or whatsoever issue, then take your time and go setup OpenSSL or other proven library

Now some features:

  • No/none tweaking knobs, it just works!
    Works out-of-the-box on little endian & big endian machines
  • Have similar performance to C implementations: see my rudimentary benchmark: Benchmark.md

Prerequisites

Usage

To use this library in your project:

  1. Download this repository and unzip it

  2. Use it like:

    // include the header
    #include "pathToLib/sha1.h"
    
    // If you are using C++14 or C++17, don't forget the "gsl" folder!
    
    void example()
    {
      // Get an instance
      Chocobo1::SHA1 sha1;
    
      // Feed data & data length to it
      sha1.addData("hello", 5);
    
      // ... or just
      sha1.addData("hello");
    
      // Tell it to wrap it up
      sha1.finalize();
    
      // Profit! oops, not really...
      std::string result = sha1.toString();
    
      // A one-liner
      std::string result2 = Chocobo1::SHA1().addData("hello").finalize().toString();
    
      // In byte array form
      auto bytes = Chocobo1::SHA1().addData("hello").finalize().toArray();  // std::array<uint8_t, 20>
    
      // Some hash algorithm provides constexpr result under C++17
      constexpr uint8_t data[] = {0x00, 0xFF};
      constexpr auto bytes2 = Chocobo1::SHA1().addData(data).finalize().toArray();
    }
  3. There is also a driver program included, you can find it at "src/program"

    1. Compile:

      cd src/program
      meson _builddir  # initialize build folder
      cd _builddir
      ninja            # call the `ninja` to build our program
    2. Run:

      $ ./hash -md5 /path/to/file

Run Tests

cd tests
meson _build
cd _build
ninja
./run_tests

Dependencies

  • Microsoft/GSL
    This library is only requried when compiling with C++14 and C++17. In C++20 or later, std::span will be used in place of gsl::span.
    No need to download or install it. This repository included a modified version that will compile locally (only for gsl::span related headers).

License

See LICENSE file

Like it?

Please consider donate

hash's People

Contributors

chocobo1 avatar koldi 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

hash's Issues

Worse library ever

This library is not usable::

MACRO is failing: USE_STD_SPAN_CHOCOBO1_HASH

Whatever you define it, it will trigger an error, telling you that std::span is not defined
-> Enable C++20 to get this feature: still failing lol.

Weird namspaces overwritten at the top on include for no reasons.

And the best for the end:

Line 376 of md5.h: iMax is NEVER defined anywhere.

I'm wondering how you can write such a lib, which that many lines of code, without encountering this fearking error !!!

I will never try to use that again, complete waste of time.

Benchmark

I was wondering if you ever tried to benchmark yours, digestpp and CryptoPP implementations to highlight the strengths and weaknesses of each library.
I wanted to but including headers on quick-bench isn't as straightforward as I hoped.
Thanks and sorry to bother you!

Fails with clang-10: constexpr variable 'bytes2' must be initialized by a constant expression

The example() function from README fails to be compiled with clang-10:

$ c++ -std=c++17 -o test test.cpp -I /usr/local/include/
test.cpp:33:18: error: constexpr variable 'bytes2' must be initialized by a constant expression
  constexpr auto bytes2 = Chocobo1::SHA1().addData(data).finalize().toArray();
                 ^        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/hash/sha1.h:225:13: note: cast from 'const void *' is not allowed in a constant expression
                                : m_ptr(static_cast<const uint8_t *>(ptr))
                                        ^
/usr/local/include/hash/sha1.h:415:27: note: in call to 'Loader(&Chocobo1::SHA1().m_buffer.m_array.__elems_[0])'
                        const Loader<uint32_t> m(static_cast<const Byte *>(data.data() + (i * BLOCK_SIZE)));
                                               ^
/usr/local/include/hash/sha1.h:302:3: note: in call to '&Chocobo1::SHA1()->addDataImpl({{{64}, &Chocobo1::SHA1().m_buffer.m_array.__elems_[0]}})'
                addDataImpl({m_buffer.data(), m_buffer.size()});
                ^
test.cpp:33:58: note: in call to '&Chocobo1::SHA1()->finalize()'
  constexpr auto bytes2 = Chocobo1::SHA1().addData(data).finalize().toArray();
                                                         ^
1 error generated.

FreeBSD 12.2

Bug in Sha1 hashing

Got an invalid hash value when hashing the contents of this file I have attached. Hash value with this Sha1 implementation: 87e4ce7bde2fc706fe982e26f118c16333180d96. Hex value I got from openssl command line: 004c318fbda1d2172f3fec228f509c06b9cbaf91. I also replaced the sha1 implementation to doublecheck that it wasn't my input that was wrong, and got the correct hash value with that. The occasional wrong MAC's I got in my TLS implementation has also dissapeared since I replaced the hash function. I can only assume that it has to be some obscure bug in this library's sha1 implementation, as it works fine 99% of the time. Compiled on windows x86-64 with MSVC and C++17.
I have also attached exactly what code I used to replicate the bug. (DebugHash.txt should be read as binary data).
DebugHash.txt
WrongHash.txt

MD5 OOB crash with specific size of data

Giving a string of 829 characters as input to the MD5 runner causes a consistent OOB error during finalize, specifically on line 304 with the call to end:

addDataImpl({m_buffer.begin(), m_buffer.end()});

This may be an off by 1 error.

Repro case:
Consider the following, where str is of type std::string and is filled with 829 characters of anything:

Chocobo1::MD5 md5;
md5.addData(str.data(), str.length());
md5.finalize();
return md5.toString();

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.