D Programming Language 2.0

- Comments
- D
- Search
- Home

Last update Wed Jul 9 01:24:33 2008

# 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.

    import std.md5;

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

    void main(string[] args)
    {
        foreach (char[] arg; args)
    	 MDFile(arg);
    }

    /* Digests a file and prints the result. */
    void MDFile(string filename)
    {
        FILE* file = enforce(fopen(filename), "Could not open file `"~filename~"'");
        scope(exit) fclose(file);
        ubyte digest[16];

        MD5_CTX context;
        context.start();
        foreach (ubyte buffer; chunks(file, 4096 * 1024))
            context.update(buffer);
        context.finish(digest);
        writefln("MD5 (%s) = %s", filename, digestToString(digest));
    }

void sum(ubyte[16u] digest, in void[][] data...);
  Computes MD5 digest of several arrays of data.
void printDigest(const ubyte[16u] digest);
  Prints a message digest in hexadecimal to stdout.
string digestToString(const ubyte[16u] digest);
  Converts MD5 digest to a string.
string getDigestString(in void[][] data...);
  Gets the digest of all data items passed in.
  Example:

      string a = "Mary has ", b = "a little lamb";
      int[] c = [ 1, 2, 3, 4, 5 ];
      string d = getDigestString(a, b, c);

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(const void[] input);
    MD5 block update operation. Continues an MD5 message-digest operation, processing another message block, and updating the context.
  void finish(ubyte[16u] digest);
    MD5 finalization. Ends an MD5 message-digest operation, writing the the message to digest and zeroing the context.

Copyright © 1999-2008 by Digital Mars, All Rights Reserved | Page generated by Ddoc.