tribble.security
Class ZoidbergBHash

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

public class ZoidbergBHash
extends java.security.MessageDigest

Zoidberg-B 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
{3F C0 C3 19 2C A6 CF 2F A1 7E 25 66 1D FA 18 1D 4C 0C 96 DB}.

A single input byte of
{00}
hashes to a message digest value of
{CA AA 06 79 73 81 30 AA F0 7C D8 76 AD A2 29 41 AC 0E BF 0C}.

A single input byte of
{01}
hashes to a message digest value of
{C8 AA 04 3A 4A C2 36 F7 3C 84 D9 46 20 42 27 C7 29 49 04 8E}.

Two input bytes of
{0D 0A}
hash to a message digest value of
{A8 8D 85 B1 2B 9E 01 7F BE E1 05 B8 2B 65 DE D0 80 1D 9A 1D}.

Four input bytes of
{01 02 03 04}
hash to a message digest value of
{1C 7B 23 3F E0 FF B9 A3 13 7C 8A 63 0A 31 B2 CB 55 02 59 68}.

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
{DA 6D A0 F6 15 33 C3 F5 02 4B 62 3E F2 4E 90 F7 F1 A8 0B 71}.

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
{0B F0 FF EB 36 A1 39 E6 75 13 40 C8 E1 B4 14 DE 02 05 78 1E}.

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
{65 95 EF 01 B3 8A 01 13 A4 00 2C A4 DF 46 01 0C 8E 82 CE 71}.

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-09-24
Version:
$Revision: 1.1 $ $Date: 2003/09/25 05:13:57 $
Author:
David R. Tribble (david@tribble.com).
Copyright ©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:
ZoidbergAHash, MessageDigest

Constructor Summary
ZoidbergBHash()
          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

ZoidbergBHash

public ZoidbergBHash()
Constructor.

Since:
1.1, 2003-09-24
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, 2003-09-24

reset

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

Since:
1.1, 2003-09-24

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, 2003-09-24

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, 2003-09-24

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, 2003-09-24

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, 2003-09-24

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, 2003-09-24

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, 2003-09-24

engineReset

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

Since:
1.1, 2003-09-24

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, 2003-09-24

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, 2003-09-24

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, 2003-09-24

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, 2003-09-24

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, 2003-09-24

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).

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:
  +-----+    +-----+    +-----+    +-----+
  | 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  |  I  (160 bits)
  +-----+    +-----+    +-----+    +-----+    +-----+
    :          :          :          :          :
    : +-----+  : +-----+  : +-----+  : +-----+  : +-----+
    : | 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
 

Returns:
The first 32 bits of the resulting updated hash vector.
Since:
1.1, 2003-09-24