KECCAK

Template API SHA-3/SHAKE implementation using the Keccak[1600] function.

Supports SHA-3-224, SHA-3-256, SHA-3-384, SHA-3-512, SHAKE-128, and SHAKE-256. It is recommended to use the SHA3_224, SHA3_256, SHA3_384, SHA3_512, SHAKE128, and SHAKE256 template aliases respectively.

To make a XOF (like SHAKE-128/256):

alias SHAKE128_256 = KECCAK!(128, 256);
alias SHAKE128_256Digest = WrapperDigest!SHAKE128_256;
auto shake128_256Of(T...)(T data) { return digest!(SHAKE128_256, T)(data); }

Members

Functions

finish
ubyte[digestSizeBytes] finish()

Returns the finished hash. This also clears part of the state, leaving just the final digest.

put
void put(const(ubyte)[] input)

Feed the algorithm with data. Also implements the std.range.primitives.isOutputRange interface for ubyte and const(ubyte)[].

start
void start()

Initiate or reset the state of the structure.

Manifest constants

blockSize
enum blockSize;

Digest size in bits.

Unions

__anonymous
union __anonymous
Undocumented in source.

Parameters

digestSize

Digest size in bits.

shake

Sets SHAKE XOF digest output. For example, KECCAK!(128, 256) results in SHAKE-128/256. Defaults to 0 for SHA-3.

Examples

Test against empty datasets

import std.ascii : LetterCase;

assert(toHexString!(LetterCase.lower)(sha3_224Of("")) ==
    "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7");

assert(toHexString!(LetterCase.lower)(sha3_256Of("")) ==
    "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a");

assert(toHexString!(LetterCase.lower)(sha3_384Of("")) ==
    "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2a"~
    "c3713831264adb47fb6bd1e058d5f004");

assert(toHexString!(LetterCase.lower)(sha3_512Of("")) ==
    "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a6"~
    "15b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26");

assert(toHexString!(LetterCase.lower)(shake128Of("")) ==
    "7f9c2ba4e88f827d616045507605853e");

assert(toHexString!(LetterCase.lower)(shake256Of("")) ==
    "46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f");

Of wrappers + toHexString

import std.ascii : LetterCase;

assert(toHexString!(LetterCase.lower)(sha3_224Of("abc")) ==
    "e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf");

assert(toHexString!(LetterCase.lower)(sha3_256Of("abc")) ==
    "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532");

assert(toHexString!(LetterCase.lower)(sha3_384Of("abc")) ==
    "ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b2"~
    "98d88cea927ac7f539f1edf228376d25");

assert(toHexString!(LetterCase.lower)(sha3_512Of("abc")) ==
    "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e"~
    "10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0");

Structure functions

SHA3_224 hash;
hash.start();
ubyte[1024] data;
hash.put(data);
ubyte[28] result = hash.finish();

Template features.

import std.ascii : LetterCase;

// NOTE: When passing a digest to a function, it must be passed by reference!
void doSomething(T)(ref T hash)
    if (isDigest!T)
    {
        hash.put(cast(ubyte) 0);
    }
SHA3_224 sha;
sha.start();
doSomething(sha);
assert(toHexString!(LetterCase.lower)(sha.finish()) ==
    "bdd5167212d2dc69665f5a8875ab87f23d5ce7849132f56371a19096");

Meta