Giter VIP home page Giter VIP logo

sha-2's Introduction

SHA-2 cryptographic hash functions

This is a header-only, C++11 implementation of the SHA-2 cryptographic hash functions (see disclaimer below) as put forward in NIST's FIPS 180-4 and the C example code in RFC 6234.

Note, this code is designed to run on little-endian systems only.

Integration

The single header file, sha2.hpp is in the src directory. Integration is simply including the header,

#include "sha2.hpp"

in the relevant files. C++11 or later must be enabled (e.g., -std=c++11, -std=c++14, -std=c++17 or -std=c++2a for GCC and Clang).

Example

A simple example of hashing an ASCII string follows,

int main()
{
    const char* const text = "abc";

    auto ptr = reinterpret_cast<const uint8_t*>(text);
    auto hash = sha2::sha512(ptr, strlen(text));

    // Print the hash
    for (auto c : hash) {
        printf("%02x", c);
    }
}

An example use can be found in main.cpp, which prints out test hashes.

Reference

Everything is within the sha2 namespace.

Types

Each hash is a typedef for a byte array of the appropriate size:

using sha224_hash = std::array<uint8_t, 28>;
using sha256_hash = std::array<uint8_t, 32>;
using sha384_hash = std::array<uint8_t, 48>;
using sha512_hash = std::array<uint8_t, 64>;

Functions

Each function takes a pointer to some data and a length while returning the corresponding hash. These functions are designed for simplicity and so hash everything at once. There is no streaming solution; the hashing cannot be resumed.

sha224_hash sha224(const uint8_t* data, uint64_t length);
sha256_hash sha256(const uint8_t* data, uint64_t length);
sha384_hash sha384(const uint8_t* data, uint64_t length);
sha512_hash sha512(const uint8_t* data, uint64_t length);

sha224_hash sha512_224(const uint8_t* data, uint64_t length);
sha256_hash sha512_256(const uint8_t* data, uint64_t length);

template <int bits>
std::array<uint8_t, bits / 8> sha512_t(const uint8_t* data, uint64_t length);

sha512_t accepts bit lengths that are a multiple of eight and range between eight and 512. A bit length of 384 is explicitly forbidden, sha384 should be used in that case.

Disclaimer

While the code is based on the RFC and tests show that the hashes match what is expected, this code should not be considered production ready. Do not use this, without further vetting, in security contexts. It was written for educational purposes, primarily for myself but hopefully others too. Performance was not a primary concern.

Licence

Licensed under the MIT Licence:

Copyright © 2018 Martyn Afford

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

sha-2's People

Contributors

martynafford avatar

Watchers

 avatar

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.