Java Hash Calculate getHashMD5(String arg)

Here you can find the source of getHashMD5(String arg)

Description

get Hash MD

License

Open Source License

Declaration

public static String getHashMD5(String arg) 

Method Source Code


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

import java.io.UnsupportedEncodingException;

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

public class Main {
    public static String getHashMD5(String arg) {
        return generateMD5Hash(arg);
    }//w w  w  .  j  av a  2 s . co m

    /**
     * Generates a MD5 Hash for the argument string.
     * 
     * @param arg
     * @return
     */
    public static String generateMD5Hash(String arg) {
        try {
            byte[] bytesOfMessage = arg.getBytes("UTF-8");

            MessageDigest md;
            md = MessageDigest.getInstance("MD5");
            byte[] thedigest = md.digest(bytesOfMessage);

            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < thedigest.length; i++) {
                String hex = Integer.toHexString(0xff & thedigest[i]);
                if (hex.length() == 1)
                    hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. getHashedPassword(String plainPassword)
  2. getHashFromString(String str)
  3. getHashInstance(final String alg)
  4. getHashKey(String input)
  5. getHashMD5(File file)
  6. getHashMD5(String str)
  7. getHashMD5(String text)
  8. getHashMDFive(String str)
  9. getHashOf(String s)