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:cc.arduino.utils.FileHash.java

/**
 * Calculate a message digest of a file using the algorithm specified. The
 * result is a string containing the algorithm name followed by ":" and by the
 * resulting hash in hex.//  w w w  .java  2 s.  co  m
 *
 * @param file
 * @param algorithm For example "SHA-256"
 * @return The algorithm followed by ":" and the hash, for example:<br />
 * "SHA-256:ee6796513086080cca078cbb383f543c5e508b647a71c9d6f39b7bca41071883"
 * @throws IOException
 * @throws NoSuchAlgorithmException
 */
public static String hash(File file, String algorithm) throws IOException, NoSuchAlgorithmException {
    FileInputStream in = null;
    try {
        in = new FileInputStream(file);
        byte buff[] = new byte[10240];
        MessageDigest digest = MessageDigest.getInstance(algorithm);
        while (in.available() > 0) {
            int read = in.read(buff);
            digest.update(buff, 0, read);
        }
        byte[] hash = digest.digest();
        String res = "";
        for (byte b : hash) {
            int c = b & 0xFF;
            if (c < 0x10)
                res += "0";
            res += Integer.toHexString(c);
        }
        return algorithm + ":" + res;
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:BitLottoVerify.java

public static String sha256(String str) {
    try {//from w  ww .ja va  2 s.c om
        byte[] buff = str.getBytes();

        MessageDigest sig = MessageDigest.getInstance("SHA-256");
        sig.update(buff, 0, buff.length);

        byte d[] = sig.digest();
        return printBytesInHex(d);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
        return null;
    }

}

From source file:com.networknt.light.util.HashUtil.java

public static String md5(String input) {

    String md5 = null;//  w  w w.  j a  v a 2 s  .c  o m

    if (null == input)
        return null;

    try {

        //Create MessageDigest object for MD5
        MessageDigest digest = MessageDigest.getInstance("MD5");

        //Update input string in message digest
        digest.update(input.getBytes(), 0, input.length());

        //Converts message digest value in base 16 (hex)
        md5 = new BigInteger(1, digest.digest()).toString(16);

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return md5;
}

From source file:MD5Sum.java

/**
 * Create a MD5 checksum for a file//from  ww w .  j a va2  s . c o m
 *
 * @param filename
 * @return md5sum
 * @throws Exception
 */
public static byte[] createChecksum(InputStream fis) throws NoSuchAlgorithmException, IOException {
    if (fis == null) {
        throw new FileNotFoundException("InputStream cannot be read");
    }

    byte[] buffer = new byte[1024];
    MessageDigest complete = MessageDigest.getInstance("MD5");
    int numRead;
    do {
        numRead = fis.read(buffer);
        if (numRead > 0) {
            complete.update(buffer, 0, numRead);
        }
    } while (numRead != -1);
    fis.close();
    return complete.digest();
}

From source file:Main.java

public static byte[] createChecksumBytes(String filename, String checksumType)
        throws java.io.IOException, java.security.NoSuchAlgorithmException {
    if (checksumType == null) { // MD5 by default
        checksumType = "MD5";
    }/*from w w  w.j  av  a  2  s. c  o  m*/
    java.io.InputStream fis = new java.io.FileInputStream(filename);

    byte[] buffer = new byte[1024];
    java.security.MessageDigest complete = java.security.MessageDigest.getInstance(checksumType);
    int numRead = 0;

    do {
        numRead = fis.read(buffer);
        if (numRead > 0) {
            complete.update(buffer, 0, numRead);
        }
    } while (numRead != -1);

    fis.close();
    return complete.digest();
}

From source file:Main.java

public static byte[] getFileDigest(File file, String algorithm) {
    InputStream fis = null;/*from   ww w.j  a  v a2 s . co m*/
    byte[] buffer = new byte[1024];
    int numRead;
    MessageDigest md;
    try {
        fis = new FileInputStream(file);
        md = MessageDigest.getInstance(algorithm);
        while ((numRead = fis.read(buffer)) > 0) {
            md.update(buffer, 0, numRead);
        }
        fis.close();
        return md.digest();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}

From source file:com.appbackr.android.tracker.Tracker.java

/**
  * This function performs md5 hashing. The reason for this function is because
  * android package does not have any default md5 hashing function.
  * @param s string encode as md5 hash//from   ww w  .  j a v  a2 s .c om
  * @return md5 hash string
  */
 private static String md5(String s) {
     try {
         // Create MD5 Hash
         MessageDigest digest = MessageDigest.getInstance("MD5");
         digest.update(s.getBytes(), 0, s.length());
         return new BigInteger(1, digest.digest()).toString(16);

     } catch (NoSuchAlgorithmException e) {
         e.printStackTrace();
     }

     return "";
 }

From source file:com.glaf.core.security.DigestUtil.java

public static void digestFile(String filename, String algorithm) {
    byte[] b = new byte[65536];
    int read = 0;
    FileInputStream fis = null;//from   www  .  java 2s. c o  m
    FileOutputStream fos = null;
    OutputStream encodedStream = null;
    try {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        fis = new FileInputStream(filename);
        while (fis.available() > 0) {
            read = fis.read(b);
            md.update(b, 0, read);
        }
        byte[] digest = md.digest();
        StringBuffer fileNameBuffer = new StringBuffer(256).append(filename).append('.').append(algorithm);
        fos = new FileOutputStream(fileNameBuffer.toString());
        encodedStream = MimeUtility.encode(fos, "base64");
        encodedStream.write(digest);
        fos.flush();
    } catch (Exception ex) {
        throw new RuntimeException("Error computing Digest: " + ex);
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(encodedStream);
    }
}

From source file:com.playonlinux.utils.Checksum.java

private static byte[] getDigest(InputStream inputStream, MessageDigest messageDigest) throws IOException {

    messageDigest.reset();//from  ww  w  .  java2  s.c  o  m
    byte[] bytes = new byte[BLOCK_SIZE];
    int numBytes;
    while ((numBytes = inputStream.read(bytes)) != -1) {
        messageDigest.update(bytes, 0, numBytes);
    }
    return messageDigest.digest();
}

From source file:com.cienet.util.EncryptDeciphering.java

/**
 * ?MD5?// w w w. j a va2  s .  c  o  m
 * 
 * @param inputStream
 * @return
 */
public static String streamToMD5(InputStream inputStream) {
    try {
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        byte[] buffer = new byte[1024];
        int numRead = 0;
        while ((numRead = inputStream.read(buffer)) > 0) {
            mdTemp.update(buffer, 0, numRead);
        }
        return toHexString(mdTemp.digest());
    } catch (Exception e) {
        return null;
    }
}