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 hexSHA1(String value) {
    try {//from   w w  w  .java 2 s  .  c  o  m
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(value.getBytes("utf-8"));
        byte[] digest = md.digest();
        return byteToHexString(digest);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static String MD5Crypto(String str) {
    try {//from   w w  w .java  2 s.  c om
        // Create MD5 Hash
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(str.getBytes());
        byte messageDigest[] = digest.digest();
        return toHexString(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:Main.java

private static byte[] getHash(@NonNull X509Certificate certificate)
        throws NoSuchAlgorithmException, CertificateEncodingException {
    MessageDigest digest = java.security.MessageDigest.getInstance("SHA1");
    digest.update(certificate.getEncoded());
    return digest.digest();
}

From source file:Main.java

public static final String toMD5(final String toEncrypt) {
    try {//from ww  w. j  a  v a 2  s . c  o 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 String convertToMD5(String toEnc) {
    try {//from   ww  w  .  j a v 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

public static String md5(String input) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    md5.update(input.getBytes("UTF-8"));
    BigInteger hash = new BigInteger(1, md5.digest());
    return String.format("%1$032X", hash);
}

From source file:Main.java

public static String SHA1(String s) {
    try {//  w  ww  .ja v  a  2  s  . c  om
        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 encryption(String plainText) {
    String re_md5 = "";
    try {//www .j  a  v  a2s  . c om
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(plainText.getBytes());
        byte b[] = md.digest();
        int i = 0;

        StringBuilder sb = new StringBuilder("");
        for (int offset = 0, len = b.length; offset < len; offset++) {
            i = b[offset];
            if (i < 0) {
                i += 256;
            }
            if (i < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(i));
        }
        re_md5 = sb.toString();

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

From source file:Main.java

public static String hashkeyForDisk(String url) {
    try {//from ww  w.  j  av a 2s  .  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 String sha1(String str) {
    if (str == null || str.length() == 0) {
        return null;
    }//from  www  .jav a  2  s.  c  o  m

    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    try {
        MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
        mdTemp.update(str.getBytes());

        byte[] md = mdTemp.digest();
        int j = md.length;
        char buf[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
            buf[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(buf);
    } catch (Exception e) {
        return null;
    }
}