Java Hash Code Calculate generateHash(String item)

Here you can find the source of generateHash(String item)

Description

generate Hash

License

Apache License

Declaration

public static String generateHash(String item) 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 generateHash(String item) throws NoSuchAlgorithmException {
        String generatedPassword = null;
        try {/*from ww w.ja  v  a  2  s .c om*/
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(item.getBytes());
            byte[] bytes = md.digest();
            generatedPassword = ByteArrayToString(bytes);
        } catch (NoSuchAlgorithmException e) {
            throw new NoSuchAlgorithmException(e);
        }

        return generatedPassword;
    }

    private static String ByteArrayToString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < bytes.length; i++) {
            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    }
}

Related

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