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 sha1(String text) {
    MessageDigest md;//w w w  . j  a v  a 2 s . co m
    try {
        md = MessageDigest.getInstance("SHA-1");
        byte[] sha1hash = new byte[40];
        md.update(text.getBytes("UTF-8"), 0, text.length());
        sha1hash = md.digest();
        return convertToHex(sha1hash);
    } catch (Exception e) {
    }

    return "";
}

From source file:Main.java

public static String md5(String value) {
    String result;//from ww  w  . j a  va  2  s.  c  o  m
    try {
        MessageDigest md = MessageDigest.getInstance("md5");
        result = byteToHexString(md.digest(value.getBytes("utf-8")));
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

public static String getMd5(String source) {
    try {/*  ww w.j av  a 2 s.  co  m*/
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(source.getBytes());
        BigInteger bi = new BigInteger(1, md5.digest());
        return String.format("%032x", bi);
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static String convertToMD5(String toEnc) {
    try {/*from www  .j av  a 2s.c om*/
        MessageDigest mdEnc = MessageDigest.getInstance("MD5");
        mdEnc.update(toEnc.getBytes(), 0, toEnc.length());
        return new BigInteger(1, mdEnc.digest()).toString(16);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

private static byte[] computeSha256(final byte[] data) {
    try {//from w  ww  .  j av a2s  . c  o m
        return MessageDigest.getInstance("SHA-256").digest(data);
    } catch (final NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:Main.java

public static String MD5(String str) {
    MessageDigest md5 = null;//from w w  w .  jav  a 2  s.com
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }

    char[] charArray = str.toCharArray();
    byte[] byteArray = new byte[charArray.length];
    for (int i = 0; i < charArray.length; i++) {
        byteArray[i] = (byte) charArray[i];
    }

    byte[] md5Bytes = md5.digest(byteArray);
    StringBuffer hexValue = new StringBuffer();
    for (int i = 0; i < md5Bytes.length; i++) {
        int val = ((int) md5Bytes[i]) & 0xff;
        if (val < 16) {
            hexValue.append("0");
        }
        hexValue.append(Integer.toHexString(val));
    }
    return hexValue.toString();
}

From source file:Main.java

public static String md5(String input) {
    String res = "";

    try {/*from   w ww  .  ja  v a 2  s . com*/
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(input.getBytes());
        byte[] md5 = algorithm.digest();
        String tmp = "";
        for (int i = 0; i < md5.length; i++) {
            tmp = (Integer.toHexString(0xFF & md5[i]));
            if (tmp.length() == 1) {
                res += "0" + tmp;
            } else {
                res += tmp;
            }
        }
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }

    return res;
}

From source file:Main.java

public static String MD5(String md5) {
    try {/*ww w . j a  v  a 2  s .c o m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
    }
    return null;
}

From source file:Main.java

public static String MD5(String inStr) {
    MessageDigest md5 = null;/* w  ww .ja v a  2  s . c  om*/
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
        return "";
    }
    char[] charArray = inStr.toCharArray();
    byte[] byteArray = new byte[charArray.length];

    for (int i = 0; i < charArray.length; i++)
        byteArray[i] = (byte) charArray[i];

    byte[] md5Bytes = md5.digest(byteArray);

    StringBuffer hexValue = new StringBuffer();

    for (int i = 0; i < md5Bytes.length; i++) {
        int val = ((int) md5Bytes[i]) & 0xff;
        if (val < 16)
            hexValue.append("0");
        hexValue.append(Integer.toHexString(val));
    }

    return hexValue.toString();
}

From source file:Main.java

public static byte[] calculateSHA1(byte[] data) {
    try {/*ww  w.j  a  va  2  s  . c o  m*/
        MessageDigest md = MessageDigest.getInstance("SHA1");
        return md.digest(data);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}