Example usage for java.security MessageDigest update

List of usage examples for java.security MessageDigest update

Introduction

In this page you can find the example usage for java.security MessageDigest update.

Prototype

public void update(byte[] input, int offset, int len) 

Source Link

Document

Updates the digest using the specified array of bytes, starting at the specified offset.

Usage

From source file:com.ery.ertc.estorm.util.MD5Hash.java

/**
 * Given a byte array, returns its MD5 hash as a hex string. Only "length" number of bytes starting at "offset" within the byte array
 * are used./*from  w w w  .  ja v a 2  s  .  co m*/
 * 
 * @param key
 *            the key to hash (variable length byte array)
 * @param offset
 * @param length
 * @return MD5 hash as a 32 character hex string.
 */
public static String getMD5AsHex(byte[] key, int offset, int length) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(key, offset, length);
        byte[] digest = md.digest();
        return new String(Hex.encodeHex(digest));
    } catch (NoSuchAlgorithmException e) {
        // this should never happen unless the JDK is messed up.
        throw new RuntimeException("Error computing MD5 hash", e);
    }
}

From source file:Main.java

public static byte[] SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md;
    md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes(), 0, text.length());
    // "iso-8859-1"
    sha1hash = md.digest();/*  w ww.j  a  v a  2s  .co m*/
    return (sha1hash);
}

From source file:Main.java

public static boolean verifyChecksum(byte[] bytesWithChecksumm) {
    try {// w  w w .ja  v  a2 s  .c  o  m
        if (bytesWithChecksumm == null || bytesWithChecksumm.length < 5) {
            return false;
        }
        MessageDigest digestSha = MessageDigest.getInstance("SHA-256");
        digestSha.update(bytesWithChecksumm, 0, bytesWithChecksumm.length - 4);
        byte[] first = digestSha.digest();
        byte[] calculatedDigest = digestSha.digest(first);
        boolean checksumValid = true;
        for (int i = 0; i < 4; i++) {
            if (calculatedDigest[i] != bytesWithChecksumm[bytesWithChecksumm.length - 4 + i]) {
                checksumValid = false;
            }
        }
        return checksumValid;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:ke.co.tawi.babblesms.server.utils.security.SecurityUtil.java

/**
 * Return the MD5 hahs of a String. It will work correctly for most 
 * strings. A word which does not work correctly is "michael" (check 
 * against online MD5 hash tools).   //w w w.  j ava 2 s.  c  o m
 *
 * @param toHash plain text string to encryption
 * @return an md5 hashed string
 */
public static String getMD5Hash(String toHash) {
    String md5Hash = "";

    try {
        MessageDigest md = MessageDigest.getInstance("MD5");

        md.update(toHash.getBytes(), 0, toHash.length());

        md5Hash = new BigInteger(1, md.digest()).toString(16);

    } catch (NoSuchAlgorithmException e) {
        logger.error("NoSuchAlgorithmException while getting MD5 hash of '" + toHash + "'");
        logger.error(ExceptionUtils.getStackTrace(e));
    }

    return md5Hash;
}

From source file:Main.java

public static boolean verifyChecksum(byte[] bytesWithChecksumm) {
    try {//ww  w  . j  a  va 2  s.c  om
        if (bytesWithChecksumm == null || bytesWithChecksumm.length < 5) {
            return false;
        }
        MessageDigest digestSha = MessageDigest.getInstance("SHA-256");
        digestSha.update(bytesWithChecksumm, 0, bytesWithChecksumm.length - 4);
        byte[] first = digestSha.digest();
        byte[] calculatedDigest = digestSha.digest(first);
        boolean checksumValid = true;
        for (int i = 0; i < 4; i++) {
            if (calculatedDigest[i] != bytesWithChecksumm[bytesWithChecksumm.length - 4 + i]) {
                checksumValid = false;
            }
        }
        return checksumValid;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Digests the given message with the given algorithm
 *
 * @param originalMessage the original message
 * @param algorithm the algorithm/*from   w  ww.java2s .c o m*/
 * @return the byte [ ]
 * @throws NoSuchAlgorithmException if the given algorithm does not exist in JCA
 */
public static byte[] digestMessage(byte[] originalMessage, String algorithm) throws NoSuchAlgorithmException {
    MessageDigest messageDigest;
    messageDigest = MessageDigest.getInstance(algorithm);

    messageDigest.update(originalMessage, 0, originalMessage.length);
    return messageDigest.digest();
}

From source file:com.netflix.exhibitor.core.s3.S3Utils.java

public static byte[] md5(byte[] buffer, int length) {
    try {/*from w w  w. j av a 2  s  .  com*/
        MessageDigest mdigest = MessageDigest.getInstance("MD5");
        mdigest.update(buffer, 0, length);
        return mdigest.digest();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    text = "l;jfa@#" + text + "fads@)*3 ";
    MessageDigest md;
    md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();/*from  www  .j a  v  a2  s  .c  om*/
    return convertToHex(sha1hash);
}

From source file:Main.java

/**
 * This method uses MessageDigest class to convert the passed string to
 * SHA-1 hexadecimal text.//from w ww .j  av  a 2 s  .c om
 *
 * @param text Input string
 * @return Return SHA-1 value
 */
public static String convertToSHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[30];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();
    return convertToHex(sha1hash);
}

From source file:com.honnix.yaacs.util.MD5.java

/**
 * Get MD5 sum of the input data.//from w w  w.ja  va 2 s.  c  o  m
 * 
 * @param data
 *            input data used to generate MD5 sum
 * @return MD5 sum of the input data, or null if no MD5 algorithm could be
 *         found
 */
public static String getMD5Sum(String data) {
    String encodedData = null;

    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");

        messageDigest.update(data.getBytes("US-ASCII"), 0, data.length());
        encodedData = new BigInteger(1, messageDigest.digest()).toString(16);
    } catch (NoSuchAlgorithmException e) {
        StringBuilder sb = new StringBuilder("No MD5 algorithm.").append(" Could not apply validation check.");

        LOG.error(sb.toString(), e);
    } catch (UnsupportedEncodingException e) {
        LOG.error("This should not happen anyway.", e);
    }

    return encodedData;
}