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:org.runway.utils.TextUtils.java

/**
 * Generates MD5 for the text passed/* w  w w . j  a v  a 2  s.  co  m*/
 * @param text
 * @return
 */
public static String genMD5(String text) {
    MessageDigest m;
    try {
        m = MessageDigest.getInstance("MD5");
        m.update(text.getBytes(), 0, text.length());
        return new BigInteger(1, m.digest()).toString(16);
    } catch (NoSuchAlgorithmException e) {// return same text if there was
        // an error
        e.printStackTrace();
        return text;
    }
}

From source file:com.cirrus.utils.EncryptionUtils.java

private static String makePasswordHash(final String value, final String salt) {
    try {//w  w  w. ja  v  a2s . c  om

        final String saltedAndHashed = value + "," + salt;
        final MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(saltedAndHashed.getBytes());
        final byte hashedBytes[] = (new String(digest.digest(), "UTF-8")).getBytes();
        final Base64 base64 = new Base64();
        return Arrays.toString(base64.encode(hashedBytes)) + '_' + salt;
    } catch (final NoSuchAlgorithmException e) {
        throw new RuntimeException("MD5 is not available", e);
    } catch (final UnsupportedEncodingException e) {
        throw new RuntimeException("UTF-8 unavailable?  Not a chance", e);
    }
}

From source file:Main.java

public static String md5hash(String string) {
    String generatedString = null;
    try {//  w w w.j av  a2  s .  co m
        // Create MessageDigest instance for MD5
        MessageDigest md = MessageDigest.getInstance("MD5");
        // Add password bytes to digest
        md.update(string.getBytes());
        // Get the hash's bytes
        byte[] bytes = md.digest();
        // This bytes[] has bytes in decimal format;
        // Convert it to hexadecimal format
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        // Get complete hashed password in hex format
        generatedString = sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return generatedString;
}

From source file:com.continusec.client.Util.java

/**
 * Calculate the Merkle Tree Node Hash for an existing left and right hash (HASH(chr(1) || l || r)).
 * @param l the left node hash./*from   w  ww.  j a  va2  s.  c  o m*/
 * @param r the right node hash.
 * @return the node hash for the combination.
 */
public static final byte[] nodeMerkleTreeHash(byte[] l, byte[] r) {
    MessageDigest d = DigestUtils.getSha256Digest();
    d.update((byte) 1);
    d.update(l);
    d.update(r);
    return d.digest();
}

From source file:de.marx_labs.utilities.common.util.HashUtil.java

public static String hash(String value) {
    MessageDigest digest;
    try {/*  w  ww . j  a v a  2s. c o  m*/
        digest = MessageDigest.getInstance("SHA-256");
        digest.update(value.getBytes("UTF-8"));
        byte[] hash = digest.digest();

        return new String(hash);
    } catch (Exception e) {
        logger.error("", e);
    }
    return null;
}

From source file:Main.java

public static String getApkSignatureMD5String(Signature signature) {
    MessageDigest md;
    try {/*from   w  ww . j a v  a 2s .com*/
        md = MessageDigest.getInstance("MD5");

        md.update(signature.toByteArray());

        byte[] digest = md.digest();

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

From source file:com.liferay.mobile.sdk.util.PortraitUtil.java

protected static void appendToken(StringBuilder sb, String uuid) throws Exception {

    if (Validator.isNull(uuid)) {
        return;/* w  w w  .  j  av  a  2  s.  c om*/
    }

    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    digest.update(uuid.getBytes());
    byte[] bytes = digest.digest();
    byte[] encodedBytes = Base64.encodeBase64(bytes);
    String token = new String(encodedBytes);

    sb.append("&img_id_token=");
    sb.append(URLEncoder.encode(token, "UTF8"));
}

From source file:com.smallfe.clerk.util.DigestUtil.java

public static String getSHA256(String message) {
    try {/*from www  .  j  a va 2  s. com*/
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(message.getBytes());

        byte byteData[] = md.digest();

        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(DigestUtil.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:Componentes.EncryptionMD5.java

public static String getStringMessageDigest(String message, String algorithm) {
    byte[] digest = null;
    byte[] buffer = message.getBytes();
    try {//from  w w  w  .  j  av  a2  s . co m
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        messageDigest.reset();
        messageDigest.update(buffer);
        digest = messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Error creando Digest");
    }
    return toHexadecimal(digest);
}

From source file:ezbake.warehaus.WarehausUtils.java

public static Text getKey(String uri) {
    try {//from  w  ww.  j  a  v  a2  s .c  o m
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(uri.getBytes());
        byte[] hash = messageDigest.digest();
        BigInteger bigInt = new BigInteger(1, hash);
        String hashtext = bigInt.toString(16);
        return new Text(hashtext + ":" + uri);
    } catch (NoSuchAlgorithmException e) {
        // We hopefully should never end up here
        logger.error("NoSuchAlgorithmException thrown while attempting " + "to hash key.", e);
        throw new RuntimeException(e);
    }
}