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:ca.qc.adinfo.rouge.util.MD5.java

public static String hash(String value) {

    try {//  www .  j a v a 2 s.  com

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] md5hash = new byte[32];
        md.update(value.getBytes("iso-8859-1"), 0, value.length());
        md5hash = md.digest();

        return Hex.encodeHexString(md5hash);

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:baldrickv.s3streamingtool.Hash.java

/**
 * Cause apparently a hex encoded MD5 isn't good enough for S3.
 * They want a base64 encode of the raw binary hash.  blech.
 *///w w  w.j  a  v a2  s .c om
public static String getMd5ForS3(byte b[]) {
    try {
        MessageDigest sig = MessageDigest.getInstance("MD5");
        sig.update(b, 0, b.length);
        byte d[] = sig.digest();

        byte encoded[] = Base64.encodeBase64(d, false);

        return new String(encoded);

    } catch (Exception e) {

        e.printStackTrace();
        System.exit(-1);
        return null;
    }

}

From source file:Main.java

public static String getSHA(byte[] message, int length) {
    if (message == null || length <= 0) {
        return null;
    }//from ww  w  . ja v a 2 s . c  om
    try {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(message, 0, length);
        byte[] output = md.digest();
        String s = "";
        for (int i = 0; i < output.length; i++) {
            s += hexByte(output[i] + 128);
        }
        return s;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:baldrickv.s3streamingtool.Hash.java

public static String hash(String algo, int output_bits, byte b[], int offset, int size) {
    try {//from  ww  w .  jav a2  s.  com
        int output_bytes = output_bits / 4; //hex = 4 bits per byte
        MessageDigest sig = MessageDigest.getInstance(algo);
        sig.update(b, offset, size);
        byte d[] = sig.digest();

        StringBuffer s = new StringBuffer(output_bytes);
        BigInteger bi = new BigInteger(1, d);
        s.append(bi.toString(16));
        while (s.length() < output_bytes) {
            s.insert(0, '0');
        }
        return s.toString();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
        return null;
    }

}

From source file:org.runway.utils.TextUtils.java

/**
 * Generates MD5 for the text passed//from  w  w  w  .  j a va  2 s. com
 * @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:net.sf.jml.util.DigestUtils.java

private static void update(MessageDigest digest, ByteBuffer buffer) {
    if (buffer.hasArray()) {
        digest.update(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
    } else {/*from   www.j  a va 2s  .com*/
        byte[] b = new byte[buffer.remaining()];
        buffer.get(b);
        digest.update(b);
    }
}

From source file:Main.java

private static String sha1Hash(String toHash) {
    String hash = null;//from www . j  a v  a2 s.  c  o m
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        byte[] bytes = toHash.getBytes("UTF-8");
        digest.update(bytes, 0, bytes.length);
        bytes = digest.digest();

        // This is ~55x faster than looping and String.formating()
        hash = bytesToHex(bytes);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return hash;
}

From source file:Main.java

/**
 * Method for returning an md5 hash of a string.
 * //from   w w w  .  ja va  2  s . c o m
 * @param val
 *            the string to hash.
 * @return A hex string representing the md5 hash of the input.
 */
private static String md5(String val) {
    String result = null;

    if ((val != null) && (val.length() > 0)) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(val.getBytes(), 0, val.length());
            result = String.format("%032X", new BigInteger(1, md5.digest()));
        } catch (NoSuchAlgorithmException nsae) {
            result = val.substring(0, 32);
        }
    }

    return result;
}

From source file:Main.java

public static String sha1Hash(String text) {
    String hash = null;//  w  ww .j  a v a  2s  .co  m
    try {
        final MessageDigest digest = MessageDigest.getInstance("SHA-1");
        final byte[] bytes = text.getBytes("UTF-8");
        digest.update(bytes, 0, bytes.length);
        hash = convertToHex(digest.digest());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return hash;
}

From source file:Main.java

/**
 * Calculates an MD5 hash for a text./*from www  .  j  ava 2s.  c  om*/
 * 
 * @param dataToEncode
 *            The data to encode.
 * @return A text representation of a hexadecimal value of length 32.
 */
public static String messageDigestFive(final String dataToEncode) {
    MessageDigest m;
    try {
        m = MessageDigest.getInstance("MD5");
        byte[] data = dataToEncode.getBytes();
        m.update(data, 0, data.length);
        BigInteger i = new BigInteger(1, m.digest());
        return String.format("%1$032X", i);
    } catch (NoSuchAlgorithmException e) {
        // MD5 Should be supported by the runtime!
        throw new IllegalStateException(e);
    }
}