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 sha1(String string) {
    byte[] hash;//from w  w  w .  j a  va  2s  .  c  o  m
    try {
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        sha1.update(string.getBytes("UTF-8"));
        hash = sha1.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Huh, SHA-1 should be supported?", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Huh, SHA-1 UTF-8 should be supported?", e);
    }
    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10)
            hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}

From source file:Main.java

public static String certHash(byte[] encoded, String digest) {
    try {// w w  w.  jav  a  2 s  . c o  m
        MessageDigest md = MessageDigest.getInstance(digest);
        md.update(encoded);
        return hexString(md.digest());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static String getMD5(String input) {
    String result = null;//  w w  w  .  j a  v a  2s.c  om

    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(input.getBytes());
        byte[] digest = md.digest();

        StringBuffer sb = new StringBuffer();

        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
        }

        result = sb.toString();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        return result;
    }

}

From source file:com.constellio.app.modules.es.connectors.http.utils.DigestUtil.java

public static String digest(final byte[] content) throws NoSuchAlgorithmException {
    MessageDigest shaDigester = MessageDigest.getInstance("SHA");
    shaDigester.update(content);/*  w w  w  .  j a v  a  2  s. c  o  m*/
    byte[] shaDigest = shaDigester.digest();
    String digestString = new String(Base64.encodeBase64(shaDigest));

    return digestString;
}

From source file:Main.java

public static byte[] computeSHA1(byte[] convertme, int offset, int len) {
    try {/*from   w  ww .j ava2  s .co  m*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(convertme, offset, len);
        return md.digest();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new byte[20];
}

From source file:com.dtolabs.rundeck.core.plugins.PluginUtils.java

public static String generateShaIdFromName(String pluginName) {
    MessageDigest digest = DigestUtils.getSha256Digest();
    digest.update(pluginName.getBytes());
    return bytesAsHex(digest.digest()).substring(0, 12);
}

From source file:Main.java

public static String md5(byte[] data) {
    try {//  w ww  .  ja v a2  s .  c  o  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 String md5(final String s) {
    try {/*from  www  . j a v a  2  s .  c o  m*/
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
    }
    return "";
}

From source file:Main.java

public static String md5s(String plainText) {
    String str = "";
    try {/*from   ww w  .  j av a2 s. c  o  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(plainText.getBytes());
        byte b[] = md.digest();
        int i;
        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        str = buf.toString();

    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block   
        e.printStackTrace();
    }
    return str;
}

From source file:Main.java

public static String crypt(String senha) {
    try {//from   w  w w  .j  av  a 2  s .  com
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(senha.getBytes());

        byte messageDigest[] = digest.digest();
        StringBuffer hexString = new StringBuffer();

        for (int i = 0; i < messageDigest.length; i++) {
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return senha;
    }
}