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 byte[] toMD5(byte[] bytes) throws Exception {
    try {//from  w w w. j  a  v a 2s  .c  o m
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        return algorithm.digest(bytes);
    } catch (NoSuchAlgorithmException e) {
        throw new Exception(e);
    }
}

From source file:Main.java

public static String encode(String text) {
    try {//w w w .  j ava 2 s . co  m
        MessageDigest digest = MessageDigest.getInstance("md5");
        byte[] result = digest.digest(text.getBytes());
        StringBuilder sb = new StringBuilder();
        for (byte b : result) {
            int number = b & 0xff;
            String hex = Integer.toHexString(number);
            if (hex.length() == 1) {
                sb.append("0" + hex);
            } else {
                sb.append(hex);
            }
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static String getStringMD5(String str) {
    String value = null;/* w w  w.j a v a2s.  c o  m*/
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(str.getBytes());
        BigInteger bi = new BigInteger(1, md5.digest());
        value = bi.toString(16).toUpperCase();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

public static String md5(byte[] data) {
    try {/*from w w  w.  j a  v  a 2s . co m*/
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(data);
        return bytesToHex(messageDigest.digest());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static final String toMD5(final String toEncrypt) {
    try {/*from w  w w. j a va2  s  .co  m*/
        final MessageDigest digest = MessageDigest.getInstance("md5");
        digest.update(toEncrypt.getBytes());
        final byte[] bytes = digest.digest();
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(String.format("%02X", bytes[i]));
        }
        return sb.toString().toLowerCase();
    } catch (Exception exc) {
        return null; // Impossibru!
    }
}

From source file:Main.java

public static final String encryptMd5String(String s) {
    try {//from ww w . j av a  2 s .  c  om
        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 hash(String pass) {
    MessageDigest md = null;/*from w ww.  j  av  a 2  s  .  com*/
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    md.update(pass.getBytes());
    byte byteData[] = md.digest();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
        sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }
    return sb.toString();

}

From source file:Main.java

public static String hashkeyForDisk(String url) {
    try {/*from w w  w. ja va 2 s. c om*/
        final MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(url.getBytes());
        return bytesToHexString(digest.digest());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return String.valueOf(url.hashCode());
    }
}

From source file:Main.java

public static byte[] computeSHA256(byte[] convertme, int offset, int len) {
    try {//  ww w .  jav  a2  s  . c o m
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(convertme, offset, len);
        return md.digest();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String hashMD5(String s) {
    try {/*from  w w w  .j a  va  2  s.c  om*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(s.getBytes(), 0, s.length());
        String result = new BigInteger(1, m.digest()).toString();
        while (result.length() < 32) {
            result = "0" + result;
        }
        return result;
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}