|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectau.com.forward.sipHash.SipHash_2_4
public class SipHash_2_4
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 |
---|
public SipHash_2_4()
Method Detail |
---|
public void initialize(byte[] key)
key
- 16 byte secret key.
java.lang.IllegalArgumentException
- if key is not exactly 16 bytes long.public void updateHash(byte b)
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
b
- the byte to addpublic long finish()
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.
public long hash(byte[] key, byte[] data)
key
- data
-
public static long rotateLeft(long l, int shift)
l
- long to rotateshift
- number of bits to rotate left
public static long bytesLEtoLong(byte[] b, int offset)
b
- bytes in LittleEndian formatoffset
- idx of first byte
public static byte[] longToBytesLE(long m)
m
- the long to convert
public static byte[] longToBytes(long m)
m
- the long to convert
public static final java.lang.String toHex(byte[] b, int offset, int length)
b
- the byte arrayoffset
- the starting indexlength
- the length to convert
public static final java.lang.String toHex(byte[] b)
b
- the byte array
public java.lang.String toString()
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |