Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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 a local clone.

std.digest.md

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

Category Functions
Template API MD5 
OOP API MD5Digest 
Helpers md5Of 

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.

CTFE: Digests do not work in CTFE

Authors:
Piotr Szturmaj, Kai Nacke, Johannes Pfau
The routines and algorithms are derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm.

References: Wikipedia on MD5

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

//Feeding data
ubyte[1024] data;
MD5 md5;
md5.start();
md5.put(data[]);
md5.start(); //Start again
md5.put(data[]);
auto hash = md5.finish();
Examples:
//OOP API
import std.digest.md;

auto md5 = new MD5Digest();
ubyte[] hash = md5.digest("abc");
assert(toHexString(hash) == "900150983CD24FB0D6963F7D28E17F72");

//Feeding data
ubyte[1024] data;
md5.put(data[]);
md5.reset(); //Start again
md5.put(data[]);
hash = md5.finish();
struct MD5;
Template API MD5 implementation. See std.digest.digest for differences between template and OOP API.
Examples:
//Simple example, hashing a string using md5Of helper function
ubyte[16] hash = md5Of("abc");
//Let's get a hash string
assert(toHexString(hash) == "900150983CD24FB0D6963F7D28E17F72");
Examples:
//Using the basic API
MD5 hash;
hash.start();
ubyte[1024] data;
//Initialize data here...
hash.put(data);
ubyte[16] result = hash.finish();
Examples:
//Let's use the template features:
void doSomething(T)(ref T hash) if(isDigest!T)
{
    hash.put(cast(ubyte)0);
}
MD5 md5;
md5.start();
doSomething(md5);
assert(toHexString(md5.finish()) == "93B885ADFE0DA089CDF634904FD59F71");
pure nothrow @nogc @trusted void put(scope const(ubyte)[] data...);
Use this to feed the digest with data. Also implements the std.range.primitives.isOutputRange interface for ubyte and const(ubyte)[].

Example:

MD5 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 @nogc @safe void start();
Used to (re)initialize the MD5 digest.

Note: For this MD5 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.

Example:

MD5 digest;
//digest.start(); //Not necessary
digest.put(0);

pure nothrow @nogc @trusted ubyte[16] finish();
Returns the finished MD5 hash. This also calls start to reset the internal state.
Examples:
//Simple example
MD5 hash;
hash.start();
hash.put(cast(ubyte)0);
ubyte[16] result = hash.finish();
auto md5Of(T...)(T data);
This is a convenience alias for std.digest.digest.digest using the MD5 implementation.
Examples:
ubyte[16] hash = md5Of("abc");
assert(hash == digest!MD5("abc"));
alias MD5Digest = std.digest.digest.WrapperDigest!(MD5).WrapperDigest;
OOP API MD5 implementation. See std.digest.digest for differences between template and OOP API.
This is an alias for std.digest.digest.WrapperDigest!MD5, see there for more information.
Examples:
//Simple example, hashing a string using Digest.digest helper function
auto md5 = new MD5Digest();
ubyte[] hash = md5.digest("abc");
//Let's get a hash string
assert(toHexString(hash) == "900150983CD24FB0D6963F7D28E17F72");
Examples:
//Let's use the OOP features:
void test(Digest dig)
{
 dig.put(cast(ubyte)0);
}
auto md5 = new MD5Digest();
test(md5);

//Let's use a custom buffer:
ubyte[16] buf;
ubyte[] result = md5.finish(buf[]);
assert(toHexString(result) == "93B885ADFE0DA089CDF634904FD59F71");