Digest::MD4 - Perl interface to the RSA Data Security Inc. MD4 Message-Digest Algorithm

- NAME
- SYNOPSIS
- DESCRIPTION
- EXAMPLES
- NOTE
- AUTHOR
- SEE ALSO

# NAME

Digest::MD4 - Perl interface to the RSA Data Security Inc. MD4 Message-Digest Algorithm

# SYNOPSIS

        use Digest::MD4;

        $context = new Digest::MD4;
        $context->reset();

        $context->add(LIST);
        $context->addfile(HANDLE);

        $digest = $context->digest();
        $string = $context->hexdigest();

        $digest = Digest::MD4->hash(SCALAR);
        $string = Digest::MD4->hexhash(SCALAR);

# DESCRIPTION

The Digest::MD4 module allows you to use the RSA Data Security Inc. MD4 Message Digest algorithm from within Perl programs.

A new MD4 context object is created with the new operation. Multiple simultaneous digest contexts can be maintained, if desired. The context is updated with the add operation which adds the strings contained in the LIST parameter. Note, however, that `add('foo', 'bar')`, `add('foo')` followed by `add('bar')` and `add('foobar')` should all give the same result.

The final message digest value is returned by the digest operation as a 16-byte binary string. This operation delivers the result of add operations since the last new or reset operation. Note that the digest operation is effectively a destructive, read-once operation. Once it has been performed, the context must be reset before being used to calculate another digest value.

Several convenience functions are also provided. The addfile operation takes an open file-handle and reads it until end-of file in 1024 byte blocks adding the contents to the context. The file-handle can either be specified by name or passed as a type-glob reference, as shown in the examples below. The hexdigest operation calls digest and returns the result as a printable string of hexdecimal digits. This is exactly the same operation as performed by the unpack operation in the examples below.

The hash operation can act as either a static member function (ie you invoke it on the MD4 class as in the synopsis above) or as a normal virtual function. In both cases it performs the complete MD4 cycle (reset, add, digest) on the supplied scalar value. This is convenient for handling small quantities of data. When invoked on the class a temporary context is created. When invoked through an already created context object, this context is used. The latter form is slightly more efficient. The hexhash operation is analogous to hexdigest.

# EXAMPLES

        use Digest::MD4;

        $md4 = new Digest::MD4;
        $md4->add('foo', 'bar');
        $md4->add('baz');
        $digest = $md4->digest();

        print("Digest is " . unpack("H*", $digest) . "\n");

The above example would print out the message

        Digest is 6df23dc03f9b54cc38a0fc1483df6e21

provided that the implementation is working correctly.

Remembering the Perl motto (``There's more than one way to do it''), the following should all give the same result:

        use Digest::MD4;
        $md4 = new Digest::MD4;

        die "Can't open /etc/passwd ($!)\n" unless open(P, "/etc/passwd");

        seek(P, 0, 0);
        $md4->reset;
        $md4->addfile(P);
        $d = $md4->hexdigest;
        print "addfile (handle name) = $d\n";

        seek(P, 0, 0);
        $md4->reset;
        $md4->addfile(\*P);
        $d = $md4->hexdigest;
        print "addfile (type-glob reference) = $d\n";

        seek(P, 0, 0);
        $md4->reset;
        while (<P>)
        {
            $md4->add($_);
        }
        $d = $md4->hexdigest;
        print "Line at a time = $d\n";

        seek(P, 0, 0);
        $md4->reset;
        $md4->add(<P>);
        $d = $md4->hexdigest;
        print "All lines at once = $d\n";

        seek(P, 0, 0);
        $md4->reset;
        while (read(P, $data, (rand % 128) + 1))
        {
            $md4->add($data);
        }
        $d = $md4->hexdigest;
        print "Random chunks = $d\n";

        seek(P, 0, 0);
        $md4->reset;
        undef $/;
        $data = <P>;
        $d = $md4->hexhash($data);
        print "Single string = $d\n";

        close(P);

# NOTE

The MD4 extension may be redistributed under the same terms as Perl. The MD4 algorithm is defined in RFC1320. The basic C code implementing the algorithm is derived from that in the RFC and is covered by the following copyright:

       Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.

       License to copy and use this software is granted provided that it
       is identified as the "RSA Data Security, Inc. MD4 Message-Digest
       Algorithm" in all material mentioning or referencing this software
       or this function.

       License is also granted to make and use derivative works provided
       that such works are identified as "derived from the RSA Data
       Security, Inc. MD4 Message-Digest Algorithm" in all material
       mentioning or referencing the derived work.

       RSA Data Security, Inc. makes no representations concerning either
       the merchantability of this software or the suitability of this
       software for any particular purpose. It is provided "as is"
       without express or implied warranty of any kind.

       These notices must be retained in any copies of any part of this
       documentation and/or software.

This copyright does not prohibit distribution of any version of Perl containing this extension under the terms of the GNU or Artistic licences.

# AUTHOR

The MD4 interface was adapted by Mike McCauley (`mikem@open.com.au`), based entirely on MD5-1.7, written by Neil Winton (`N.Winton@axion.bt.co.uk`).

# SEE ALSO

perl(1).

Digest::MD4 - Perl interface to the RSA Data Security Inc. MD4 Message-Digest Algorithm