au.com.forward.sipHash
Class SipHash_2_4

java.lang.Object
  extended by au.com.forward.sipHash.SipHash_2_4

public class SipHash_2_4
extends java.lang.Object

SipHash_2_4 -- This is a streaming implementation of https://131002.net/siphash/

Usage:

  // the standard test key 
  byte key[] = {(byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x06, (byte)0x07,
                (byte)0x08, (byte)0x09, (byte)0x0a, (byte)0x0b, (byte)0x0c (byte)0x0d, (byte)0x0e, (byte)0x0f};

  // the standard test msg  15 bytes long 
  byte msg[] = {(byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x06, (byte)0x07,
                (byte)0x08, (byte)0x09, (byte)0x0a, (byte)0x0b, (byte)0x0c (byte)0x0d, (byte)0x0e};
  
Block Usage:
   long result = sipHash.hash(key, msg);
   // this matches the pdf result https://131002.net/siphash/siphash.pdf
   System.out.println(SipHash.toHex(SipHash.longToBytes(result)); 
  

Streaming Usage:

  sipHash.init(key); // initialize with key

  // for each byte of the data call updateHash( ) 
   sipHash.updateHash(b); // update hash with each byte of msg

   // after all bytes have been processed, call finish to get result
  long hash = sipHash.finish(); 
  
see https://131002.net/siphash/ for details of algorithm

(c)2013 Forward Computing and Control Pty. Ltd. (www.forward.com.au)
This code may be freely used for both private and commercial use.
Provide this copyright is maintained.


Constructor Summary
SipHash_2_4()
           
 
Method Summary
static long bytesLEtoLong(byte[] b, int offset)
          Converts byte[] in LittleEndian format to a long
 long finish()
          Call this to complete the hash by processing any remaining bytes and adding the msg length.
 long hash(byte[] key, byte[] data)
          Convenience method for hashing a byte array with key
 void initialize(byte[] key)
          Initialize hash with 16 byte key
Call this before starting each hash of a message
static byte[] longToBytes(long m)
          Convert a long to bytes in BigEndian format
static byte[] longToBytesLE(long m)
          Convert a long to bytes in LittleEndian format
static long rotateLeft(long l, int shift)
          Rotate long left by shift bits.
static java.lang.String toHex(byte[] b)
          Convert a byte array to Hex Digits
static java.lang.String toHex(byte[] b, int offset, int length)
          Convert a byte array to Hex Digits
 java.lang.String toString()
          The current state of hash, v0,v1,v2,v3, as hex digits in BigEndian format
 void updateHash(byte b)
          Add a byte to the hash and increment the message length count % 256
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SipHash_2_4

public SipHash_2_4()
Method Detail

initialize

public void initialize(byte[] key)
Initialize hash with 16 byte key
Call this before starting each hash of a message

Parameters:
key - 16 byte secret key.
Throws:
java.lang.IllegalArgumentException - if key is not exactly 16 bytes long.

updateHash

public void updateHash(byte b)
Add a byte to the hash and increment the message length count % 256

The bytes are accumulated until there are 8 of them and then the corresponding long (read from the bytes as LittleEndian format) is added to the hash

Parameters:
b - the byte to add

finish

public long finish()
Call this to complete the hash by processing any remaining bytes and adding the msg length.

This method returns the hash as long. Use one of the utility methods, longToByteLE or longToByte to convert the long to bytes in LittleEndian or BigEndian format respectively and then use toHex to convert the byte[] to a string of hex digits for display or transmission.

Returns:
the hash as long.

hash

public long hash(byte[] key,
                 byte[] data)
Convenience method for hashing a byte array with key

Parameters:
key -
data -
Returns:

rotateLeft

public static long rotateLeft(long l,
                              int shift)
Rotate long left by shift bits. bits rotated off to the left are put back on the right

Parameters:
l - long to rotate
shift - number of bits to rotate left
Returns:
the rotated long

bytesLEtoLong

public static long bytesLEtoLong(byte[] b,
                                 int offset)
Converts byte[] in LittleEndian format to a long

Parameters:
b - bytes in LittleEndian format
offset - idx of first byte
Returns:
the resulting long

longToBytesLE

public static byte[] longToBytesLE(long m)
Convert a long to bytes in LittleEndian format

Parameters:
m - the long to convert
Returns:
byte[8] containing the LittleEndian format of the long

longToBytes

public static byte[] longToBytes(long m)
Convert a long to bytes in BigEndian format

Parameters:
m - the long to convert
Returns:
byte[8] containing the BigEndian format of the long

toHex

public static final java.lang.String toHex(byte[] b,
                                           int offset,
                                           int length)
Convert a byte array to Hex Digits

Parameters:
b - the byte array
offset - the starting index
length - the length to convert
Returns:
string contain the hex digits, 2 for each byte.

toHex

public static final java.lang.String toHex(byte[] b)
Convert a byte array to Hex Digits

Parameters:
b - the byte array
Returns:
string contain the hex digits, 2 for each byte.

toString

public java.lang.String toString()
The current state of hash, v0,v1,v2,v3, as hex digits in BigEndian format

Overrides:
toString in class java.lang.Object
Returns:
current state as a string (in BigEndian format)