Skip to content

Commit 43376e3

Browse files
committed
Migrate to new naming scheme
1 parent 0d2be3f commit 43376e3

File tree

7 files changed

+66
-66
lines changed

7 files changed

+66
-66
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SipHash
2-
[![Build Status](https://img.shields.io/github/actions/workflow/status/whitfin/siphash-java/ci.yml?branch=main)](https://github.com/whitfin/siphash-java/actions) [![Coverage Status](https://img.shields.io/coveralls/whitfin/siphash-java.svg)](https://coveralls.io/github/whitfin/siphash-java) [![Maven Version](https://img.shields.io//maven-central/v/io.whitfin/siphash-java.svg)](https://central.sonatype.com/artifact/io.whitfin/siphash)
2+
[![Build Status](https://img.shields.io/github/actions/workflow/status/whitfin/siphash-java/ci.yml?branch=main)](https://github.com/whitfin/siphash-java/actions) [![Coverage Status](https://img.shields.io/coveralls/whitfin/siphash-java.svg)](https://coveralls.io/github/whitfin/siphash-java) [![Maven Version](https://img.shields.io//maven-central/v/io.whitfin/siphash-java.svg)](https://central.sonatype.com/artifact/io.whitfin/siphash) [![Documentation](https://img.shields.io/badge/docs-latest-blue.svg)](https://www.javadoc.io/doc/io.whitfin/siphash)
33

44
A Java implementation of the SipHash cryptographic hash family. Supports any variation, although defaults to the widely used SipHash-2-4. This library offers both a zero-allocation implementation, along with a streaming digest.
55

@@ -9,11 +9,11 @@ This library was heavily influenced by [veorq's C implementation](https://github
99

1010
`siphash` is available on Maven central, via Sonatype OSS:
1111

12-
```
12+
```xml
1313
<dependency>
1414
<groupId>io.whitfin</groupId>
1515
<artifactId>siphash</artifactId>
16-
<version>2.1.0</version>
16+
<version>3.0.0</version>
1717
</dependency>
1818
```
1919

@@ -26,32 +26,36 @@ There are three main ways to use this library, and the appropriate choice will d
2626
The fastest use of this algorithm is to simply call `SipHasher.hash/2` which will call a zero-allocation implementation of the SipHash algorithm. This implementation should be used in most cases; specifically cases where you have frequently differing seed keys.
2727

2828
```java
29+
import io.whitfin.siphash.SipHash;
30+
2931
// specify the key and data pair
3032
String key = "0123456789ABCDEF".getBytes();
3133
String data = "my-input".getBytes();
3234

3335
// hash using default compression (2-4)
34-
long hash1 = SipHasher.hash(key, data);
36+
long hash1 = SipHash.hash(key, data);
3537

3638
// you can also specify compression rounds
37-
long hash2 = SipHasher.hash(key, data, 2, 4);
39+
long hash2 = SipHash.hash(key, data, 2, 4);
3840
```
3941

4042
### Contained Hashing
4143

4244
This is an optimized implementation for cases where you have a single key (such as a hash table). In these cases, the seed values can be precomputed and re-used, rather than calculating them repeatedly on each call to hash. Although the initial call to create a container uses an allocation, there are no other allocations inside the container.
4345

44-
4546
```java
47+
import io.whitfin.siphash.SipHash;
48+
import io.whitfin.siphash.SipHashContext;
49+
4650
// create a container from our key
4751
String key = "0123456789ABCDEF".getBytes();
48-
SipHasherContainer container = SipHasher.container(key);
52+
SipHashContext ctx = SipHasher.context(key);
4953

5054
// hash using default compression (2-4)
51-
long hash1 = container.hash(data);
55+
long hash1 = ctx.hash(data);
5256

5357
// you can also specify compression rounds
54-
long hash2 = container.hash(data, 2, 4);
58+
long hash2 = ctx.hash(data, 2, 4);
5559
```
5660

5761
### Streaming Digest
@@ -61,7 +65,7 @@ The final way to use the library is as a streaming digest; meaning that you can
6165
```java
6266
// create a container from our key
6367
String key = "0123456789ABCDEF".getBytes();
64-
SipHasherStream hash = SipHasher.init(key);
68+
SipHashStream hash = SipHasher.init(key);
6569

6670
// update several times
6771
hash.update("chu".getBytes());
@@ -78,20 +82,16 @@ By default, as of v2.0.0, all hashes are returned as a `long`. However, you can
7882

7983
```java
8084
// output will be padded (if necessary) to 16 bytes
81-
SipHasher.toHexString(-3891084581787974112L); // ca0017304f874620
82-
SipHasher.toHexString( 77813817455948350L); // 011473413414323e
85+
SipHash.toHexString(-3891084581787974112L); // ca0017304f874620
86+
SipHash.toHexString( 77813817455948350L); // 011473413414323e
8387
```
8488

8589
## Contributing
8690

87-
If you wish to contribute (awesome!), please file an issue first! All PRs should pass `mvn clean verify` and maintain 100% test coverage.
88-
89-
## Testing
90-
91-
Tests are run using `mvn`. I aim to maintain 100% coverage where possible (both line and branch).
92-
93-
Tests can be run as follows:
91+
Tests are run using `mvn`. I aim to maintain 100% coverage where possible (both line and branch):
9492

9593
```bash
9694
$ mvn clean verify
9795
```
96+
97+
If you wish to contribute (awesome!), please file an issue first! All PRs should pass `mvn clean verify` and maintain 100% test coverage.

src/main/java/io/whitfin/siphash/SipHasher.java renamed to src/main/java/io/whitfin/siphash/SipHash.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@
66
* This class offers three main utilities;
77
*
88
* - A zero-allocation SipHash algorithm.
9-
* - A container implementation for single-key environments.
9+
* - A context implementation for single-key environments.
1010
* - A streaming SipHash algorithm for unknown input length.
1111
*
1212
* In most cases, the zero-allocation (0A) implementation will be desired. This
1313
* can be called via {@link #hash(byte[], byte[])} on the most basic level.
1414
*
1515
* In the case you're using a single key (such as one seeded at application
16-
* startup), you can make good use of a container which will simply avoid the
16+
* startup), you can make good use of a context which will simply avoid the
1717
* need to recalculate the initial states on each hash call. This is an extremely
1818
* small optimization, but avoids all possible overhead for the best throughput.
19-
* Containers can be created via the {@link #container(byte[])} method, and can
20-
* hash input via {@link SipHasherContainer#hash(byte[])}.
19+
* Contexts can be created via the {@link #context(byte[])} method, and can
20+
* hash input via {@link SipHashContext#hash(byte[])}.
2121
*
2222
* For the case where the input length is unknown, a streaming implementation is
23-
* available via {@link SipHasherStream}. This can be initialized on a per-hash
23+
* available via {@link SipHashStream}. This can be initialized on a per-hash
2424
* basis via {@link #init(byte[])} and can be updated with bytes multiple times
25-
* via {@link SipHasherStream#update(byte[])}. Once all input has been updated,
26-
* a final call to {@link SipHasherStream#digest()} will return the digested data.
25+
* via {@link SipHashStream#update(byte[])}. Once all input has been updated,
26+
* a final call to {@link SipHashStream#digest()} will return the digested data.
2727
*/
28-
public final class SipHasher {
28+
public final class SipHash {
2929

3030
/**
3131
* Default value for the C rounds of compression.
@@ -58,15 +58,15 @@ public final class SipHasher {
5858
static final long INITIAL_V3 = 0x7465646279746573L;
5959

6060
/**
61-
* Creates a new container, seeded with the provided key.
61+
* Creates a new context, seeded with the provided key.
6262
*
6363
* @param key
64-
* the key bytes used to seed the container.
64+
* the key bytes used to seed the context.
6565
* @return
66-
* a {@link SipHasherContainer} instance after initialization.
66+
* a {@link SipHashContext} instance after initialization.
6767
*/
68-
public static SipHasherContainer container(byte[] key) {
69-
return new SipHasherContainer(key);
68+
public static SipHashContext context(byte[] key) {
69+
return new SipHashContext(key);
7070
}
7171

7272
/**
@@ -126,9 +126,9 @@ public static long hash(byte[] key, byte[] data, int c, int d) {
126126
* @param key
127127
* the key to seed the hash with.
128128
* @return
129-
* a {@link SipHasherStream} instance to update and digest.
129+
* a {@link SipHashStream} instance to update and digest.
130130
*/
131-
public static SipHasherStream init(byte[] key) {
131+
public static SipHashStream init(byte[] key) {
132132
return init(key, DEFAULT_C, DEFAULT_D);
133133
}
134134

@@ -145,10 +145,10 @@ public static SipHasherStream init(byte[] key) {
145145
* @param d
146146
* the number of D rounds of compression.
147147
* @return
148-
* a {@link SipHasherStream} instance to update and digest.
148+
* a {@link SipHashStream} instance to update and digest.
149149
*/
150-
public static SipHasherStream init(byte[] key, int c, int d) {
151-
return new SipHasherStream(key, c, d);
150+
public static SipHashStream init(byte[] key, int c, int d) {
151+
return new SipHashStream(key, c, d);
152152
}
153153

154154
/**

src/main/java/io/whitfin/siphash/SipHasherContainer.java renamed to src/main/java/io/whitfin/siphash/SipHashContext.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.whitfin.siphash;
22

3-
import static io.whitfin.siphash.SipHasher.*;
3+
import static io.whitfin.siphash.SipHash.*;
44

55
/**
66
* Small container of state to aid SipHash throughput.
@@ -9,7 +9,7 @@
99
* hashes. As such, this avoids a small amount of overhead on each hash which
1010
* might prove useful in the case you have constant keys (hash tables, etc).
1111
*/
12-
public final class SipHasherContainer {
12+
public final class SipHashContext {
1313

1414
/**
1515
* The seeded value for the magic v0 number.
@@ -32,12 +32,12 @@ public final class SipHasherContainer {
3232
private final long v3;
3333

3434
/**
35-
* Initializes a container from a key seed.
35+
* Initializes a context from a key seed.
3636
*
3737
* @param key
38-
* the key to use to seed this hash container.
38+
* the key to use to seed this hash context.
3939
*/
40-
SipHasherContainer(byte[] key) {
40+
SipHashContext(byte[] key) {
4141
if (key.length != 16) {
4242
throw new IllegalArgumentException("Key must be exactly 16 bytes!");
4343
}
@@ -76,7 +76,7 @@ public long hash(byte[] data) {
7676
* a long value as the output of the hash.
7777
*/
7878
public long hash(byte[] data, int c, int d) {
79-
return SipHasher.hash(
79+
return SipHash.hash(
8080
c, d,
8181
this.v0,
8282
this.v1,

src/main/java/io/whitfin/siphash/SipHasherStream.java renamed to src/main/java/io/whitfin/siphash/SipHashStream.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.whitfin.siphash;
22

3-
import static io.whitfin.siphash.SipHasher.*;
3+
import static io.whitfin.siphash.SipHash.*;
44

55
/**
66
* Streaming implementation of the SipHash algorithm.
@@ -14,7 +14,7 @@
1414
* no further allocations - so memory should prove similar to the non-streaming
1515
* implementation.
1616
*/
17-
public final class SipHasherStream {
17+
public final class SipHashStream {
1818

1919
/**
2020
* The specified rounds of C compression.
@@ -71,7 +71,7 @@ public final class SipHasherStream {
7171
* @param d
7272
* the desired rounds of D compression.
7373
*/
74-
SipHasherStream(byte[] key, int c, int d) {
74+
SipHashStream(byte[] key, int c, int d) {
7575
if (key.length != 16) {
7676
throw new IllegalArgumentException("Key must be exactly 16 bytes!");
7777
}
@@ -101,9 +101,9 @@ public final class SipHasherStream {
101101
* @param b
102102
* the byte being added to the digest.
103103
* @return
104-
* the same {@link SipHasherStream} for chaining.
104+
* the same {@link SipHashStream} for chaining.
105105
*/
106-
public SipHasherStream update(byte b) {
106+
public SipHashStream update(byte b) {
107107
this.len++;
108108
this.m |= (((long) b & 0xff) << (this.m_idx++ * 8));
109109
if (this.m_idx < 8) {
@@ -125,9 +125,9 @@ public SipHasherStream update(byte b) {
125125
* @param bytes
126126
* the bytes being added to the digest.
127127
* @return
128-
* the same {@link SipHasherStream} for chaining.
128+
* the same {@link SipHashStream} for chaining.
129129
*/
130-
public SipHasherStream update(byte[] bytes) {
130+
public SipHashStream update(byte[] bytes) {
131131
for (byte b : bytes) {
132132
update(b);
133133
}

src/test/java/io/whitfin/siphash/SipHasherContainerTest.java renamed to src/test/java/io/whitfin/siphash/SipHashContextTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
import org.testng.annotations.Test;
44

55
/**
6-
* Test cases for the {@link SipHasherContainer} class.
6+
* Test cases for the {@link SipHashContext} class.
77
*/
8-
public class SipHasherContainerTest extends SipHasherTest {
8+
public class SipHashContextTest extends SipHashTest {
99

1010
/**
1111
* Tests invalid key exceptions are thrown.
1212
*/
1313
@Test(expectedExceptions = IllegalArgumentException.class)
1414
public void testExceptionOnInvalidKey() {
15-
SipHasher.container(new byte[0]).hash(new byte[0]);
15+
SipHash.context(new byte[0]).hash(new byte[0]);
1616
}
1717

1818
/**
19-
* Tests all vectors using the container hash implementation.
19+
* Tests all vectors using the context hash implementation.
2020
*/
2121
@Test
2222
public void testVectorsForContainerHash() {
2323
testVectors(new Hasher() {
2424
@Override
2525
public long hash(byte[] key, byte[] data) {
26-
return SipHasher.container(key).hash(data);
26+
return SipHash.context(key).hash(data);
2727
}
2828
});
2929
}

src/test/java/io/whitfin/siphash/SipHasherStreamTest.java renamed to src/test/java/io/whitfin/siphash/SipHashStreamTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
import org.testng.annotations.Test;
44

55
/**
6-
* Test cases for the {@link SipHasherStream} class.
6+
* Test cases for the {@link SipHashStream} class.
77
*/
8-
public class SipHasherStreamTest extends SipHasherTest {
8+
public class SipHashStreamTest extends SipHashTest {
99

1010
/**
1111
* Tests invalid key exceptions are thrown.
1212
*/
1313
@Test(expectedExceptions = IllegalArgumentException.class)
1414
public void testExceptionOnInvalidKey() {
15-
SipHasher.init(new byte[0]);
15+
SipHash.init(new byte[0]);
1616
}
1717

1818
/**
@@ -23,7 +23,7 @@ public void testVectorsForStreamHash() {
2323
testVectors(new Hasher() {
2424
@Override
2525
public long hash(byte[] key, byte[] data) {
26-
return SipHasher.init(key).update(data).digest();
26+
return SipHash.init(key).update(data).digest();
2727
}
2828
});
2929
}

src/test/java/io/whitfin/siphash/SipHasherTest.java renamed to src/test/java/io/whitfin/siphash/SipHashTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import org.testng.annotations.Test;
55

66
/**
7-
* Test cases for the {@link SipHasher} class.
7+
* Test cases for the {@link SipHash} class.
88
*
99
* This also contains reference methods which can be used by other
1010
* implementations in order to make sure of the same vector bootstrap.
1111
*/
12-
public class SipHasherTest {
12+
public class SipHashTest {
1313

1414
// test vectors via https://www.131002.net/siphash/siphash24.c
1515
private static final long[] EXPECTED = new long[] {
@@ -36,7 +36,7 @@ public class SipHasherTest {
3636
*/
3737
@Test
3838
public void testConstructorUsage() {
39-
new SipHasher();
39+
new SipHash();
4040
}
4141

4242
/**
@@ -47,7 +47,7 @@ public void testVectorsForZeroAllocHash() {
4747
testVectors(new Hasher() {
4848
@Override
4949
public long hash(byte[] key, byte[] data) {
50-
return SipHasher.hash(key, data);
50+
return SipHash.hash(key, data);
5151
}
5252
});
5353
}
@@ -63,8 +63,8 @@ public void testConversionToHexString() {
6363
long hash1 = -3891084581787974112L;
6464
long hash2 = 77813817455948350L;
6565

66-
String hex1 = SipHasher.toHexString(hash1);
67-
String hex2 = SipHasher.toHexString(hash2);
66+
String hex1 = SipHash.toHexString(hash1);
67+
String hex2 = SipHash.toHexString(hash2);
6868

6969
Assert.assertEquals(hex1, "ca0017304f874620");
7070
Assert.assertEquals(hex2, "011473413414323e");
@@ -75,7 +75,7 @@ public void testConversionToHexString() {
7575
*/
7676
@Test(expectedExceptions = IllegalArgumentException.class)
7777
public void testExceptionOnInvalidKey() {
78-
SipHasher.hash(new byte[0], new byte[0]);
78+
SipHash.hash(new byte[0], new byte[0]);
7979
}
8080

8181
/**

0 commit comments

Comments
 (0)