Extending MerkleDamgard to create a custom hash function requires
the implementation of a number of abstract methods. These include:
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
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()
- protected abstract void createDigest(ubyte[] buf);
- Constructs the digest
Params:
ubyte[] buf |
a buffer with enough space to hold the digest |
Remarks:
Constructs the digest.
- protected abstract uint blockSize();
- 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().
- protected abstract @property uint addSize();
- 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.
- protected abstract void padMessage(ubyte[] data);
- Pads the digest data
Params:
ubyte[] data |
a 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().
- protected void padLength(ubyte[] data, ulong length);
- Performs the length padding
Params:
ubyte[] data |
the slice of the digest buffer to fill with padding |
ulong length |
the 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.
- protected abstract void transform(const(ubyte[]) data);
- Performs the digest on a block of data
Params:
const(ubyte[]) data |
the 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().
- protected void extend();
- 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().
- protected void reset();
- Initialize the digest
Remarks:
Returns the digest state to its initial value
- MerkleDamgard update(const(void[]) input);
- Digest additional data
Params:
const(void[]) input |
the data to digest |
Remarks:
Continues the digest operation on the additional data.
- ubyte[] binaryDigest(ubyte[] buf = null);
- Complete the digest
Returns:
the completed digest
Remarks:
Concludes the algorithm producing the final digest.
- protected static final void littleEndian32(const(ubyte[]) input, uint[] output);
- Converts 8 bit to 32 bit Little Endian
Params:
const(ubyte[]) input |
the source array |
uint[] output |
the destination array |
Remarks:
Converts an array of ubyte[] into uint[] in Little Endian byte order.
- protected static final void bigEndian32(const(ubyte[]) input, uint[] output);
- Converts 8 bit to 32 bit Big Endian
Params:
const(ubyte[]) input |
the source array |
uint[] output |
the destination array |
Remarks:
Converts an array of ubyte[] into uint[] in Big Endian byte order.
- protected static final void littleEndian64(const(ubyte[]) input, ulong[] output);
- Converts 8 bit to 64 bit Little Endian
Params:
const(ubyte[]) input |
the source array |
ulong[] output |
the destination array |
Remarks:
Converts an array of ubyte[] into ulong[] in Little Endian byte order.
- protected static final void bigEndian64(const(ubyte[]) input, ulong[] output);
- Converts 8 bit to 64 bit Big Endian
Params:
const(ubyte[]) input |
the source array |
ulong[] output |
the destination array |
Remarks:
Converts an array of ubyte[] into ulong[] in Big Endian byte order.
- protected static final uint rotateLeft(uint x, uint n);
- Rotate left by n
Params:
uint x |
the value to rotate |
uint n |
the amount to rotate by |
Remarks:
Rotates a 32 bit value by the specified amount.