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. 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.
//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();
//OOP API
import std.digest.md;
auto md5 = new MD5Digest();
ubyte[] hash = md5.digest("abc");
writeln(toHexString(hash)); // "900150983CD24FB0D6963F7D28E17F72"
//Feeding data
ubyte[1024] data;
md5.put(data[]);
md5.reset(); //Start again
md5.put(data[]);
hash = md5.finish();
Template API MD5 implementation. See std.digest for differences between template and OOP API.
//Simple example, hashing a string using md5Of helper function
ubyte[16] hash = md5Of("abc");
//Let's get a hash string
writeln(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:
void doSomething(T)(ref T hash)
if (isDigest!T)
{
hash.put(cast(ubyte) 0);
}
MD5 md5;
md5.start();
doSomething(md5);
writeln(toHexString(md5.finish())); // "93B885ADFE0DA089CDF634904FD59F71"
Use this to feed the digest with data. Also implements the std.range.primitives.isOutputRange interface for ubyte and const(ubyte)[].
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
Used to (re)initialize the MD5 digest.
MD5 digest; //digest.start(); //Not necessary digest.put(0);
Returns the finished MD5 hash. This also calls start to reset the internal state.
//Simple example MD5 hash; hash.start(); hash.put(cast(ubyte) 0); ubyte[16] result = hash.finish();
This is a convenience alias for std.digest.digest using the MD5 implementation.
ubyte[16] hash = md5Of("abc");
writeln(hash); // digest!MD5("abc")
OOP API MD5 implementation. See std.digest for differences between template and OOP API.
This is an alias for std.digest.WrapperDigest!MD5, see there for more information.
//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
writeln(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[]);
writeln(toHexString(result)); // "93B885ADFE0DA089CDF634904FD59F71"
© 1999–2019 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_digest_md.html