SHA

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.

Members

Functions

finish
ubyte[digestSize / 8] finish()

Returns the finished SHA hash. This also calls start to reset the internal state.

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

Use this to feed the digest with data. Also implements the std.range.primitives.isOutputRange interface for ubyte and const(ubyte)[].

start
void start()

SHA initialization. Begins an SHA1/SHA2 operation.

Examples

//Simple example, hashing a string using sha1Of helper function
ubyte[20] hash = sha1Of("abc");
//Let's get a hash string
assert(toHexString(hash) == "A9993E364706816ABA3E25717850C26C9CD0D89D");

//The same, but using SHA-224
ubyte[28] hash224 = sha224Of("abc");
assert(toHexString(hash224) == "23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7");
//Using the basic API
SHA1 hash;
hash.start();
ubyte[1024] data;
//Initialize data here...
hash.put(data);
ubyte[20] result = hash.finish();
//Let's use the template features:
//Note: When passing a SHA1 to a function, it must be passed by reference!
void doSomething(T)(ref T hash)
if (isDigest!T)
{
  hash.put(cast(ubyte) 0);
}
SHA1 sha;
sha.start();
doSomething(sha);
assert(toHexString(sha.finish()) == "5BA93C9DB0CFF93F52B521D7420E43F6EDA2784F");

Meta