Java Hash Code Calculate generateHash(String input, String salt)

Here you can find the source of generateHash(String input, String salt)

Description

generate Hash

License

Open Source License

Declaration

public static String generateHash(String input, String salt)
             throws NoSuchAlgorithmException, UnsupportedEncodingException 

Method Source Code

//package com.java2s;

import java.io.UnsupportedEncodingException;

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

public class Main {
    public static String generateHash(String input, String salt)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        // SHA or MD5
        MessageDigest md = MessageDigest.getInstance("MD5");
        String hash = "";

        if (salt == null) {
            salt = "";
        }/*  w  w w.ja va 2 s .  co  m*/

        input += salt;
        byte[] data = input.getBytes("US-ASCII");

        md.update(data);
        byte[] digest = md.digest();
        for (int i = 0; i < digest.length; i++) {
            String hex = Integer.toHexString(digest[i]);
            if (hex.length() == 1)
                hex = "0" + hex;
            hex = hex.substring(hex.length() - 2);
            hash += hex;
        }

        return hash;
    }
}

Related

  1. generateHash(final String data)
  2. generateHash(final String input)
  3. generateHash(final String msg, final String hashAlgorithm)
  4. generateHash(String algo, byte[]... bytes)
  5. generateHash(String input)
  6. generateHash(String item)
  7. generateHash(String plaintext)
  8. generateHash(String plainText, String hashType)
  9. generateHash(String serviceUri, String username, String password)