Java Hash Code Calculate generateHash(final String msg, final String hashAlgorithm)

Here you can find the source of generateHash(final String msg, final String hashAlgorithm)

Description

generate Hash

License

Open Source License

Declaration

public static String generateHash(final String msg, final String hashAlgorithm) 

Method Source Code


//package com.java2s;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String generateHash(final String msg, final String hashAlgorithm) {

        if (!hashAlgorithm.equals("plain")) {
            StringBuffer hexString = new StringBuffer();

            try {

                MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm);
                messageDigest.update(msg.getBytes());
                byte[] digest = messageDigest.digest();

                for (byte aDigest : digest) {

                    String val = Integer.toHexString(0xFF & aDigest);

                    if (val.length() < 2) {
                        val = "0" + val;
                    }//  www . j  av  a  2  s.  c o m

                    hexString.append(val);
                }

            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }

            return hexString.toString();
        } else {
            return msg;
        }
    }
}

Related

  1. generateHash(byte[] data)
  2. generateHash(char[] password, byte[] salt)
  3. generateHash(File file)
  4. generateHash(final String data)
  5. generateHash(final String input)
  6. generateHash(String algo, byte[]... bytes)
  7. generateHash(String input)
  8. generateHash(String input, String salt)
  9. generateHash(String item)