CTFEDigests do not work in CTFE
ReferencesWikipedia on MD5
Sourcestd/digest/md.d
//Template APIimport std.digest.md;//Feeding dataubyte[1024] data;MD5 md5;md5.start();md5.put(data[]);md5.start();//Start againmd5.put(data[]);auto hash = md5.finish();
//OOP APIimport std.digest.md;auto md5 =new MD5Digest();ubyte[] hash = md5.digest("abc");writeln(toHexString(hash));// "900150983CD24FB0D6963F7D28E17F72"//Feeding dataubyte[1024] data;md5.put(data[]);md5.reset();//Start againmd5.put(data[]);hash = md5.finish();
MD5;//Simple example, hashing a string using md5Of helper functionubyte[16] hash = md5Of("abc");//Let's get a hash stringwriteln(toHexString(hash));// "900150983CD24FB0D6963F7D28E17F72"
//Using the basic APIMD5 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"
put(scope const(ubyte)[]data...);Example
MD5 dig;dig.put(cast(ubyte) 0);//single ubytedig.put(cast(ubyte) 0,cast(ubyte) 0);//variadicubyte[10] buf;dig.put(buf);//buffer
start();NoteFor 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 necessarydigest.put(0);finish();//Simple exampleMD5 hash;hash.start();hash.put(cast(ubyte) 0);ubyte[16] result = hash.finish();
md5Of(T...)(Tdata);ubyte[16] hash =md5Of("abc");writeln(hash);// digest!MD5("abc")
MD5Digest = std.digest.WrapperDigest!(MD5).WrapperDigest;//Simple example, hashing a string using Digest.digest helper functionauto md5 =newMD5Digest();ubyte[] hash = md5.digest("abc");//Let's get a hash stringwriteln(toHexString(hash));// "900150983CD24FB0D6963F7D28E17F72"
//Let's use the OOP features:void test(Digest dig){ dig.put(cast(ubyte) 0);}auto md5 =newMD5Digest();test(md5);//Let's use a custom buffer:ubyte[16] buf;ubyte[] result = md5.finish(buf[]);writeln(toHexString(result));// "93B885ADFE0DA089CDF634904FD59F71"