www.digitalmars.com Home | Search | D | Comments
Last update Mon Aug 22 2005
D
Language
Phobos
Comparisons


object

std
 std.base64
 std.boxer
 std.compiler
 std.conv
 std.ctype
 std.date
 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.windows

std.linux

std.c
 std.c.stdio

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.

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

void sum(ubyte[16] digest, void[] data)
Compute MD5 digest from data.

void printDigest(ubyte[16] digest)
Print MD5 digest to standard output.

struct MD5_CTX
Use when data to be digested is buffered.

void start()
Begins an MD5 message-digest operation.

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

void finish(ubyte[16] digest)
Ends an MD5 message-digest operation and writes the result to digest.

Example


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

import std.md5;
import std.string;
import std.c.stdio;

int main(char[][] args)
{
    for (int i = 1; i < args.length; i++)
	 MDFile(args[i]);
    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)
	printf("%.*s can't be opened\n", filename);
    else
    {
	context.start();
	while ((len = fread(buffer, 1, buffer.size, file)) != 0)
	    context.update(buffer[0 .. len]);
	context.finish(digest);
	fclose(file);

	printf("MD5 (%.*s) = ", filename);
	printDigest(digest);
	printf("\n");
    }
}

Feedback and Comments

Add feedback and comments regarding this page.
Copyright © 1999-2005 by Digital Mars, All Rights Reserved