Giter VIP home page Giter VIP logo

blake2-d's Introduction

blake2-d

This is a library written in D implementing the BLAKE2b and BLAKE2s hashing algorithms and is compatible with the Phobos Digest API (std.digest).

The BLAKE2 algorithm was introduced in 2015 as IETF RFC 7693. You can visit the website for more information.

Features (so far):

  • Supports BLAKE2b and BLAKE2s.
  • Custom digest sizes.
  • Keying at runtime (Template API).
  • Keying at runtime (OOP API).
  • Keying at compile-time (Template API).
  • Keying at compile-time (OOP API).
  • Support for BLAKE2bp and BLAKE2sp.

Notes:

  • May be incompatible with HMAC.
  • BLAKE2X was never finished, so it is not implemented (including XOF).

Compatible and tested with DMD, GDC, and LDC.

Pull Requests accepted.

If you would like to disclose a vulnerability, please consult SECURITY.md.

Usage

To include it in your project, simply import the blake2d package.

Digest API

If you are unfamiliar with the Digest API, here is a quick summary.

Two APIs are available: Template API and OOP API.

Template API

The template API uses a structure template and is a good choice if your application only plans to support one digest algorithm.

import std.cov : hexString;

BLAKE2b512 b2b512;
b2b512.put("abc");
assert(b2b512.finish() == cast(ubyte[]) hexString!(
    "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1"~
    "7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923"));
b2b512.start(); // reset
b2b512.put("abcdef");
assert(b2b512.finish() == cast(ubyte[]) hexString!(
    "dde410524e3569b303e494aa82a3afb3e426f9df24c1398e9ff87aafbc2f5b7b"~
    "3c1a4c9400409de3b45d37a00e5eae2a93cc9c4a108b00f05217d41a424d2b8a"));

OOP API

The OOP API uses a class (object) implementation and is a good choice if your application plans to support one or more digest algorithms.

import std.string : representation;
import std.conv : hexString;

Digest dgst = new BLAKE2b512Digest();
dgst.put("abc");
assert(dgst.finish() == cast(ubyte[]) hexString!(
    "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1"~
    "7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923"));
dgst.reset();
dgst.put("abcdef");
assert(dgst.finish() == cast(ubyte[]) hexString!(
    "dde410524e3569b303e494aa82a3afb3e426f9df24c1398e9ff87aafbc2f5b7b"~
    "3c1a4c9400409de3b45d37a00e5eae2a93cc9c4a108b00f05217d41a424d2b8a"));

There are numerous ways to avoid GC allocation. For example when only using a digest for a one-time use in a short scope, there's std.typecons.scoped.

Keying

A key can be supplied to the digest using the key function.

It must be supplied before putting data in.

import std.string : representation;
import std.conv : hexString;

// Key can be between 1 to 32 bytes for BLAKE2s256
// and 1 to 64 bytes for BLAKE2b512.
// Though recommended key sizes are their respective maximum sizes.
auto secret = hexString!(
    "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
    .representation;
// Vector from official suite.
auto data = hexString!("000102").representation;

BLAKE2s256 b2s;
b2s.key(secret);
b2s.put(data);

assert(b2s.finish().toHexString!(LetterCase.lower) ==
    "1d220dbe2ee134661fdf6d9e74b41704710556f2f6e5a091b227697445dbea6b");

License

Published under the Boost License 1.0.

blake2-d's People

Contributors

dd86k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jefferyq7

blake2-d's Issues

Include version string

I should be including the version number as a string (enum) for diagnostic and printing purposes.

Also because I don't have to manually type it in version pages and the like.

Like

enum BLAKE2D_VERSION_STRING = "0.1.1";

Fix HMAC usage

While the Digest API uses std.digest.hmac.HMAC for HMAC, which adds a secret hashed key using the template API, the way BLAKE2 does is just... Weird.

At initiation:

  • Parameter block index 0 byte 1 (little-endian) sets key size in bytes.
  • Key is XOR'd with state alongside IV (ulong for b, uint for s, see the inner_t alias).

Which is simply not possible with the HMAC structure template. Unless there's a hack I don't know of.

Add support for core.simd, intel-intrinsics, or inlined assembly

Waiting on: #2

Options:

  1. core.simd -- Supported everywhere, I think.
  2. intel-intrinsics DUB package -- Somewhat supports all compilers.
  3. Inlined assembly -- If all fails, at least x86 users would benefit. But limited to AVX/AV2 and not SSE* at best (because DMD).

Versions: Blake2dUseSIMD or Blake2dUseIntrinsics (user will have to manually assign version)

Redo structure template

Currently, because of the (rather silly) use of an enum for a structure template parameter, I cannot make inner_t types available outside the scope for compile-time keyed input.

The new idea would be:

  • struct BLAKE2b(uint digestSize = 512, ulong[8] key = null) defining inner parameters
  • struct BLAKE2s(uint digestSize = 256, uint[8] key = null) defining inner parameters
  • template BLAKE2Impl(T, ...) called from either (and possible p variants)

I think you get the idea. I'll do this on my free time.

Add key() function to WrapperDigest

The Digest API does not support keyed algorithms. Thankfully, WrapperDigest (OOP API) is a class template and I hope to inherit it to add a key function already found in the structure template (Template API).

Key can still be given to template structure anyway when initiating the class template.

This adds the key function to the Digest OOP API.

Concept

class WrapperDigestKeyed(T) if (isDigest!T) : WrapperDigest!T
{
    void key(ubyte[] input)
    {
        _digest.key(input);
    }
}

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.