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(String Data)
get Hash
try {
    MessageDigest sha = MessageDigest.getInstance("SHA-512");
    sha.update(Data.getBytes());
    byte result[] = sha.digest();
    StringBuilder sb = new StringBuilder();
    for (byte data : result) {
        sb.append(Integer.toString((data & 0xff) + 0x100, 16).substring(1));
    return sb.toString();
} catch (Exception e) {
    e.printStackTrace();
return null;
StringgetHash(String fileName, String hashType)
get Hash
InputStream fis;
fis = new FileInputStream(fileName);
byte[] buffer = new byte[1024];
MessageDigest md5 = MessageDigest.getInstance(hashType);
int numRead = 0;
while ((numRead = fis.read(buffer)) > 0) {
    md5.update(buffer, 0, numRead);
fis.close();
return byteArrayToHexString(md5.digest());
StringgetHash(String givenStr)
Get hash sum of given string.
StringBuffer hexString = new StringBuffer();
try {
    java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
    md.update(givenStr.getBytes());
    byte[] hash = md.digest();
    for (int i = 0; i < hash.length; i++) {
        if ((0xff & hash[i]) < 0x10) {
            hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
...
StringgetHash(String input)
get Hash
input += "vkoqasnm";
try {
    java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-1");
    byte[] array = md.digest(input.getBytes());
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < array.length; ++i) {
        sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
    return sb.toString();
} catch (java.security.NoSuchAlgorithmException e) {
    throw new RuntimeException(e);
StringgetHash(String input)
Return a hash of the given String .
try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(input.getBytes());
    byte byteData[] = md.digest();
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
        String hex = Integer.toHexString(0xff & byteData[i]);
        if (hex.length() == 1)
...
byte[]getHash(String key)
get Hash
byte[] hash = new byte[6];
System.arraycopy(md5.digest(key.getBytes(Charset.forName("UTF-8"))), 0, hash, 0, 6);
return hash;
StringgetHash(String message)
get Hash
return getHash(message, "MD5");
byte[]getHash(String pass, String algorithm)
get Hash
MessageDigest md = MessageDigest.getInstance(algorithm);
md.update(pass.getBytes());
byte[] hash = md.digest();
return hash;
StringgetHash(String password, byte[] salt)
From a password returns the corresponding digest using a generated salt
char[] passChars = password.toCharArray();
PBEKeySpec spec = new PBEKeySpec(passChars, salt, ITERATION_NUMBER, 512);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = skf.generateSecret(spec).getEncoded();
return byteToBase64(salt) + ":" + byteToBase64(hash);
byte[]getHash(String password, byte[] salt)
get Hash
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.reset();
digest.update(salt);
byte[] input = digest.digest(password.getBytes("UTF-8"));
for (int i = 0; i < 13; i++) {
    digest.reset();
    input = digest.digest(input);
return input;