Java Utililty Methods Hash Calculate

List of utility methods to do Hash Calculate

Description

The list of methods to do Hash Calculate are organized into topic(s).

Method

intcalculateHash(int x, int y)
calculate Hash
return (x * 1000) + y;
intcalculateHash(Long... ids)

Calculate the hash code.

final int prime = 31;
final int offset = 32;
int result = 1;
for (Long id : ids) {
    if (id != null) {
        result = prime * result + (int) (id ^ (id >>> offset));
return result;
StringcalculateHash(String str)
Calculates Hash.
long h = 0;
long g;
int pos = 0;
while (pos < str.length()) {
    h = (h << 4) + str.charAt(pos++);
    if ((g = (h & 0xf0000000)) != 0)
        h ^= g >> 24;
    h &= ~g;
...
intcalculateHashCode(byte a[])
calculate Hash Code
if (a == null)
    return 0;
int result = 1;
for (byte b : a) {
    result = 31 * result + b;
return result;
byte[]getHash(byte[] bytes)
Calculate the MD5 hash of the given bytes.
try {
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    return messageDigest.digest(bytes);
} catch (NoSuchAlgorithmException ex) {
    throw new IllegalStateException("Could not find MD5 MessageDigest instance", ex);
byte[]getHash(byte[] bytes)
get Hash
return getHash(bytes, 0, bytes.length);
byte[]getHash(byte[] data)
get Hash
return MessageDigest.getInstance("MD5").digest(data);
StringgetHash(byte[] data)
get Hash
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] hash = digest.digest(data);
String hexHash = byte2hex(hash);
return hexHash;
byte[]getHash(byte[] data, int offset, int length)
Calculates SHA-1 hash for given data.
byte[] tmp = new byte[20];
try {
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(data, offset, length);
    md.digest(tmp, 0, tmp.length);
} catch (GeneralSecurityException e) {
    throw new RuntimeException("SHA-1 algorithm for MessageDigest not supported");
return tmp;
byte[]getHash(byte[] inputBytes)
get Hash
MessageDigest hash;
try {
    hash = MessageDigest.getInstance(HASH_ALGORITHM);
    hash.reset();
    hash.update(inputBytes);
    return hash.digest();
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
...