tribble.security
Class ZoidbergAHash

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

public class ZoidbergAHash
extends java.security.MessageDigest

Zoidberg-A 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 4 bytes (32 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 20 bytes (160 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
{29 49 BB F4 CB DD 7D FA D5 C5 D0 EA 98 6E 16 B5 94 36 47 92}.

A single input byte of
{00}
hashes to a message digest value of
{45 CF F6 22 22 CB 87 3E 1E 25 26 81 09 7C 81 1D D3 08 89 C6}.

A single input byte of
{01}
hashes to a message digest value of
{E4 54 03 82 2B 89 2C 9D E5 1A FC DA EA 90 7F B4 88 0A 93 73}.

Two input bytes of
{0D 0A}
hash to a message digest value of
{2A 8D EA B4 BE 93 EA C4 B0 03 6A C1 F7 3D A4 71 72 3C 9F 39}.

Four input bytes of
{01 02 03 04}
hash to a message digest value of
{8D 25 C9 DE 73 26 03 9C 2D 0F DF 26 EF F9 F8 65 18 42 E9 9F}.

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
{A5 B4 96 43 34 1C 9A 1D 8F 1E 5D 85 F9 8A 01 56 0D A7 7E 11}.

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
{1C CA A1 32 FC 70 A6 1A 10 9F 4D 38 15 E1 F9 E2 C8 06 A0 51}.

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
{0B 81 67 DC D9 EE 42 FE 16 BD FD 0E 94 1E F7 92 DC 49 8F 42}.

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:
2002-03-29
Version:
$Revision: 1.11 $ $Date: 2003/09/24 03:35:09 $
Author:
David R. Tribble (david@tribble.com).
Copyright ©2002-2003 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:
MessageDigest

Constructor Summary
ZoidbergAHash()
          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 hashWord(int x)
          Hash a 32-bit input word, 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

ZoidbergAHash

public ZoidbergAHash()
Constructor.

Since:
1.1, 2002-03-29
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, 2002-03-29

reset

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

Since:
1.2, 2002-04-03

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.3, 2002-04-06

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.3, 2002-04-06

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.2, 2002-04-03

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, 2002-03-29

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.3, 2002-04-06

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.3, 2002-04-06

engineReset

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

Since:
1.3, 2002-04-06

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.3, 2002-04-06

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.3, 2002-04-06

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.3, 2002-04-06

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.3, 2002-04-06

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, 2002-03-29

hashWord

protected int hashWord(int x)
Hash a 32-bit input word, 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 a 32-bit input word across the current hash vector. The R, P1, X, P2, and A phases are then repeated 5 times, intermixing the contents of the input vector with the hash vector. The final result of the intermixing is the output hash vector.

 I-phase:
  +-----+
  |  X  |  Input word
  +-----+
     :
     :---------.
     :          :
     :      (X+1)**2
     :          :
     :          :---------.
     :          :          :
     :          :      (X+1)**2
     :          :          :
     :          :          :---------.
     :          :          :          :
     :          :          :      (X+1)**2
     :          :          :          :
     :          :          :          :---------.
     :          :          :          :          :
     :          :          :          :      (X+1)**2
     :          :          :          :          :
  +-----+    +-----+    +-----+    +-----+    +-----+
  | i0  |    | i1  |    | i2  |    | i3  |    | i4  |  I
  +-----+    +-----+    +-----+    +-----+    +-----+
    :          :          :          :          :
    : +-----+  : +-----+  : +-----+  : +-----+  : +-----+
    : | o0  |  : | o1  |  : | o2  |  : | o3  |  : | o4  |  H
    : +-----+  : +-----+  : +-----+  : +-----+  : +-----+
    :     :    :     :    :     :    :     :    :     :
     :   :      :   :      :   :      :   :      :   :
      ADD        ADD        ADD        ADD        ADD
       :          :          :          :          :
    +-----+    +-----+    +-----+    +-----+    +-----+
    | i0  |    | i1  |    | i2  |    | i3  |    | i4  |  Ii
    +-----+    +-----+    +-----+    +-----+    +-----+

 R-phase:
  +-----+ +-----+ +-----+ +-----+ +-----+
  | i0  | | i1  | | i2  | | i3  | | i4  | Ii
  +-----+ +-----+ +-----+ +-----+ +-----+
     :       :       :       :       :
  ROR(17) ROR(5)  ROR(25) ROR(13) ROR(29)
     :       :       :       :       :
  +-----+ +-----+ +-----+ +-----+ +-----+
  |  A  | |  B  | |  C  | |  D  | |  E  |  Ri
  +-----+ +-----+ +-----+ +-----+ +-----+

 P1-phase:
  +-----+ +-----+ +-----+ +-----+ +-----+
  |  A  | |  B  | |  C  | |  D  | |  E  |  Ri
  +-----+ +-----+ +-----+ +-----+ +-----+
     :       :       :       :       :
         :  :       :       :       :
           : :     :       :       :
          :      ::       :       :
         :       :   :   :       :
        :       :       ::      :
       :       :       :     : :
      :       :       :       :  :
     :       :       :       :       :
  +-----+ +-----+ +-----+ +-----+ +-----+
  |  B  | |  C  | |  D  | |  E  | |  A  |  P1i
  +-----+ +-----+ +-----+ +-----+ +-----+

 X-phase:
      +-----+    +-----+    +-----+    +-----+    +-----+
      |  A  |    |  B  |    |  C  |    |  D  |    |  E  |  P1i
      +-----+    +-----+    +-----+    +-----+    +-----+
          :          :          :          :          :
  +-----+ :  +-----+ :  +-----+ :  +-----+ :  +-----+ :
  | i0  | :  | i1  | :  | i2  | :  | i3  | :  | i4  | :  Ii
  +-----+ :  +-----+ :  +-----+ :  +-----+ :  +-----+ :
    :     :    :     :    :     :    :     :    :     :
     :   :      :   :      :   :      :   :      :   :
      XOR        XOR        XOR        XOR        XOR
       :          :          :          :          :
    +-----+    +-----+    +-----+    +-----+    +-----+
    |  A  |    |  B  |    |  C  |    |  D  |    |  E  |  Xi
    +-----+    +-----+    +-----+    +-----+    +-----+

 P2-phase:
  +-----+ +-----+ +-----+ +-----+ +-----+
  |  A  | |  B  | |  C  | |  D  | |  E  |  Xi
  +-----+ +-----+ +-----+ +-----+ +-----+
     :       :       :       :       :
       :    :          :   :        :
         : :             :         :
          ::           :   :      :
         :   :       :       :   :
        :      :   :           ::
       :         :             : :
      :        :   :          :    :
     :       :       :       :       :
  +-----+ +-----+ +-----+ +-----+ +-----+
  |  B  | |  D  | |  A  | |  E  | |  C  |  P2i
  +-----+ +-----+ +-----+ +-----+ +-----+

 A-phase:
      +-----+    +-----+    +-----+    +-----+    +-----+
      |  A  |    |  B  |    |  C  |    |  D  |    |  E  |  P2i
      +-----+    +-----+    +-----+    +-----+    +-----+
          :          :          :          :          :
  +-----+ :  +-----+ :  +-----+ :  +-----+ :  +-----+ :
  | i0  | :  | i1  | :  | i2  | :  | i3  | :  | i4  | :  Ii
  +-----+ :  +-----+ :  +-----+ :  +-----+ :  +-----+ :
    :     :    :     :    :     :    :     :    :     :
     :   :      :   :      :   :      :   :      :   :
      ADD        ADD        ADD        ADD        ADD
       :          :          :          :          :
    +-----+    +-----+    +-----+    +-----+    +-----+
    |  A  |    |  B  |    |  C  |    |  D  |    |  E  |  Ai
    +-----+    +-----+    +-----+    +-----+    +-----+
       :          :          :          :          :
    +-----+    +-----+    +-----+    +-----+    +-----+
    | o0  |    | o1  |    | o2  |    | o3  |    | o4  |  Oi
    +-----+    +-----+    +-----+    +-----+    +-----+
 

The P1 and P2 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   P1   iteration 1
     2.  C E B A D   P2
     3.  E B A D C   P1   iteration 2
     4.  B D E C A   P2
     5.  D E C A B   P1   iteration 3
     6.  E A D B C   P2
     7.  A D B C E   P1   iteration 4
     8.  D C A E B   P2
     9.  C A E B D   P1   iteration 5
    10.  A B C D E   P2
 

Parameters:
x - A 32-bit binary input word with which to update this message digest.
Returns:
The first 32 bits of the resulting updated hash vector.
Since:
1.1, 2002-04-03