Java Hash Calculate getHash(String phrase, String algorithm)

Here you can find the source of getHash(String phrase, String algorithm)

Description

get Hash

License

Open Source License

Declaration

public static String getHash(String phrase, String algorithm) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {

    public static String getHash(String phrase, String algorithm) {
        return getHexString(getByteHash(phrase, algorithm));
    }/*from   w  ww.j  ava 2 s  .  c  om*/

    private static String getHexString(byte[] bytes) {
        StringBuilder s = new StringBuilder();
        for (byte aByte : bytes) {
            int higherPart = ((aByte >> 4) & 0xf) << 4;
            int lowerPart = aByte & 0xf;
            if (higherPart == 0) {
                s.append('0');
            }
            s.append(Integer.toHexString(higherPart | lowerPart));
        }
        return s.toString();
    }

    private static byte[] getByteHash(String phrase, String algorithm) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            md.update(phrase.getBytes());
            return md.digest();
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }
}

Related

  1. getHash(String password, byte[] salt)
  2. getHash(String password, byte[] salt)
  3. getHash(String password, String bsalt)
  4. getHash(String password, String salt)
  5. getHash(String password, String salt)
  6. getHash(String s)
  7. getHash(String str, String algorithm)
  8. getHash(String strDate)
  9. getHash(String text)