Java Hash Calculate getHash(byte[] data)

Here you can find the source of getHash(byte[] data)

Description

get Hash

License

Apache License

Declaration

public static String getHash(byte[] data) throws NoSuchAlgorithmException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String getHash(byte[] data) throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] hash = digest.digest(data);

        String hexHash = byte2hex(hash);

        return hexHash;
    }//  w  ww .j  a  v  a 2 s.c om

    /**
     * Converts a hash into its hexadecimal string representation.
     *
     * @param bytes
     *        the byte array to convert
     * @return the hexadecimal string representation
     */
    private static String byte2hex(byte[] bytes) {
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

        StringBuffer sb = new StringBuffer();
        for (byte b : bytes) {
            sb.append(hexChars[b >> 4 & 0xf]);
            sb.append(hexChars[b & 0xf]);
        }

        return new String(sb);
    }
}

Related

  1. calculateHash(String str)
  2. calculateHashCode(byte a[])
  3. getHash(byte[] bytes)
  4. getHash(byte[] bytes)
  5. getHash(byte[] data)
  6. getHash(byte[] data, int offset, int length)
  7. getHash(byte[] inputBytes)
  8. getHash(byte[]... bytesToHash)
  9. getHash(File file)