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(byte[] input) 

Source Link

Document

Performs a final update on the digest using the specified array of bytes, then completes the digest computation.

Usage

From source file:Main.java

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

From source file:Main.java

public static String MD5(String md5) {
    try {//from w ww  . ja  va 2  s .c om
        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:brainleg.app.util.EncryptionUtil.java

private static byte[] generateDigest(String algorithm, byte[] data) {
    if (data == null) {
        return null;
    }//www  . ja va  2  s . com

    try {
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        byte[] digest = messageDigest.digest(data);
        return digest;
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:Main.java

static public String digest(String input) {
    try {/*from  www .  ja  v a  2  s  . co  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        return new BigInteger(1, md.digest(input.getBytes())).toString(16).toUpperCase();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String calculateMD5(String string, String encoding) {
    try {/*from  www  .  jav a2s.  co m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(string.getBytes(Charset.forName(encoding)));
        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 input) {
    try {/*from   ww  w.  ja  v  a 2 s  . c o m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(input.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte anArray : array) {
            sb.append(String.format("%02x", anArray));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        return null;
    }
}

From source file:Utils.java

public static String digest(String token) throws GeneralSecurityException, IOException {
    MessageDigest sha1 = MessageDigest.getInstance("SHA1");
    return byteArrayToHexStr(sha1.digest(token.getBytes("UTF-8")));
}

From source file:Main.java

/**
 * Returns SHA256 hash of the public key of a given certificate.
 *
 * @param cert the cert that should be used to retrieve the public key from.
 * @return SHA256 hash of the public key.
 *//*from   ww w.  j  av a  2  s. co  m*/
public static byte[] getPublicKeySha256(Certificate cert) {
    try {
        byte[] publicKey = cert.getPublicKey().getEncoded();
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        return digest.digest(publicKey);
    } catch (NoSuchAlgorithmException ex) {
        // This exception should never happen since SHA-256 is known algorithm
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

private static String getMd5(String md5) throws Exception {
    MessageDigest md = java.security.MessageDigest.getInstance(MD5);
    byte[] array = md.digest(md5.getBytes(CHARSET_NAME));
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < array.length; ++i) {
        sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
    }//from   ww w  .  j  a  va2 s .c om
    return sb.toString();
}

From source file:Main.java

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