Essentials hash functions

With greenrobot-essentials hash functions, we provide a set of highly performant non-cryptographic Java hash functions in an easy-to-use way:

The hash functions are available as classes in the de.greenrobot.common.hash package. They implement the java.util.zip.Checksum interface, and are thus straight forward to use.

 

Utility classes

Hash and checksum functions usually only accept bytes as input. Our class PrimitiveDataChecksum transforms shorts, ints, longs, Strings, and arrays on the fly to bytes. It’s a wrapper around Java’s Checksum interface and can thus be used with all of our hash function classes (or Java’s Adler32/CRC32).

The class CombinedChecksum takes two Checksum objects (preferably 32 bit) and combines their hashes into a 64 bit hash. CombinedChecksum implements Checksum itself. This class can be useful to work around flaws in hashing functions, or to make collision attacks harder.

 

Getting hashes on the fly with streaming

Because all hash classes implement Checksum, you can use Java’s CheckedInputStream and CheckedOutputStream. They calculate the checksum (hash) of read/written data on the fly.

 

FNV-1a hash functions

FNV is a popular hash function and one of the easiest to implement. Our implementation produces the same hashes as the C reference (unit tested). Because of the byte-per-byte algorithm, it’s not the fastest on today’s processors.

  • Classes: FNV32 and FNV64
  • Speed (i7-3720QM) FNV32: 737 MB/s
  • Speed (i7-3720QM) FNV64: 785 MB/s
  • SMHasher quality: mediocre (a couple of flaws)

 

FNVJ hash functions

These are some experimental custom hash functions, sacrificing quality to perform very fast. They might be an alternative if your use case does not have strict requirements. When in doubt, stick to Murmur3A or Murmur3F.

  • Classes: FNVJ32 and FNVJ64
  • Speed (i7-3720QM) FNVJ32: 3128 MB/s
  • Speed (i7-3720QM) FNVJ64: 5879 MB/s
  • SMHasher quality: bad (lots of flaws, only FNVJ32 ported and tested)

 

Murmur3 hash functions

Murmur3 hash functions are fast and produce high quality hashes. You cannot make a wrong choice here.

  • Classes: Murmur3A and Murmur3F
  • Speed (i7-3720QM) Murmur3A: 2130 MB/s
  • Speed (i7-3720QM) Murmur3F: 3752 MB/s
  • SMHasher quality: very good (no obvious flaws)

 

Spread the love