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.md
Category | Functions |
---|---|
Template API | MD5 |
OOP API | MD5Digest |
Helpers | md5Of |
Boost License 1.0 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 Source:
std/digest/md.d Examples:
//Template API import std.digest.md; ubyte[16] hash = md5Of("abc"); assert(toHexString(hash) == "900150983CD24FB0D6963F7D28E17F72"); //Feeding data ubyte[1024] data; MD5 md5; md5.start(); md5.put(data[]); md5.start(); //Start again md5.put(data[]); hash = md5.finish();
//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");
//Using the basic API MD5 hash; hash.start(); ubyte[1024] data; //Initialize data here... hash.put(data); ubyte[16] result = hash.finish();
//Let's use the template features: //Note: When passing a MD5 to a function, it must be passed by referece! 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 @trusted void put(scope const(ubyte)[] data...);
- Use this to feed the digest with data.
Also implements the std.range.OutputRange interface for ubyte and
const(ubyte)[].
Examples:
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 @trusted 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. Examples:MD5 digest; //digest.start(); //Not necessary digest.put(0);
- pure nothrow @trusted ubyte[16LU] 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")); //This is the same as above
- alias std.digest.digest.WrapperDigest!(MD5).WrapperDigest MD5Digest;
- 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
std.digest.digest.WrapperDigest 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");
//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");