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 getSignatureString(Signature sig, String type) {
    byte[] hexBytes = sig.toByteArray();
    String fingerprint = "error!";
    try {/*from   www. jav a2s  . com*/
        MessageDigest digest = MessageDigest.getInstance(type);
        if (digest != null) {
            byte[] digestBytes = digest.digest(hexBytes);
            StringBuilder sb = new StringBuilder();
            for (byte digestByte : digestBytes) {
                sb.append((Integer.toHexString((digestByte & 0xFF) | 0x100)).substring(1, 3));
            }
            fingerprint = sb.toString();
        }
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return fingerprint;
}

From source file:fi.laverca.util.X509Util.java

/** 
 * Calculates a SHA-1 hash of the given byte[] certificate
 * @param cert Certificate as byte[]/*from   www  . j  a v  a2s .  c  om*/
 * @return SHA-1 hash of the cert  or null if the calculation failed
 */
public static byte[] certHash(final byte[] cert) {
    if (cert == null) {
        return null;
    }

    byte[] hash = null;
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        hash = md.digest(cert);
    } catch (Throwable t) {
        // never happens
    }

    return hash;
}

From source file:com.softenido.cafedark.image.hash.ImageHashBuilder.java

protected static byte[] buildDigest(int[] pixels) {
    try {/*from   www . ja  va 2  s  . co  m*/
        MessageDigest md = ParallelMessageDigest.getInstance(DEF_ALG);
        return md.digest(ArrayUtils.getByteArray(pixels));
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(ImageHashBuilder.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ArrayUtils.getByteArray(pixels);
}

From source file:Main.java

private static String getMD5(byte[] source) {
    try {/*from ww w.java2s. c  o m*/
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        StringBuffer result = new StringBuffer();
        for (byte b : md5.digest(source)) {
            result.append(Integer.toHexString((b & 0xf0) >>> 4));
            result.append(Integer.toHexString(b & 0x0f));
        }
        return result.toString();
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.ikon.util.SecureStore.java

/**
 * MD5 encoder/*www.  j  a  v a2  s .c  o m*/
 */
public static String md5Encode(byte[] src) throws NoSuchAlgorithmException {
    StringBuilder sb = new StringBuilder();
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] dst = md.digest(src);

    for (int i = 0; i < dst.length; i++) {
        sb.append(Integer.toHexString((dst[i] >> 4) & 0xf));
        sb.append(Integer.toHexString(dst[i] & 0xf));
    }

    return sb.toString();
}

From source file:database.DBAccountManager.java

private static String hashPassword(String password) {
    String digest;/*from  ww w  .  ja  v  a  2 s  .co  m*/
    try {
        MessageDigest md = MessageDigest.getInstance("md5");
        md.reset();
        byte[] bytes = md.digest(password.getBytes());
        digest = new BigInteger(1, bytes).toString(16);
    } catch (NoSuchAlgorithmException nsae) {
        nsae.printStackTrace();
        digest = null;
    }
    return digest;
}

From source file:net.sf.keystore_explorer.crypto.digest.DigestUtil.java

/**
 * Get a digest of the supplied message.
 *
 * @param message/*from  ww  w.  j  a v  a  2  s  .co m*/
 *            The message to digest
 * @param digestType
 *            The message digest algorithm
 * @return The message digest
 * @throws CryptoException
 *             If message digester could not be created
 */
public static byte[] getMessageDigest(byte[] message, DigestType digestType) throws CryptoException {
    MessageDigest messageDigester = getMessageDigester(digestType);

    return messageDigester.digest(message);
}

From source file:com.zotoh.maedr.device.netty.NettpHplr.java

/**
 * @param key//from  w  w w. j  a v  a2s. c  o  m
 * @return
 */
public static String calcHybiSecKeyAccept(String key) {
    // add fix GUID according to 
    // http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10
    String k = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    String rc = "";
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] bits = md.digest(k.getBytes("utf-8"));
        rc = Base64.encodeBase64String(bits);
    } catch (Exception e) {
        //TODO
    }
    return rc;
}

From source file:fr.esecure.banking.metier.utils.PasswordGenerator.java

public static String crypter(String password) {
    StringBuilder stringBuilder = null;

    try {//ww  w  .j  av  a  2 s.co  m
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        byte[] hash = messageDigest.digest(password.getBytes("UTF-8"));
        stringBuilder = new StringBuilder();
        for (int i = 0; i < hash.length; i++) {
            stringBuilder.append(Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1));
        }
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(PasswordGenerator.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(PasswordGenerator.class.getName()).log(Level.SEVERE, null, ex);
    }
    return stringBuilder.toString();
}

From source file:eds.component.encryption.EncryptionUtility.java

public static String hashText(String text, EncryptionType type) {
    byte[] hash;/*from  www .ja v  a 2  s  . c  om*/
    MessageDigest md;
    try {
        //md = MessageDigest.getInstance("SHA-256");
        md = MessageDigest.getInstance(type.toString());
        hash = md.digest(text.getBytes("UTF-8"));
        String hashedText = new String(Hex.encodeHex(hash));//String.format("%032x", new BigInteger(hash));;//String.format("%032x", Arrays.toString(hash));

        return hashedText;
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
        throw new RuntimeException("No such algorithm or encoding method: " + ex.getMessage());
    }
}