Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

In this page you can find the example usage for java.security MessageDigest getInstance.

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:Main.java

public static String md5Hex(String message) {
    try {// w  w w .j a va 2  s  .c  o m
        MessageDigest md = MessageDigest.getInstance("MD5");
        return hex(md.digest(message.getBytes()));
    } catch (NoSuchAlgorithmException e) {
    }
    return null;
}

From source file:Main.java

public static String MD5(String source) {
    String resultHash = null;/*from w w  w.java  2  s  .  c o  m*/
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.reset();
        md5.update(source.getBytes("UTF-8"));
        byte[] result = md5.digest();
        StringBuffer buf = new StringBuffer(result.length * 2);
        for (int i = 0; i < result.length; i++) {
            int intVal = result[i] & 0xff;
            if (intVal < 0x10) {
                buf.append("0");
            }
            buf.append(Integer.toHexString(intVal));
        }
        resultHash = buf.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resultHash.toString();
}

From source file:Main.java

public static String sha1(String s) {
    try {//  w ww  .j a  va  2  s.  c o  m
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            hexString.append(Integer.toHexString((0xFF & messageDigest[i]) | 0x100).substring(1));
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        return "";
    }
}

From source file:Main.java

public final static String MD5(String s) {
    try {/*from   ww  w  .  jav  a 2s.co m*/
        byte[] btInput = s.getBytes();
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(btInput);
        byte[] md = mdInst.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < md.length; i++) {
            int val = ((int) md[i]) & 0xff;
            if (val < 16)
                sb.append("0");
            sb.append(Integer.toHexString(val));
        }
        return sb.toString();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String crypt(String senha) {
    try {//w  ww .  j a va 2  s .co m
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(senha.getBytes());

        byte messageDigest[] = digest.digest();
        StringBuffer hexString = new StringBuffer();

        for (int i = 0; i < messageDigest.length; i++) {
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return senha;
    }
}

From source file:Main.java

public static String SHA1(String s) {
    try {//from  w  w w. jav a 2 s .  c  o m
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();
        return toHexString(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String sha256(String base) {
    try {//from   w  w  w.j a v  a2  s  . co  m
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hash = digest.digest(base.getBytes("UTF-8"));
        StringBuffer hexString = new StringBuffer();

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

        return hexString.toString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static String encryptMD5(String password) {
    try {//from  w  w w.ja v  a 2  s.  c  om
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(password.getBytes());
        byte byteData[] = md.digest();
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            String hex = Integer.toHexString(0xff & byteData[i]);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String md5(final String s) {
    try {/*from  w w w. j  av  a2 s  .  com*/
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
    }
    return "";
}

From source file:Main.java

public static byte[] digest(byte[] input, String algoritmo) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance(algoritmo);
    md.reset();//from www.  j  a va  2 s  . co m
    return md.digest(input);
}