Example usage for java.security MessageDigest digest

List of usage examples for java.security MessageDigest digest

Introduction

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

Prototype

public byte[] digest() 

Source Link

Document

Completes the hash computation by performing final operations such as padding.

Usage

From source file:Main.java

public static String md5(String s) {
    try {//  ww w  .j av a 2s  .  c o  m
        MessageDigest digester = MessageDigest.getInstance("MD5");
        digester.update(s.getBytes("UTF-8"));
        byte[] a = digester.digest();
        int len = a.length;
        StringBuilder sb = new StringBuilder(len << 1);

        for (int i = 0; i < len; i++) {
            sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
            sb.append(Character.forDigit(a[i] & 0x0f, 16));
        }

        return sb.toString();
    } catch (UnsupportedEncodingException e) {
        return "";
    } catch (NoSuchAlgorithmException e) {
        return "";
    }
}

From source file:Main.java

public static String imageDigest(BufferedImage img) {

    // write image to byte stream
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {/*ww w .  j  a  v a2 s  . c  o m*/
        ImageIO.write(img, "png", os);
        os.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    byte[] data = os.toByteArray();

    // compute md5 hash
    byte[] hash = null;
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(data);
        hash = md.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }

    // convert to string
    String hexString = "";
    for (int i = 0; i < hash.length; i++) {
        hexString += Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1);
    }
    return hexString;
}

From source file:Main.java

public static String hashMD5(String s) {
    try {/*from   www  .  j  a  v  a 2  s.  co  m*/
        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;
    }
}

From source file:Main.java

public static String stringToMD5(String str) {

    try {//from  w  w w .j av a2 s.  c o  m
        byte[] strTemp = str.getBytes();
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(strTemp);
        return toHexString(mdTemp.digest());
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String md5(final String s) {
    final String MD5 = "MD5";
    try {/*  ww  w. j a  va2  s .  c  o m*/
        // Create MD5 Hash
        MessageDigest digest = MessageDigest.getInstance(MD5);
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuilder hexString = new StringBuilder();
        for (byte aMessageDigest : messageDigest) {
            String h = Integer.toHexString(0xFF & aMessageDigest);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String computeMD5(byte[] input) {
    try {//w ww  . j  ava2 s. c  o  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(input, 0, input.length);
        byte[] md5bytes = md.digest();

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

From source file:Main.java

public static String toMd5(String input) {
    String output = input;/*from ww  w . j  a  v  a 2s  .  c  om*/
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(input.getBytes(), 0, input.length());
        output = new BigInteger(1, md5.digest()).toString(16);

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return output;
}

From source file:Main.java

private static String md5(final String s) {
    final String MD5 = "MD5";
    try {/* www .  j  av  a 2  s . c o m*/
        MessageDigest digest = MessageDigest.getInstance(MD5);
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        StringBuilder hexString = new StringBuilder();
        for (byte aMessageDigest : messageDigest) {
            String h = Integer.toHexString(0xFF & aMessageDigest);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static final String md5(String tps) {
    final String MD5 = "MD5";
    try {/*from  w  w  w  .  ja  v a2 s  .c  o m*/
        // Create MD5 Hash
        MessageDigest digest = MessageDigest.getInstance(MD5);
        digest.update(tps.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuilder hexString = new StringBuilder();
        for (byte aMessageDigest : messageDigest) {
            String h = Integer.toHexString(0xFF & aMessageDigest);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String md5L(String input) {
    try {/* w  w  w  .  j a va  2 s  . co m*/
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(input.getBytes());
        byte[] md = mdInst.digest();
        StringBuilder hexString = new StringBuilder();
        for (byte aMd : md) {
            String shaHex = Integer.toHexString(aMd & 0xFF);
            if (shaHex.length() < 2) {
                hexString.append(0);
            }
            hexString.append(shaHex);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}