I need to generate a fixed length hash of 30 characters based on some input data (like a customer email address) in Java. After some searching, I found out about SHA-3 sponge functions, where I can specify the required length. I implemented the following using Bouncy Castle SHAKEDigest
class.
public class App {
public static void main(String[] args) {
final String message = "Hello World!";
System.out.println(getHash(message, 64));
System.out.println(getHash(message, 30));
System.out.println(getHash(message, 20));
}
static String getHash(final String message, final int lengthInCharacters) {
final byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
final SHAKEDigest digest = new SHAKEDigest(128);
final byte[] hashBytes = new byte[lengthInCharacters / 2];
digest.update(messageBytes, 0, messageBytes.length);
digest.doOutput(hashBytes, 0, hashBytes.length);
return Hex.toHexString(hashBytes);
}
}
If I execute it, I get the following output:
aacfe6ebd3737d9f195c837c5281d3f87646ecd7e43864e1a40456e40f264046
aacfe6ebd3737d9f195c837c5281d3
aacfe6ebd3737d9f195c
I expected that the hashes are totally different depending on the requested length. As it looks now, I could also generate a simple SHA-256 hash using JDK MessageDigest
and just truncate it on the required length.
Am I doing something wrong or am I misunderstanding the point of those sponge functions?
Full code with unit tests is available at: https://github.com/steinsag/java-dynamic-hash