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

StringgetHash(byte[]... bytesToHash)
get Hash
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.reset();
for (byte[] bytes : bytesToHash) {
    digest.update(bytes);
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16);
byte[]getHash(File file)
Return the MD5 hash for the given file.
synchronized (md) {
    byte[] dataBytes = new byte[1024];
    if (file != null && file.exists() && file.canRead() && file.isFile()) {
        try (FileInputStream fis = new FileInputStream(file);) {
            md.reset();
            int nread = 0;
            while ((nread = fis.read(dataBytes)) != -1) {
                md.update(dataBytes, 0, nread);
...
StringgetHash(File file, String hashType)
get Hash
InputStream fis = new FileInputStream(file);
byte buffer[] = new byte[1024];
MessageDigest md5 = MessageDigest.getInstance(hashType);
for (int numRead = 0; (numRead = fis.read(buffer)) > 0;) {
    md5.update(buffer, 0, numRead);
fis.close();
return toHexString(md5.digest());
...
StringgetHash(final byte[] data, final String hashAlgorithm)
get Hash
if (data == null)
    return null;
try {
    final MessageDigest md = MessageDigest.getInstance(hashAlgorithm);
    return toHexString(md.digest(data));
} catch (final NoSuchAlgorithmException e) {
    return null;
StringgetHash(final String text, final Charset charset, final MessageDigest algorithm)
Computes the hash of a given text.
final StringBuilder buffer = new StringBuilder();
algorithm.reset();
algorithm.update(text.getBytes(charset));
final byte[] digest = algorithm.digest();
for (final byte element : digest) {
    int value = element;
    if (value < 0) {
        value += Byte.MAX_VALUE - Byte.MIN_VALUE + 1;
...
StringgetHash(InputStream in, String algorithm)
get Hash
MessageDigest md = MessageDigest.getInstance(algorithm);
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = in.read(dataBytes)) != -1) {
    md.update(dataBytes, 0, nread);
byte[] mdbytes = md.digest();
StringBuilder sb = new StringBuilder("");
...
StringgetHash(InputStream is, String algorithm)
Reads the input stream and returns an hash for it, based on the algorithm passed as parameter.
MessageDigest md = MessageDigest.getInstance(algorithm);
byte buffer[] = new byte[1024];
try {
    for (int read = is.read(buffer); read != -1; read = is.read(buffer))
        if (read > 0)
            md.update(buffer, 0, read);
} finally {
    is.close();
...
byte[]getHash(int iterationNb, String password, byte[] salt)
From a password, a number of iterations and a salt, returns the corresponding digest
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.reset();
digest.update(salt);
byte[] input = digest.digest(password.getBytes("UTF-8"));
for (int i = 0; i < iterationNb; i++) {
    digest.reset();
    input = digest.digest(input);
return input;
StringgetHash(String algorithm, int i)
get Hash
return getHash(algorithm, String.valueOf(i));
StringgetHash(String credentials)
get Hash
try {
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    digest.reset();
    byte[] input = digest.digest(credentials.getBytes());
    return byteToBase64(input);
} catch (NoSuchAlgorithmException e) {
    throw new RuntimeException(e);