Skip to content

Instantly share code, notes, and snippets.

@fabiolimace
Last active October 31, 2021 03:48
Show Gist options
  • Save fabiolimace/1dc41baf6928f5714d4de520e30f1874 to your computer and use it in GitHub Desktop.
Save fabiolimace/1dc41baf6928f5714d4de520e30f1874 to your computer and use it in GitHub Desktop.
A simple hash using CRC32 and Adler32 in Java
package com.example;
import java.util.zip.Adler32;
import java.util.zip.CRC32;
public class MySimpleHash {
/**
* Calculate a 64 bits hash by combining CRC32 with Adler32.
*
* @param bytes a byte array
* @return a hash number
*/
public static long getHash(byte[] bytes) {
CRC32 crc32 = new CRC32();
Adler32 adl32 = new Adler32();
crc32.update(bytes);
adl32.update(bytes);
long crc = crc32.getValue();
long adl = adl32.getValue();
return (crc << 32) | adl;
}
public static void main(String[] args) {
String string = "This is a test string";
long hash = getHash(string.getBytes());
System.out.println(hash);
// output: 7732385261082445741
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment