public class HashFunctionUtil
extends java.lang.Object
Constructor and Description |
---|
HashFunctionUtil() |
Modifier and Type | Method and Description |
---|---|
static byte[] |
combineHash(HashFunction hash,
boolean ordered,
byte[]... inputs)
Cascades the hash codes
|
static byte[] |
fromHexString(java.lang.String hexString)
Converts the hex-string back into an array of bytes
|
static boolean |
isPrime(long num)
Determines if a number is prime or not
|
static long |
nextPrime(long num)
Returns the next-greater prime, sort of like "ceil" but for primes.
|
static byte[] |
salt(HashFunction hash,
byte[]... inputs)
Computes the salted hash of the given inputs, that is, we concatenate
the inputs together and compute the hash of the concatenated bytes.
|
static void |
saltInto(byte[] output,
HashFunction hash,
byte[]... inputs)
Computes the salted hash of the given inputs, that is, we concatenate
the inputs together and compute the hash of the concatenated bytes.
|
static byte[] |
toByteArray(int value)
Converts an int into a 4-byte array in Big Endian order
(most-significant byte in slot 0)
|
static void |
toByteArray(int value,
byte[] output)
Converts an int into a 4-byte array in Big Endian order
(most-significant byte in slot 0)
|
static byte[] |
toByteArray(long value)
Converts a long into an 8-byte array in Big Endian order
(most-significant byte in slot 0)
|
static void |
toByteArray(long value,
byte[] output)
Converts a long into an 8-byte array in Big Endian order
(most-significant byte in slot 0)
|
static java.lang.String |
toHexString(byte[] bytes)
Converts a byte array to a lower-case Hex string
|
static java.lang.String |
toHexString(int input)
Converts the given int to a Big Endian hex string
|
static java.lang.String |
toHexString(long input)
Converts the given long to a Big Endian hex string
|
static int |
toInteger(byte[] data)
Converts the 4-byte array to a Big Endian int.
|
static int |
toInteger(byte[] data,
int offset)
Converts the 4 bytes in the given array starting at "offset"
to a Big Endian int.
|
static long |
toLong(byte[] data)
Converts the 8-byte value to a Big Endian long
|
static long |
toLong(byte[] data,
int offset)
Converts the 8-byte value at "offset" to a Big Endian long
|
static byte |
valueOf(char hexChar)
Returns the hex value of the given character
|
public static byte[] toByteArray(int value)
value
- Integer to convertpublic static void toByteArray(int value, byte[] output)
value
- Integer to convertoutput
- 4-byte array to store the intpublic static byte[] toByteArray(long value)
value
- long to convertpublic static void toByteArray(long value, byte[] output)
value
- long to convertoutput
- 8-byte array to store the longpublic static byte[] combineHash(HashFunction hash, boolean ordered, byte[]... inputs)
hash
- HashFunction used to compute the resultsordered
- true for ordered hash, false for unorderedinputs
- Inputs to compute the hash ofpublic static byte[] salt(HashFunction hash, byte[]... inputs)
hash
- HashFunction that computes the hashinputs
- Ordered inputs to the hash functionpublic static void saltInto(byte[] output, HashFunction hash, byte[]... inputs)
output
- Output into which we place the output (salted hash)hash
- HashFunction that computes the hashinputs
- Ordered inputs to the hash functionpublic static java.lang.String toHexString(int input)
input
- Input to convertpublic static java.lang.String toHexString(long input)
input
- Input to convertpublic static java.lang.String toHexString(byte[] bytes)
bytes
- byte array to convertpublic static byte valueOf(char hexChar)
hexChar
- Hex character, such as '0' or 'A'public static byte[] fromHexString(java.lang.String hexString)
hexString
- Hex string, such as "0123456789ABCDEF", where each byte is represented
by two characterspublic static int toInteger(byte[] data)
data
- 4-byte array to convertpublic static int toInteger(byte[] data, int offset)
data
- Array to convertoffset
- Offset into the arraypublic static long toLong(byte[] data)
data
- byte array to convertpublic static long toLong(byte[] data, int offset)
data
- byte array to convertoffset
- Offset into the byte array@PublicationReference(author="Snort IDS", title="sfhashfcn.c", type=Misc, year=2012, url="http://www.rajivchakravorty.com/source-code/.tmp/snort-html/sfhashfcn_8c-source.html", notes={"Not an efficient implementation.","My improvements make the algorithm O(sqrt(n/2)), instead of O(n), but it still sucks"}) public static boolean isPrime(long num)
num
- Number to determine if it's a prime number or not@PublicationReference(author="Snort IDS", title="sfhashfcn.c", type=Misc, year=2012, url="http://www.rajivchakravorty.com/source-code/.tmp/snort-html/sfhashfcn_8c-source.html", notes="Not an efficient implementation.") public static long nextPrime(long num)
num
- Number to return the next-greater prime number of