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.cmri.bpt.common.util.DigestUtil.java

private static byte[] digest(InputStream input, String algorithm) throws IOException {
    try {/*from w w w  .  j  av  a 2s. c  o  m*/
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        int bufferLength = 8 * 1024;
        byte[] buffer = new byte[bufferLength];
        int read = input.read(buffer, 0, bufferLength);

        while (read > -1) {
            messageDigest.update(buffer, 0, read);
            read = input.read(buffer, 0, bufferLength);
        }

        return messageDigest.digest();
    } catch (GeneralSecurityException e) {
        throw ExceptionUtil.asUnChecked(e);
    }
}

From source file:com.modoop.core.security.utils.Digests.java

private static byte[] digest(InputStream input, String algorithm) throws IOException {
    try {/*from  w  w w.j  a  v  a 2  s.  c  om*/
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        int bufferLength = 8 * 1024;
        byte[] buffer = new byte[bufferLength];
        int read = input.read(buffer, 0, bufferLength);

        while (read > -1) {
            messageDigest.update(buffer, 0, read);
            read = input.read(buffer, 0, bufferLength);
        }

        return messageDigest.digest();
    } catch (GeneralSecurityException e) {
        throw ExceptionUtils.unchecked(e);
    }
}

From source file:com.topsem.common.security.utils.Digests.java

private static byte[] digest(InputStream input, String algorithm) throws IOException {
    try {/* www.ja v a  2  s . com*/
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        int bufferLength = 8 * 1024;
        byte[] buffer = new byte[bufferLength];
        int read = input.read(buffer, 0, bufferLength);

        while (read > -1) {
            messageDigest.update(buffer, 0, read);
            read = input.read(buffer, 0, bufferLength);
        }

        return messageDigest.digest();
    } catch (GeneralSecurityException e) {
        throw Throwables.propagate(e);
    }
}

From source file:Main.java

public static String getFileMD5String(File file) {
    if (!file.exists() || file.isFile()) {
        return null;
    }// ww w. jav  a2s  .  com
    MessageDigest messagedigest;
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(file);
        messagedigest = MessageDigest.getInstance("MD5");
        int len;
        byte[] buf = new byte[1024];
        while ((len = fis.read(buf)) != -1) {
            messagedigest.update(buf, 0, len);
        }
        byte md5Bytes[] = messagedigest.digest();
        StringBuffer stringbuffer = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            stringbuffer.append(HEX_DIGITS[(md5Bytes[i] & 0xf0) >>> 4]);
            stringbuffer.append(HEX_DIGITS[md5Bytes[i] & 0x0f]);
        }
        return stringbuffer.toString();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:Main.java

public static String getBinaryHash(File apk, String algo) {
    FileInputStream fis = null;/*from  ww  w  .ja v  a2 s  .c  o m*/
    BufferedInputStream bis = null;
    try {
        MessageDigest md = MessageDigest.getInstance(algo);
        fis = new FileInputStream(apk);
        bis = new BufferedInputStream(fis);

        byte[] dataBytes = new byte[524288];
        int nread = 0;

        while ((nread = bis.read(dataBytes)) != -1)
            md.update(dataBytes, 0, nread);

        byte[] mdbytes = md.digest();
        return toHexString(mdbytes);
    } catch (IOException e) {
        Log.e("FDroid", "Error reading \"" + apk.getAbsolutePath() + "\" to compute " + algo + " hash.");
        return null;
    } catch (NoSuchAlgorithmException e) {
        Log.e("FDroid", "Device does not support " + algo + " MessageDisgest algorithm");
        return null;
    } finally {
        closeQuietly(fis);
    }
}

From source file:apm.common.security.Digests.java

private static byte[] digest(InputStream input, String algorithm) throws IOException {
    try {//from  w w w.  j a v a  2  s.  co  m
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        int bufferLength = 8 * 1024;
        byte[] buffer = new byte[bufferLength];
        int read = input.read(buffer, 0, bufferLength);

        while (read > -1) {
            messageDigest.update(buffer, 0, read);
            read = input.read(buffer, 0, bufferLength);
        }

        return messageDigest.digest();
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:com.apabi.qrcode.utils.DigestUtils.java

/**
 * , ?md5sha1.//from  w  w w . ja va 2s .c om
 */
private static String digest(final InputStream input, final String algorithm) throws IOException {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        int bufferLength = BUFFER_LENGTH;
        byte[] buffer = new byte[bufferLength];
        int read = input.read(buffer, 0, bufferLength);

        while (read > -1) {
            messageDigest.update(buffer, 0, read);
            read = input.read(buffer, 0, bufferLength);
        }

        return EncodeUtils.hexEncode(messageDigest.digest());

    } catch (GeneralSecurityException e) {
        throw new IllegalStateException("Security exception", e);
    }
}

From source file:Main.java

public static String hashStream(InputStream in, String hash) throws IOException {
    MessageDigest md5 = null;
    try {// w  ww .j  av a2s . c o  m
        md5 = MessageDigest.getInstance(hash);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    int bsize = 4096;
    byte[] buffer = new byte[bsize];
    int length;

    while ((length = in.read(buffer, 0, bsize)) > 0) {
        md5.update(buffer, 0, length);
    }

    return toHex(md5.digest());
}

From source file:com.myb.portal.util.Digests.java

private static byte[] digest(InputStream input, String algorithm) throws IOException {
    try {/* www  .  j a  v  a 2  s. c  o m*/
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        int bufferLength = 8 * 1024;
        byte[] buffer = new byte[bufferLength];
        int read = input.read(buffer, 0, bufferLength);

        while (read > -1) {
            messageDigest.update(buffer, 0, read);
            read = input.read(buffer, 0, bufferLength);
        }

        return messageDigest.digest();
    } catch (GeneralSecurityException e) {

        //         throw Exceptions.unchecked(e);
    }
    return null;
}

From source file:com.thinkgem.jeesite.modules.sso.util.Digests.java

private static byte[] digest(InputStream input, String algorithm) throws IOException {
    try {//  w  ww . ja v  a  2s. co m
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        int bufferLength = 8 * 1024;
        byte[] buffer = new byte[bufferLength];
        int read = input.read(buffer, 0, bufferLength);

        while (read > -1) {
            messageDigest.update(buffer, 0, read);
            read = input.read(buffer, 0, bufferLength);
        }

        return messageDigest.digest();
    } catch (GeneralSecurityException e) {
        return "".getBytes();
    }
}