tribble.security
Class ZoidbergDHash1

java.lang.Object
  extended byjava.security.MessageDigestSpi
      extended byjava.security.MessageDigest
          extended bytribble.security.ZoidbergDHash1

public class ZoidbergDHash1
extends java.security.MessageDigest

Zoidberg-D1 message digest (cryptographic hash). Computes a 160-bit (20-byte) message digest (a binary hash) from a series of input bytes.

The digest algorithm is designed to produce a unique hash vector for any given input byte stream, such that altering a single bit in the input stream results in a completely different hash value.

An input stream of bytes fed into the digest algorithm are hashed 16 bytes (128 bits) at a time. After all input bytes have been fed into the algorithm, the input is padded with a trailing count byte, and then enough zero bytes to make the total input length a multiple of 16 bytes (128 bits). The result of the digest algorithm is a 20-byte (160-bit) output byte vector.

This digest algorithm can be used as the basis of a digital signature scheme.

Note that none of these methods is synchronized.

Test Vectors

An empty input vector of zero bytes hashes to a message digest value of
+REDO +REDO {E0 47 A9 DC 13 99 0F A3 06 82 21 D8 7E BA 28 0B 44 17 9A 27}.

A single input byte of
{00}
hashes to a message digest value of
+REDO +REDO {C6 E3 BF 9B B1 B4 2F A0 C7 E5 59 07 A8 5D 6E AB DF 56 BB D0}.

A single input byte of
{01}
hashes to a message digest value of
+REDO +REDO {C0 AA 48 59 F4 65 30 8B 22 EF 59 D1 0D 5E 10 AD 38 16 BC D9}.

Two input bytes of
{0D 0A}
hash to a message digest value of
+REDO +REDO {1D 38 C5 A4 8E A2 74 0F 9A 13 CE 30 76 CD 47 77 04 A9 D7 18}.

Four input bytes of
{01 02 03 04}
hash to a message digest value of
+REDO +REDO {63 48 17 EE 0D 9C BD A9 47 0D 4F 86 9D 3B B0 5E 24 2C D0 94}.

16 input bytes of all zero bytes
{00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00}
hash to a message digest value of
+REDO +REDO {D8 C0 4B 69 86 C9 1F 1A 82 F4 EC B4 88 82 20 7F 7A B8 50 9C}.

16 input bytes of
{01 02 03 04 01 02 03 04 01 02 03 04 01 02 03 04}
hash to a message digest value of
+REDO +REDO {D8 C0 4B 69 86 C9 1F 1A 82 F4 EC B4 88 82 20 7F 7A B8 50 9C}.

16 input bytes of
{00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F}
hash to a message digest value of
+REDO +REDO {A9 3D 79 C7 54 B1 64 65 EF 96 7E 42 AD 5A 8F 67 E3 DC 52 F4}.

16 input bytes of
{00 01 02 03 04 25 06 07 08 09 0A 0B 0C 0D 0E 0F}
hash to a message digest value of
+REDO +REDO {6D 42 D0 76 09 E8 66 EB 21 54 B7 F7 F3 5E DD 2B E0 E4 FE 22}.

17 input bytes of
{00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00}
hash to a message digest value of
+REDO +REDO {46 4E 72 EF 41 BD 22 75 4E CF 3F 06 92 F5 D9 BE C9 61 9A 91}.

16 input bytes of
{44 61 76 69 64 20 52 2E 20 54 72 69 62 62 6C 65}
hash to a message digest value of
+REDO +REDO {69 D6 98 77 6D 38 36 31 67 11 64 78 7A 14 F5 27 C4 10 8A A2}.

Acknowledgments

This code is based on an original algorithm invented by David R. Tribble in March 2002.

License is granted to use this algorithm without fees or restrictions for all private and commercial uses.

Since:
2004-12-30
Version:
$Revision$ $Date: 2004-12-30 $
Author:
David R. Tribble (david@tribble.com).
Copyright ©2004 by David R. Tribble, all rights reserved.
Permission is granted to freely use and distribute this source code provided that the original copyright and authorship notices remain intact.
See Also:
ZoidbergCHash, ZoidbergCipher, MessageDigest

Constructor Summary
ZoidbergDHash1()
          Constructor.
 
Method Summary
 java.lang.Object clone()
          Clone this message digest, making a copy of the contents accumulated up to this point.
 byte[] digest()
          Compute the final value of this message digest.
 byte[] digest(byte[] in)
          Hash an array of bytes, updating and computing the final message digest.
protected  byte[] engineDigest()
          Compute the final value of this message digest.
protected  int engineGetDigestLength()
          Retrieve the size, in bytes, of this message digest.
protected  void engineReset()
          Reset this message digest, clearing the results of any previously accumulated hashed input data.
protected  void engineUpdate(byte in)
          Hash a single byte, updating this message digest.
protected  void engineUpdate(byte[] in, int off, int len)
          Hash an array of bytes, updating this message digest.
protected  byte[] getBytes()
          Retrieve the output bytes of this message digest.
protected  int hashWords()
          Hash four 32-bit input words (128 bits), updating this message digest.
static void main(java.lang.String[] args)
          Test driver.
 void reset()
          Reset this message digest, clearing the results of any previously accumulated hashed input data.
 void update(byte in)
          Hash a single byte, updating this message digest.
 void update(byte[] in)
          Hash an array of bytes, updating this message digest.
 void update(byte[] in, int off, int len)
          Hash an array of bytes, updating this message digest.
 
Methods inherited from class java.security.MessageDigest
digest, getAlgorithm, getDigestLength, getInstance, getInstance, getInstance, getProvider, isEqual, toString
 
Methods inherited from class java.security.MessageDigestSpi
engineDigest
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ZoidbergDHash1

public ZoidbergDHash1()
Constructor.

Since:
1.1, 2004-12-20
Method Detail

main

public static void main(java.lang.String[] args)
Test driver.

Reads bytes from the standard input, hashes them, and then prints the resulting 160-bit (20-byte) hexadecimal hash vector.

Parameters:
args - Command line arguments. If specified, a message digest is computed from the first argument; otherwise the digest is computed from the bytes read from standard input.
Since:
1.1, 2004-12-20

reset

public void reset()
Reset this message digest, clearing the results of any previously accumulated hashed input data.

Since:
1.1, 2004-12-20

clone

public java.lang.Object clone()
Clone this message digest, making a copy of the contents accumulated up to this point.

A cloned copy of a message digest can be used to produce a partial message digest. Simply clone the message digest after the partial message contents have been fed into update(byte[]), and then call digest() on the clone. The original message digest object can then be used to continue hashing the rest of the message.

Since:
1.1, 2004-12-20

update

public void update(byte in)
Hash a single byte, updating this message digest.

Parameters:
in - A single byte of input data to hash.
Since:
1.1, 2004-12-20

update

public void update(byte[] in)
Hash an array of bytes, updating this message digest.

Parameters:
in - An array of data bytes to hash.
Since:
1.1, 2004-12-20

update

public void update(byte[] in,
                   int off,
                   int len)
Hash an array of bytes, updating this message digest.

Parameters:
in - An array of data bytes to hash.
off - The index of the first byte of array in to hash.
len - The number of bytes in array in to hash.
Since:
1.1, 2004-12-20

digest

public byte[] digest()
Compute the final value of this message digest.

Note that this also resets the hash vector of this message digest.

Returns:
A 160-bit (20-byte) array of bytes encoding the binary message digest.
Since:
1.1, 2004-12-20

digest

public byte[] digest(byte[] in)
Hash an array of bytes, updating and computing the final message digest.

Note that this also resets the hash vector of this message digest.

Parameters:
in - An array of data bytes to hash.
Returns:
A 160-bit (20-byte) array of bytes encoding the binary message digest.
Since:
1.1, 2004-12-20

engineReset

protected void engineReset()
Reset this message digest, clearing the results of any previously accumulated hashed input data.

Since:
1.1, 2004-12-20

engineGetDigestLength

protected int engineGetDigestLength()
Retrieve the size, in bytes, of this message digest.

Returns:
The number of bytes computed by this message digest, which is always 20 bytes (160 bits).
Since:
1.1, 2004-12-20

engineUpdate

protected void engineUpdate(byte in)
Hash a single byte, updating this message digest.

Parameters:
in - A single byte of input data to hash.
Since:
1.1, 2004-12-20

engineUpdate

protected void engineUpdate(byte[] in,
                            int off,
                            int len)
Hash an array of bytes, updating this message digest.

Parameters:
in - An array of data bytes to hash.
off - The index of the first byte of array in to hash.
len - The number of bytes in array in to hash.
Since:
1.1, 2004-12-20

engineDigest

protected byte[] engineDigest()
Compute the final value of this message digest.

The input is padded with a trailing count byte, and then enough zero bytes to make the total input length a multiple of 20 bytes (160 bits).

Note that this also resets the hash vector of this message digest.

Returns:
A 160-bit (20-byte) array of bytes encoding the binary message digest.
Since:
1.1, 2004-12-20

getBytes

protected byte[] getBytes()
Retrieve the output bytes of this message digest.

Note that the returned byte array will contain all zeros (0x00) if this message digest has not been updated with any data bytes or has been reset.

Returns:
An array of bytes encoding the 160-bit (20-byte) message digest.
Since:
1.1, 2004-12-20

hashWords

protected int hashWords()
Hash four 32-bit input words (128 bits), updating this message digest.

Algorithm

The hashing algorithm is a "braiding" of five 32-bit binary words, which comprise a total of 160 bits of the hash vector. The "braiding" refers to the way in which the words are permuted (rearranged). Between each permuting operation (Pi-phase), the input vector is combined with the output of the permutation, using exclusive-or (X-phase) and two's-complement addition (A-phase).

The I-phase spreads four 32-bit input words across the current 160-bit hash vector. The four 32-bit input words comprise the first four words of the input vector; the last word of the input vector is the sum of the squares of the four input words minus one (i.e., i4 = in0**2 + in1**2 + in2**2 + in3**2 - 1).

 I-phase:
  +-----+    +-----+    +-----+    +-----+
  | in0 |    | in1 |    | in2 |    | in3 |  Input words (128 bits)
  +-----+    +-----+    +-----+    +-----+
    :          :          :          :
    : :        : :        : :        : :
    :   :      :   :      :   :      :   :
    :  X**2    :  X**2    :  X**2    :  X**2    -1
    :    :     :    :     :    :     :    :      :
    :    :.... : ..ADD... : ..ADD... : ..ADD....ADD
    :          :          :          :           :
  +-----+    +-----+    +-----+    +-----+    +-----+
  | i0  |    | i1  |    | i2  |    | i3  |    | i4  |  I0 (160 bits)
  +-----+    +-----+    +-----+    +-----+    +-----+
 

The A, R, P, X, S, and Q phases are then repeated 5 times, intermixing the contents of the input vector with the previous hash vector. The final result of the five rounds is the output hash vector.

 A-phase:
  +-----+    +-----+    +-----+    +-----+    +-----+
  | i0  |    | i1  |    | i2  |    | i3  |    | i4  |  Ii (160 bits)
  +-----+    +-----+    +-----+    +-----+    +-----+
    :          :          :          :          :
    : +-----+  : +-----+  : +-----+  : +-----+  : +-----+
    : | h0  |  : | h1  |  : | h2  |  : | h3  |  : | h4  |  Hi-1
    : +-----+  : +-----+  : +-----+  : +-----+  : +-----+
    :     :    :     :    :     :    :     :    :     :
     :   :      :   :      :   :      :   :      :   :
      ADD        ADD        ADD        ADD        ADD
       :          :          :          :          :
    +-----+    +-----+    +-----+    +-----+    +-----+
    | a0  |    | a1  |    | a2  |    | a3  |    | a4  |  Ai
    +-----+    +-----+    +-----+    +-----+    +-----+

 R-phase:
  +------+ +------+ +------+ +------+ +------+
  |  a0  | |  a1  | |  a2  | |  a3  | |  a4  |  Ai
  +------+ +------+ +------+ +------+ +------+
      :        :        :        :        :
    ROR(17)  ROR(5)   ROR(23)  ROR(10)  ROR(27)
      :        :        :        :        :
  +------+ +------+ +------+ +------+ +------+
  |  r0  | |  r1  | |  r2  | |  r3  | |  r4  |  Ri
  +------+ +------+ +------+ +------+ +------+

 P-phase:
  +-----+ +-----+ +-----+ +-----+ +-----+
  | r0  | | r1  | | r2  | | r3  | | r4  |  Ri
  +-----+ +-----+ +-----+ +-----+ +-----+
     :       :       :       :       :
         :  :       :       :       :
           : :     :       :       :
          :      ::       :       :
         :       :   :   :       :
        :       :       ::      :
       :       :       :     : :
      :       :       :       :  :
     :       :       :       :       :
  +-----+ +-----+ +-----+ +-----+ +-----+
  | p0  | | p1  | | p2  | | p3  | | p4  |  Pi
  +-----+ +-----+ +-----+ +-----+ +-----+

 X-phase:
      +------+   +------+   +------+   +------+   +------+
      |  p0  |   |  p1  |   |  p2  |   |  p3  |   |  p4  |  Pi
      +------+   +------+   +------+   +------+   +------+
          :          :          :          :          :
  +-----+ :  +-----+ :  +-----+ :  +-----+ :  +-----+ :
  | h0  | :  | h1  | :  | h2  | :  | h3  | :  | h4  | :  Hi-1
  +-----+ :  +-----+ :  +-----+ :  +-----+ :  +-----+ :
    :     :    :     :    :     :    :     :    :     :
    :    NEG   :     :    :     :    :     :    :     :
    :     :    :     :    :     :    :     :    :     :
     :   :      :   :      :   :      :   :      :   :
      XOR        XOR        XOR        XOR        XOR
       :          :          :          :          :
    +------+   +------+   +------+   +------+   +------+
    |  x0  |   |  x1  |   |  x2  |   |  x3  |   |  x4  |  Xi
    +------+   +------+   +------+   +------+   +------+

 S-phase:
  +-----+ +-----+ +-----+ +-----+ +-----+
  | x0  | | x1  | | x2  | | x3  | | x4  |  Xi
  +-----+ +-----+ +-----+ +-----+ +-----+
       :      :      :      :      :
     +-------------------------------+
     |            ROL(16)            |
     +-------------------------------+
       :      :      :      :      :
  +-----+ +-----+ +-----+ +-----+ +-----+
  | s0  | | s1  | | s2  | | s3  | | s4  |  Si
  +-----+ +-----+ +-----+ +-----+ +-----+

 Q-phase:
  +-----+ +-----+ +-----+ +-----+ +-----+
  | s0  | | s1  | | s2  | | s3  | | s4  |  Si
  +-----+ +-----+ +-----+ +-----+ +-----+
     :       :       :       :       :
       :    :          :   :        :
         : :             :         :
          ::           :   :      :
         :   :       :       :   :
        :      :   :           ::
       :         :             : :
      :        :   :          :    :
     :       :       :       :       :
  +-----+ +-----+ +-----+ +-----+ +-----+
  | i0  | | i1  | | i2  | | i3  | | i4  |  Ii+1
  +-----+ +-----+ +-----+ +-----+ +-----+
 

The P and Q phases permute the 5 words of the hash vector in the following braiding pattern (which repeats every 5 iterations):

     0.  A B C D E
     1.  B C D E A   P  iteration 1
     2.  C E B A D   Q
     3.  E B A D C   P  iteration 2
     4.  B D E C A   Q
     5.  D E C A B   P  iteration 3
     6.  E A D B C   Q
     7.  A D B C E   P  iteration 4
     8.  D C A E B   Q
     9.  C A E B D   P  iteration 5
    10.  A B C D E   Q
 

Returns:
The first 32 bits of the resulting updated hash vector.
Since:
1.1, 2004-12-30