Last active
October 31, 2021 03:48
-
-
Save fabiolimace/1dc41baf6928f5714d4de520e30f1874 to your computer and use it in GitHub Desktop.
A simple hash using CRC32 and Adler32 in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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