DCPcrypt v2: Users Guide - Hash Algorithms

DCPcrypt Cryptographic Component Library v2
Copyright © 1999-2002 David Barton
http://www.cityinthesky.co.uk/
crypto@cityinthesky.co.uk

Hash Algorithms - TDCP_hash

All hashes are derived from the TDCP_hash component. It provides a range of functions to allow the hashing of virtually every type of data.

Functions available are:

          property Initialized: boolean;
          property Id: integer;
          property Algorithm: string;
          property HashSize: integer;

          class function SelfTest: boolean;

          procedure Init;
          procedure Final(var Digest);
          procedure Burn;

          procedure Update(const Buffer; Size: longword);
          procedure UpdateStream(Stream: TStream; Size: longword);
          procedure UpdateStr(const Str: string);

Example usage:

- Example 1 - File hashing.

property Initialized: boolean;

This is set to true after Init has been called.

property Id: integer;

Every algorithm I implement gets given a unique ID number so that if I use several different algorithms within a program I can determine which one was used. This is a purely arbitrary numbering system.

property Algorithm: string;

This is the name of the algorithm implemented in the component.

property HashSize: integer;

This is the size of the output of the hash algorithm in BITS.

class function SelfTest: boolean;

In order to test whether the implementations have all been compiled correctly you can call the SelfTest function. This compares the results of several hash operations with known results for the algorithms (so called test vectors). If all the tests are passed then true is returned. If ANY of the tests are failed then false is returned. You may want to run this function for all the components when you first install the DCPcrypt package and again if you modify any of the source files, you don't need to run this everytime your program is run. Note: this only performs a selection of tests, it is not exhaustive.

procedure Init;

Call this procedure to initialize the hash algorithm, this must be called before using the Update procedure.

procedure Final(var Digest);

This procedure returns the final message digest (hash) in Digest. This variable must be the same size as the hash size. This procedure also calls Burn to clear any stored information.

procedure Burn;

Call this procedure if you want to abort the hashing operation (normally Final is used). This clears all information stored within the hash. Before the hash can be used again Init must be called.

procedure Update(const Buffer; Size: longword);

This procedure hashes Size bytes of Buffer. To get the hash result call Final.

Update example:

      procedure HashBuffer(const Buffer; Size: longint; var Output);
      var
        Hash: TDCP_ripemd160;
      begin
        Hash:= TDCP_ripemd160.Create(nil);
        Hash.Init;
        Hash.Update(Buffer,Size);
        Hash.Final(Output);
        Hash.Free;
      end;

procedure UpdateStream(Stream: TStream; Size: longword);

This procedure hashes Size bytes from Stream. To get the hash result call Final.

procedure UpdateStr(const Str: string);

This procedure hashes the string Str. To get the hash result call Final.

Example 1 - File hashing

This example shows how you can hash the contents of a file

      procedure TForm1.Button1Click(Sender: TObject);
      var
        Hash: TDCP_ripemd160;
        Digest: array[0..19] of byte;  // RipeMD-160 produces a 160bit digest (20bytes)
        Source: TFileStream;
        i: integer;
        s: string;
      begin
        Source:= nil;
        try
          Source:= TFileStream.Create(Edit1.Text,fmOpenRead);  // open the file specified by Edit1
        except
          MessageDlg('Unable to open file',mtError,[mbOK],0);
        end;
        if Source <> nil then
        begin
          Hash:= TDCP_ripemd160.Create(Self);          // create the hash
          Hash.Init;                                   // initialize it
          Hash.UpdateStream(Source,Source.Size);       // hash the stream contents
          Hash.Final(Digest);                          // produce the digest
          Source.Free;
          s:= '';
          for i:= 0 to 19 do
            s:= s + IntToHex(Digest[i],2);
          Edit2.Text:= s;                              // display the digest
        end;
      end;

Index, Ciphers, Block Ciphers

DCPcrypt is copyrighted © 1999-2002 David Barton.
All trademarks are property of their respective owners.