www.digitalmars.com Home | Search | D | Comments
Last update Sat Mar 18 23:51:28 2006
D
Language
Phobos
Comparisons


object

std
 std.base64
 std.boxer
 std.compiler
 std.conv
 std.cover
 std.ctype
 std.date
 std.demangle
 std.file
 std.format
 std.gc
 std.intrinsic
 std.math
 std.md5
 std.mmfile
 std.openrj
 std.outbuffer
 std.path
 std.process
 std.random
 std.recls
 std.regexp
 std.socket
 std.socketstream
 std.stdint
 std.stdio
 std.cstream
 std.stream
 std.string
 std.system
 std.thread
 std.uri
 std.utf
 std.zip
 std.zlib
 std.c.fenv
 std.c.math
 std.c.process
 std.c.stdarg
 std.c.stddef
 std.c.stdio
 std.c.stdlib
 std.c.string
 std.c.time
 std.c.wcharh
 std.windows.charset

std.windows

std.linux

std.c.windows

std.c.linux

std.md5

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

There are two ways to do this. The first does it all in one function call to sum(). The second is for when the data is buffered.

BUGS:
MD5 digests have been demonstrated to not be unique.

Author:
The routines and algorithms are derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm.

References:
Wikipedia on MD5

Example:
// This code is derived from the
// RSA Data Security, Inc. MD5 Message-Digest Algorithm.

module std.md5;

private import std.stdio;
private import std.string;
private import std.c.stdio;
private import std.c.string;

int main(char[][] args)
{
    foreach (char[] arg; args)
	 MDFile(arg);
    return 0;
}

/* Digests a file and prints the result. */
void MDFile(char[] filename)
{
    FILE* file;
    MD5_CTX context;
    int len;
    ubyte[4 * 1024] buffer;
    ubyte digest[16];

    if ((file = fopen(std.string.toStringz(filename), "rb")) == null)
	writefln("%s can't be opened", filename);
    else
    {
	context.start();
	while ((len = fread(buffer, 1, buffer.sizeof, file)) != 0)
	    context.update(buffer[0 .. len]);
	context.finish(digest);
	fclose(file);

	writefln("MD5 (%s) = %s", filename, digestToString(digest));
    }
}


void sum(ubyte[16] digest, void[] data);
Computes MD5 digest of array of data.

void printDigest(ubyte[16] digest);
Prints a message digest in hexadecimal to stdout.

char[] digestToString(ubyte[16] digest);
Converts MD5 digest to a string.

struct MD5_CTX;
Holds context of MD5 computation.

Used when data to be digested is buffered.

void start();
MD5 initialization. Begins an MD5 operation, writing a new context.

void update(void[] input);
MD5 block update operation. Continues an MD5 message-digest operation, processing another message block, and updating the context.

void finish(ubyte[16] digest);
MD5 finalization. Ends an MD5 message-digest operation, writing the the message to digest and zeroing the context.