public class Saph extends Object
This class is a configurable implementation of the Saph hashing algorithm. Parts may be
added using the add methods (such as add(java.lang.String) or
add(byte[])), and then finalized using the hash() method.
An example for calculating the test vector 1 would be:
// New instance with memory size = 4 and iterations = 2
Saph saph = new Saph(4, 2);
saph.add("just");
saph.add("a");
saph.add("test");
byte[] hash = saph.hash();
For convenience, add methods support return the current instance, for call chaining,
so the above code may also be written as:
byte[] hash = (new Saph(4, 2)).add("just").add("a").add("test").hash();
Once the hash() method has been called, no further additions are supported.
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_ITERATIONS
Default number of iterations.
|
static int |
DEFAULT_MEMORY_SIZE
Default memory size, in 64-byte blocks.
|
| Constructor and Description |
|---|
Saph()
Creates a new instance with default memory and iteration count.
|
Saph(int memorySize,
int iterations)
Creates a new instance with the specified memory and iterations count.
|
| Modifier and Type | Method and Description |
|---|---|
Saph |
add(byte[] part)
Adds the given part to the Saph hash.
|
Saph |
add(byte[] buffer,
int offset,
int length)
Adds a chunk of the given buffer as a part of the Saph hash.
|
Saph |
add(ByteBuffer part)
Adds the given part to the Saph hash.
|
Saph |
add(String part)
Encodes the given string to UTF-8 and adds it to the Saph hash.
|
byte[] |
getHash()
Returns the calculated hash.
|
int |
getIterations()
Returns the configured number of iterations.
|
int |
getMemorySize()
Returns the configured memory size, in 64-byte blocks.
|
byte[] |
hash()
Calculates and returns the hash for the parts.
|
boolean |
isCalculated()
Returns true if the hash has been already calculated.
|
public static final int DEFAULT_MEMORY_SIZE
public static final int DEFAULT_ITERATIONS
public Saph()
public Saph(int memorySize,
int iterations)
memorySize - memory size, in 64-byte blocksiterations - iteration countIllegalArgumentException - if memory or iterations are less than onepublic Saph add(String part)
part - part to addNullPointerException - if part is nullIllegalStateException - if the hash has been already calculatedpublic Saph add(byte[] part)
part - part to addNullPointerException - if part is nullIllegalStateException - if the hash has been already calculatedpublic Saph add(byte[] buffer, int offset, int length)
buffer - buffer containing the partoffset - offset of data in given bufferlength - chunk lengthNullPointerException - if part is nullIllegalStateException - if the hash has been already calculatedpublic Saph add(ByteBuffer part)
part - the buffer containing the partNullPointerException - if part is nullIllegalStateException - if the hash has been already calculatedpublic byte[] hash()
This call supports being called several times, but would be only calculated on the first call.
public int getMemorySize()
public int getIterations()
public boolean isCalculated()
public byte[] getHash()
IllegalStateException - if the hash has not been yet calculated.Copyright © 2019. All rights reserved.