std.digest.sha

Computes SHA1 and SHA2 hashes of arbitrary data. SHA hashes are 20 to 64 byte quantities (depending on the SHA algorithm) that are like a checksum or CRC, but are more robust.

SHA2 comes in several different versions, all supported by this module: SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224 and SHA-512/256.

This module conforms to the APIs defined in std.digest. To understand the differences between the template and the OOP API, see std.digest.

This module publicly imports std.digest and can be used as a stand-alone module.

License

Boost License 1.0.

CTFE: Digests do not work in CTFE

Authors

The routines and algorithms are derived from the Secure Hash Signature Standard (SHS) (FIPS PUB 180-2).

Kai Nacke, Johannes Pfau, Nick Sabalausky

References:

Source: std/digest/sha.d

Types 15

structSHA(uint hashBlockSize, uint digestSize)

Template API SHA1/SHA2 implementation. Supports: SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224 and SHA-512/256.

The hashBlockSize and digestSize are in bits. However, it's likely easier to simply use the convenience aliases: SHA1, SHA224, SHA256, SHA384, SHA512, SHA512_224 and SHA512_256.

See std.digest for differences between template and OOP API.

Fields
hashBlockSize blockSize
ulong[blockSize / 512] count
ubyte[blockSize / 8] buffer
ubyte[128] padding
Methods
T Ch(T)(T x, T y, T z)
T Maj(T)(T x, T y, T z)
uint Parity(uint x, uint y, uint z)
uint BigSigma0(uint x)
uint BigSigma1(uint x)
uint SmSigma0(uint x)
uint SmSigma1(uint x)
ulong BigSigma0(ulong x)
ulong BigSigma1(ulong x)
ulong SmSigma0(ulong x)
ulong SmSigma1(ulong x)
void T_0_15(int i, const(ubyte[64]) * input, ref uint[16] W, uint A, ref uint B, uint C, uint D, uint E, ref uint T) pure nothrow @nogc
void T_16_19(int i, ref uint[16] W, uint A, ref uint B, uint C, uint D, uint E, ref uint T) pure nothrow @nogc
void T_20_39(int i, ref uint[16] W, uint A, ref uint B, uint C, uint D, uint E, ref uint T) pure nothrow @nogc
void T_40_59(int i, ref uint[16] W, uint A, ref uint B, uint C, uint D, uint E, ref uint T) pure nothrow @nogc
void T_60_79(int i, ref uint[16] W, uint A, ref uint B, uint C, uint D, uint E, ref uint T) pure nothrow @nogc
private void transformX86(uint[5] * state, const(ubyte[64]) * block) pure nothrow @nogc
void T_SHA2_0_15(Word)(int i, const(ubyte[blockSize / 8]) * input, ref Word[16] W, Word A, Word B, Word C, ref Word D, Word E, Word F, Word G, ref Word H, Word K) pure nothrow @nogc
void T_SHA2_16_79(Word)(int i, ref Word[16] W, Word A, Word B, Word C, ref Word D, Word E, Word F, Word G, ref Word H, Word K) pure nothrow @nogc
private void transformSHA2(Word)(Word[8] * state, const(ubyte[blockSize / 8]) * block) pure nothrow @nogc
void start() @safe pure nothrow @nogcSHA initialization. Begins an SHA1/SHA2 operation.
void put(scope const(ubyte)[] input...) @trusted pure nothrow @nogcUse this to feed the digest with data. Also implements the isOutputRange interface for `ubyte` and `const(ubyte)[]`.
ubyte[digestSize / 8] finish() @trusted pure nothrow @nogcReturns the finished SHA hash. This also calls start to reset the internal state.
aliasSHA1 = SHA!(512, 160)

SHA alias for SHA-1, hash is ubyte[20]

aliasSHA224 = SHA!(512, 224)

SHA alias for SHA-224, hash is ubyte[28]

aliasSHA256 = SHA!(512, 256)

SHA alias for SHA-256, hash is ubyte[32]

aliasSHA384 = SHA!(1024, 384)

SHA alias for SHA-384, hash is ubyte[48]

aliasSHA512 = SHA!(1024, 512)

SHA alias for SHA-512, hash is ubyte[64]

aliasSHA512_224 = SHA!(1024, 224)

SHA alias for SHA-512/224, hash is ubyte[28]

aliasSHA512_256 = SHA!(1024, 256)

SHA alias for SHA-512/256, hash is ubyte[32]

OOP API SHA1 and SHA2 implementations. See std.digest for differences between template and OOP API.

This is an alias for WrapperDigest!SHA1, see there for more information.

Functions 7

fnauto sha1Of(T...)(T data)These are convenience aliases for digest using the SHA implementation.
fnauto sha224Of(T...)(T data)ditto
fnauto sha256Of(T...)(T data)ditto
fnauto sha384Of(T...)(T data)ditto
fnauto sha512Of(T...)(T data)ditto
fnauto sha512_224Of(T...)(T data)ditto
fnauto sha512_256Of(T...)(T data)ditto