PTypes: streams: outmd5

Top: Streams: outmd5

>     #include <pstreams.h>

>     class outmd5: outstm {
>         outmd5();
>         outmd5(outstm* outthru);
>         string get_digest();
>         const unsigned char* get_bindigest();
>     }

MD5, the message digest algorithm described in RFC 1321, computes a 128-bit sequence (sometimes called 'message digest', 'fingerprint' or 'MD5 checksum') from arbitrary data. As stated in RFC 1321, it is conjectured that it is computationally infeasible to produce two messages having the same message digest, or to produce any message having a given prespecified target message digest. MD5 can be viewed as a one-way encryption system and can be used, for example, to encrypt passwords in a password database.

The MD5 fingerprint is more often converted to so-called ASCII-64 form in order to conveniently store it in plain text environments and protocols. Thus, the 128-bit binary sequence becomes a 22-character text string consisting of letters, digits and two special symbols '.' and '/'. (Note that this is not the same as Base64 encoding in MIME).

In order to compute a MD5 fingerprint you first create a stream object of type outmd5 and then send data to it as if it was an ordinary output file or a socket stream. After you close the stream, you can obtain the fingerprint in ASCII-64 form using the object's get_digest() method.

The implementation of MD5 is derived from L. Peter Deutsch's work.

This class derives all public methods and properties from iobase and outstm, and in addition defines the following:

outmd5::outmd5() creates a bufferless MD5 stream.

outmd5::outmd5(outstm* outthru) creates a MD5 stream and attaches an output stream outthru to it. Everything sent to the MD5 stream will also be duplicated to outthru. You may want, for example, to attach perr to your MD5 stream for debugging purposes.

string outmd5::get_digest() closes the stream and returns the computed fingerprint in text form (ASCII-64).

const unsigned char* outmd5::get_bindigest() closes the stream and returns a pointer to a 16-byte buffer with the binary MD5 fingerprint.

Example:

>     string cryptpw(string username, string password)
>     {
>         outmd5 m;
>         m.open();
>         m.put(username);
>         m.put(password);
>         m.put("Banana with ketchup");
>         return m.get_digest();
>     }

See also: iobase, outstm

PTypes home