tango.util.digest.MerkleDamgard

License:

BSD style: see doc/license.txt for details

Version:

Initial release: Feb 2006

Author:

Regan Heath, Oskar Linde

This module implements a generic Merkle-Damgard hash function
class MerkleDamgard : Digest [package]
Extending MerkleDamgard to create a custom hash function requires the implementation of a number of abstract methods. These include:
1
2
3
4
5
6
7
public uint digestSize();
protected void reset();
protected void createDigest(ubyte[] buf);
protected uint blockSize();
protected uint addSize();
protected void padMessage(ubyte[] data);
protected void transform(ubyte[] data);
In addition there exist two further abstract methods; these methods have empty default implementations since in some cases they are not required:
1
2
protected abstract void padLength(ubyte[] data, ulong length);
protected abstract void extend();

The method padLength() is required to implement the SHA series of Hash functions and also the Tiger algorithm. Method extend() is required only to implement the MD2 digest.

The basic sequence of internal events is as follows:
  • transform(), 0 or more times
  • padMessage()
  • padLength()
  • transform()
  • extend()
  • createDigest()
  • reset()
void createDigest(ubyte[] buf) [protected, abstract]
Constructs the digest

Parameters:

bufa buffer with enough space to hold the digest

Remarks:

Constructs the digest.
uint blockSize() [protected, abstract]
Digest block size

Returns:

the block size

Remarks:

Specifies the size (in bytes) of the block of data to pass to each call to transform().
uint addSize() [protected, @property, abstract]
Length padding size

Returns:

the length padding size

Remarks:

Specifies the size (in bytes) of the padding which uses the length of the data which has been fed to the algorithm, this padding is carried out by the padLength method.
void padMessage(ubyte[] data) [protected, abstract]
Pads the digest data

Parameters:

dataa slice of the digest buffer to fill with padding

Remarks:

Fills the passed buffer slice with the appropriate padding for the final call to transform(). This padding will fill the message data buffer up to blockSize()-addSize().
void padLength(ubyte[] data, ulong length) [protected]
Performs the length padding

Parameters:

datathe slice of the digest buffer to fill with padding
lengththe length of the data which has been processed

Remarks:

Fills the passed buffer slice with addSize() bytes of padding based on the length in bytes of the input data which has been processed.
void transform(const(ubyte[]) data) [protected, abstract]
Performs the digest on a block of data

Parameters:

datathe block of data to digest

Remarks:

The actual digest algorithm is carried out by this method on the passed block of data. This method is called for every blockSize() bytes of input data and once more with the remaining data padded to blockSize().
void extend() [protected]
Final processing of digest.

Remarks:

This method is called after the final transform just prior to the creation of the final digest. The MD2 algorithm requires an additional step at this stage. Future digests may or may not require this method.
this()
Construct a digest

Remarks:

Constructs the internal buffer for use by the digest, the buffer size (in bytes) is defined by the abstract method blockSize().
void reset() [protected]
Initialize the digest

Remarks:

Returns the digest state to its initial value
MerkleDamgard update(const(void[]) input) [override]
Digest additional data

Parameters:

inputthe data to digest

Remarks:

Continues the digest operation on the additional data.
ubyte[] binaryDigest(ubyte[] buf = null) [override]
Complete the digest

Returns:

the completed digest

Remarks:

Concludes the algorithm producing the final digest.
void littleEndian32(const(ubyte[]) input, uint[] output) [protected, static, final]
Converts 8 bit to 32 bit Little Endian

Parameters:

inputthe source array
outputthe destination array

Remarks:

Converts an array of ubyte[] into uint[] in Little Endian byte order.
void bigEndian32(const(ubyte[]) input, uint[] output) [protected, static, final]
Converts 8 bit to 32 bit Big Endian

Parameters:

inputthe source array
outputthe destination array

Remarks:

Converts an array of ubyte[] into uint[] in Big Endian byte order.
void littleEndian64(const(ubyte[]) input, ulong[] output) [protected, static, final]
Converts 8 bit to 64 bit Little Endian

Parameters:

inputthe source array
outputthe destination array

Remarks:

Converts an array of ubyte[] into ulong[] in Little Endian byte order.
void bigEndian64(const(ubyte[]) input, ulong[] output) [protected, static, final]
Converts 8 bit to 64 bit Big Endian

Parameters:

inputthe source array
outputthe destination array

Remarks:

Converts an array of ubyte[] into ulong[] in Big Endian byte order.
uint rotateLeft(uint x, uint n) [protected, static, final]
Rotate left by n

Parameters:

xthe value to rotate
nthe amount to rotate by

Remarks:

Rotates a 32 bit value by the specified amount.