- Notifications
You must be signed in to change notification settings - Fork5
SipHash in Java; zero-allocation and streaming implementations
License
whitfin/siphash-java
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A Java implementation of the SipHash cryptographic hash family. Supports any variation, although defaults to the widely used SipHash-2-4. Can be used with either full input, or used as a streaming digest.
This library was heavily influenced byveorq's C implementation andForward C&C's reference implementation - I just decided it was time a Java implementation of SipHash made it onto Maven :).
siphash is (soon to be) available on Maven central, via Sonatype OSS:
<dependency> <groupId>com.zackehh</groupId> <artifactId>siphash</artifactId> <version>1.0.0</version></dependency>There are two ways of using SipHash (see below). Both return aSipHashResult which can be used to retrieve the result in various forms. For further usage, please visit thedocumentation.
The first is to simple create aSipHash instance and use it to repeatedly hash using the same key.
The internal state is immutable, so you can hash many inputs without having to recreate a newSipHash instance (unless you want a new key).
SipHashhasher =newSipHash("0123456789ABCDEF".getBytes());SipHashResultresult =hasher.hash("my-input".getBytes());System.out.println(result.get());// 182795880124085484 <-- this can overflowSystem.out.println(result.getHex());// "2896be26d3374ec"System.out.println(result.getHex(true));// "02896be26d3374ec"System.out.println(result.getHex(SipHashCase.UPPER));// "2896BE26D3374EC"System.out.println(result.getHex(true,SipHashCase.UPPER));// "02896BE26D3374EC"
The second is to use the library as a streaming hash, meaning you can apply chunks of bytes to the hash as they become available.
Using this method you must create a new digest every time you want to hash a different input as the internal state is mutable.
SipHashDigestdigest =newSipHashDigest("0123456789ABCDEF".getBytes());digest.update("chu".getBytes());digest.update("nked".getBytes());digest.update(" string".getBytes());SipHashResultresult =digest.finish();System.out.println(result.get());// 3502906798476177428 <-- this can overflowSystem.out.println(result.getHex());// "309cd32c8c793014"System.out.println(result.getHex(true));// "309cd32c8c793014"System.out.println(result.getHex(SipHashCase.UPPER));// "309CD32C8C793014"System.out.println(result.getHex(true,SipHashCase.UPPER));// "309CD32C8C793014"
If you wish to contribute (awesome!), please file an issue first! All PRs should passmvn clean verify and maintain 100% test coverage.
Tests are run usingmvn. I aim to maintain 100% coverage where possible (both line and branch).
Tests can be run as follows:
$ mvn clean verify
About
SipHash in Java; zero-allocation and streaming implementations
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.