Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

std.digest.sha

Category Functions
Template API SHA1 
OOP API SHA1Digest 
Helpers sha1Of 

Computes SHA1 hashes of arbitrary data. SHA1 hashes are 20 byte quantities that are like a checksum or CRC, but are more robust.

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

This module publicly imports std.digest.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

References:

Source:
std/digest/sha.d

Examples:
 //Template API
 import std.digest.sha;

 ubyte[20] hash = sha1Of("abc");
 assert(toHexString(hash) == "A9993E364706816ABA3E25717850C26C9CD0D89D");

 //Feeding data
 ubyte[1024] data;
 SHA1 sha;
 sha.start();
 sha.put(data[]);
 sha.start(); //Start again
 sha.put(data[]);
 hash = sha.finish();
 //OOP API
 import std.digest.sha;

 auto sha = new SHA1Digest();
 ubyte[] hash = sha.digest("abc");
 assert(toHexString(hash) == "A9993E364706816ABA3E25717850C26C9CD0D89D");

 //Feeding data
 ubyte[1024] data;
 sha.put(data[]);
 sha.reset(); //Start again
 sha.put(data[]);
 hash = sha.finish();

struct SHA1;
Template API SHA1 implementation. See std.digest.digest for differences between template and OOP API.

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");
 //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 referece!
 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");

pure nothrow @trusted void start();
SHA1 initialization. Begins a SHA1 operation.

Note:
For this SHA1 Digest implementation calling start after default construction is not necessary. Calling start is only necessary to reset the Digest.

Generic code which deals with different Digest types should always call start though.

Examples:
 SHA1 digest;
 //digest.start(); //Not necessary
 digest.put(0);

pure nothrow @trusted void put(scope const(ubyte)[] input...);
Use this to feed the digest with data. Also implements the std.range.OutputRange interface for ubyte and const(ubyte)[].

Examples:
 SHA1 dig;
 dig.put(cast(ubyte)0); //single ubyte
 dig.put(cast(ubyte)0, cast(ubyte)0); //variadic
 ubyte[10] buf;
 dig.put(buf); //buffer

pure nothrow @trusted ubyte[20u] finish();
Returns the finished SHA1 hash. This also calls start to reset the internal state.

Examples:
 //Simple example
 SHA1 hash;
 hash.start();
 hash.put(cast(ubyte)0);
 ubyte[20] result = hash.finish();

auto sha1Of(T...)(T data);
This is a convenience alias for std.digest.digest.digest using the SHA1 implementation.

Examples:
 ubyte[20] hash = sha1Of("abc");
 assert(hash == digest!SHA1("abc")); //This is the same as above

alias SHA1Digest;
OOP API SHA1 implementation. See std.digest.digest for differences between template and OOP API.

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

Examples:
 //Simple example, hashing a string using Digest.digest helper function
 auto sha = new SHA1Digest();
 ubyte[] hash = sha.digest("abc");
 //Let's get a hash string
 assert(toHexString(hash) == "A9993E364706816ABA3E25717850C26C9CD0D89D");
 //Let's use the OOP features:
 void test(Digest dig)
 {
     dig.put(cast(ubyte)0);
 }
 auto sha = new SHA1Digest();
 test(sha);

 //Let's use a custom buffer:
 ubyte[20] buf;
 ubyte[] result = sha.finish(buf[]);
 assert(toHexString(result) == "5BA93C9DB0CFF93F52B521D7420E43F6EDA2784F");