java.security.DigestInputStream

Here are the examples of the java api class java.security.DigestInputStream taken from open source projects.

1. StoredRevisionCache#doDownload()

Project: bnd
File: StoredRevisionCache.java
/*
	 * Download an URI into a temporary file while calculating SHA & MD5. The
	 * connection uses the normal protections
	 */
Download doDownload(URI url) throws Exception {
    InputStream connect = httpc.connect(url.toURL());
    if (connect == null)
        return null;
    Download d = new Download();
    d.tmp = IO.createTempFile(tmpdir, "tmp", ".tmp");
    MessageDigest sha = MessageDigest.getInstance("SHA1");
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    DigestInputStream shaIn = new DigestInputStream(connect, sha);
    DigestInputStream md5In = new DigestInputStream(shaIn, md5);
    IO.copy(md5In, d.tmp);
    d.sha = sha.digest();
    d.md5 = md5.digest();
    return d;
}

2. DocumentUploadUtil#combineToTempFile()

Project: zanata-server
File: DocumentUploadUtil.java
private File combineToTempFile(HDocumentUpload upload, DocumentFileUploadForm finalPart) throws SQLException {
    Vector<InputStream> partStreams = new Vector<InputStream>();
    for (HDocumentUploadPart part : upload.getParts()) {
        partStreams.add(part.getContent().getBinaryStream());
    }
    partStreams.add(finalPart.getFileStream());
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        log.error("MD5 algorithm not available.", e);
        throw new RuntimeException(e);
    }
    InputStream combinedParts = new SequenceInputStream(partStreams.elements());
    combinedParts = new DigestInputStream(combinedParts, md);
    File tempFile = translationFileServiceImpl.persistToTempFile(combinedParts);
    checkAndUpdateHash(finalPart, md, upload.getContentHash());
    return tempFile;
}

3. BaseSchemaResourceManager#digestInputStream()

Project: xmlbeans
File: BaseSchemaResourceManager.java
private static DigestInputStream digestInputStream(InputStream input) {
    MessageDigest sha;
    try {
        sha = MessageDigest.getInstance("SHA");
    } catch (NoSuchAlgorithmException e) {
        throw (IllegalStateException) (new IllegalStateException().initCause(e));
    }
    DigestInputStream str = new DigestInputStream(input, sha);
    return str;
}

4. CryptographyUtils#getMd5FromFile()

Project: UltimateAndroid
File: CryptographyUtils.java
/**
     * Get the MD5 of the file
     *
     * @param filePath
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
public static String getMd5FromFile(String filePath) throws IOException, NoSuchAlgorithmException {
    int bufferSize = 256 * 1024;
    FileInputStream fileInputStream = null;
    DigestInputStream digestInputStream = null;
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        fileInputStream = new FileInputStream(filePath);
        digestInputStream = new DigestInputStream(fileInputStream, messageDigest);
        byte[] buffer = new byte[bufferSize];
        while (digestInputStream.read(buffer) > 0) ;
        messageDigest = digestInputStream.getMessageDigest();
        byte[] resultByteArray = messageDigest.digest();
        return byteArrayToHex(resultByteArray);
    } catch (NoSuchAlgorithmException e) {
        throw e;
    } finally {
        if (digestInputStream != null) {
            digestInputStream.close();
        }
        if (fileInputStream != null) {
            fileInputStream.close();
        }
    }
}

5. UpdateHashUtils#computeHash()

Project: reacteu-app
File: UpdateHashUtils.java
private static String computeHash(InputStream dataStream) throws IOException, NoSuchAlgorithmException {
    MessageDigest messageDigest = null;
    DigestInputStream digestInputStream = null;
    try {
        messageDigest = MessageDigest.getInstance("SHA-256");
        digestInputStream = new DigestInputStream(dataStream, messageDigest);
        byte[] byteBuffer = new byte[1024 * 8];
        while (digestInputStream.read(byteBuffer) != -1) ;
    } finally {
        try {
            if (digestInputStream != null)
                digestInputStream.close();
            if (dataStream != null)
                dataStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    byte[] hash = messageDigest.digest();
    return String.format("%064x", new java.math.BigInteger(1, hash));
}

6. CipherStreamClose#streamDecrypt()

Project: openjdk
File: CipherStreamClose.java
public static Object streamDecrypt(byte[] data, SecretKey key, MessageDigest digest) throws Exception {
    Cipher decCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    decCipher.init(Cipher.DECRYPT_MODE, key);
    digest.reset();
    try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
        DigestInputStream dis = new DigestInputStream(bis, digest);
        CipherInputStream cis = new CipherInputStream(dis, decCipher)) {
        try (ObjectInputStream ois = new ObjectInputStream(cis)) {
            return ois.readObject();
        }
    }
}

7. SHA1Utils#computeSha1ForFile()

Project: mobile-android
File: SHA1Utils.java
public static String computeSha1ForFile(String filePath) throws IOException, NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA1");
    // reads the file path & file name As a argument
    FileInputStream fis = new FileInputStream(filePath);
    DigestInputStream dis = new DigestInputStream(fis, md);
    BufferedInputStream bis = new BufferedInputStream(dis);
    try {
        // Read the bis so SHA1 is auto calculated at dis
        while (true) {
            int b = bis.read();
            if (b == -1)
                break;
        }
    } finally {
        bis.close();
    }
    return byteArray2Hex(md.digest());
}

8. FileUtil#digest()

Project: jodd
File: FileUtil.java
// ---------------------------------------------------------------- digests
/**
	 * Calculates digest for a file using provided algorithm.
	 */
public static byte[] digest(final File file, MessageDigest algorithm) throws IOException {
    algorithm.reset();
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DigestInputStream dis = new DigestInputStream(bis, algorithm);
    try {
        while (dis.read() != -1) {
        }
    } finally {
        StreamUtil.close(fis);
    }
    return algorithm.digest();
}

9. PackedGrammar#computeVocabularyChecksum()

Project: incubator-joshua
File: PackedGrammar.java
/**
   * Computes the MD5 checksum of the vocabulary file.
   * Can be used for comparing vocabularies across multiple packedGrammars.
   * @return the computed checksum
   */
public String computeVocabularyChecksum() {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Unknown checksum algorithm");
    }
    byte[] buffer = new byte[1024];
    try (final InputStream is = Files.newInputStream(Paths.get(vocabFile.toString()));
        DigestInputStream dis = new DigestInputStream(is, md)) {
        while (dis.read(buffer) != -1) {
        }
    } catch (IOException e) {
        throw new RuntimeException("Can not find vocabulary file. This should not happen.");
    }
    byte[] digest = md.digest();
    // convert the byte to hex format
    StringBuffer sb = new StringBuffer("");
    for (int i = 0; i < digest.length; i++) {
        sb.append(Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1));
    }
    return sb.toString();
}

10. MD5#md5()

Project: ftpserver
File: MD5.java
/**
     * @param is
     *            InputStream for which the MD5 hash is calculated
     * @return The hash of the content in the input stream
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
private String md5(InputStream is) throws IOException, NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance("MD5");
    DigestInputStream dis = new DigestInputStream(is, digest);
    byte[] buffer = new byte[1024];
    int read = dis.read(buffer);
    while (read > -1) {
        read = dis.read(buffer);
    }
    return new String(encodeHex(dis.getMessageDigest().digest()));
}

11. SslCertificateUtils#digest()

Project: cattle
File: SslCertificateUtils.java
public static String digest(X509Certificate k) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance("SHA1");
    DigestInputStream in = new DigestInputStream(new ByteArrayInputStream(k.getEncoded()), md5);
    try {
        while (in.read(new byte[128]) > 0) {
        }
    } finally {
        in.close();
    }
    StringBuilder buf = new StringBuilder();
    char[] hex = Hex.encodeHex(md5.digest());
    for (int i = 0; i < hex.length; i += 2) {
        if (buf.length() > 0)
            buf.append(':');
        buf.append(hex, i, 2);
    }
    return buf.toString();
}

12. MorePaths#inputStreamDigest()

Project: buck
File: MorePaths.java
private static byte[] inputStreamDigest(InputStream inputStream, MessageDigest messageDigest) throws IOException {
    try (DigestInputStream dis = new DigestInputStream(inputStream, messageDigest)) {
        byte[] buf = new byte[4096];
        while (true) {
            // Read the contents of the existing file so we can hash it.
            if (dis.read(buf) == -1) {
                break;
            }
        }
        return dis.getMessageDigest().digest();
    }
}

13. ReposTemplateLoaderTest#testExtendUnprocessedPatternAndIgnore()

Project: bndtools
File: ReposTemplateLoaderTest.java
public void testExtendUnprocessedPatternAndIgnore() throws Exception {
    List<Template> templates = loader.findTemplates("test4", new ProgressMonitorReporter(new NullProgressMonitor(), "")).getValue();
    assertEquals(1, templates.size());
    Template template = templates.get(0);
    Map<String, List<Object>> parameters = new HashMap<>();
    parameters.put("projectName", Collections.<Object>singletonList("org.example.foo"));
    ResourceMap outputs = template.generateOutputs(parameters);
    assertEquals(1, outputs.size());
    Entry<String, Resource> entry;
    Iterator<Entry<String, Resource>> iter = outputs.entries().iterator();
    entry = iter.next();
    assertEquals("pic.xxx", entry.getKey());
    // Check the digest of the pic to ensure it didn't get damaged by the templating engine
    DigestInputStream digestStream = new DigestInputStream(entry.getValue().getContent(), MessageDigest.getInstance("SHA-256"));
    IO.drain(digestStream);
    byte[] digest = digestStream.getMessageDigest().digest();
    assertEquals("ea5d770bc2deddb1f9a20df3ad337bdc1490ba7b35fa41c33aa4e9a534e82ada", Hex.toHexString(digest).toLowerCase());
}

14. Md5Hasher#md5Hash()

Project: azkaban
File: Md5Hasher.java
public static byte[] md5Hash(File file) throws IOException {
    MessageDigest digest = getMd5Digest();
    FileInputStream fStream = new FileInputStream(file);
    BufferedInputStream bStream = new BufferedInputStream(fStream);
    DigestInputStream blobStream = new DigestInputStream(bStream, digest);
    byte[] buffer = new byte[BYTE_BUFFER_SIZE];
    int num = 0;
    do {
        num = blobStream.read(buffer);
    } while (num > 0);
    bStream.close();
    return digest.digest();
}

15. BaseSchemaResourceManager#shaDigestForFile()

Project: xmlbeans
File: BaseSchemaResourceManager.java
private String shaDigestForFile(String filename) throws IOException {
    DigestInputStream str = digestInputStream(inputStreamForFile(filename));
    byte[] dummy = new byte[4096];
    for (int i = 1; i > 0; i = str.read(dummy)) ;
    str.close();
    return HexBin.bytesToString(str.getMessageDigest().digest());
}

16. SchemaTypeLoaderBase#parse()

Project: xmlbeans
File: SchemaTypeLoaderBase.java
public XmlObject parse(InputStream jiois, SchemaType type, XmlOptions options) throws XmlException, IOException {
    XmlFactoryHook hook = XmlFactoryHook.ThreadContext.getHook();
    DigestInputStream digestStream = null;
    setupDigest: if (options != null && options.hasOption(XmlOptions.LOAD_MESSAGE_DIGEST)) {
        MessageDigest sha;
        try {
            sha = MessageDigest.getInstance("SHA");
        } catch (NoSuchAlgorithmException e) {
            break setupDigest;
        }
        digestStream = new DigestInputStream(jiois, sha);
        jiois = digestStream;
    }
    if (hook != null)
        return hook.parse(this, jiois, type, options);
    XmlObject result = createNewStore(null, options).loadXml(jiois, type, options);
    if (digestStream != null)
        result.documentProperties().setMessageDigest(digestStream.getMessageDigest().digest());
    return result;
}

17. MD5Utils#fileMD5()

Project: UltimateAndroid
File: MD5Utils.java
public static String fileMD5(String inputFile) throws IOException {
    // ?????????????????
    int bufferSize = 256 * 1024;
    FileInputStream fileInputStream = null;
    DigestInputStream digestInputStream = null;
    try {
        // ????MD5?????????????SHA1?
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        // ??DigestInputStream
        fileInputStream = new FileInputStream(inputFile);
        //  Base64InputStream base64InputStream=new Base64InputStream(fileInputStream,0);
        digestInputStream = new DigestInputStream(fileInputStream, messageDigest);
        // read??????MD5?????????
        byte[] buffer = new byte[bufferSize];
        while (digestInputStream.read(buffer) > 0) ;
        // ?????MessageDigest
        messageDigest = digestInputStream.getMessageDigest();
        // ??????????????16???
        byte[] resultByteArray = messageDigest.digest();
        // ??????????????
        return byteArrayToHex(resultByteArray);
    } catch (NoSuchAlgorithmException e) {
        return null;
    } finally {
        try {
            digestInputStream.close();
        } catch (Exception e) {
        }
        try {
            fileInputStream.close();
        } catch (Exception e) {
        }
    }
}

18. RpmRepositoryWriter#loadCompsFile()

Project: spacewalk
File: RpmRepositoryWriter.java
/**
     *
     * @param channel channel indo
     * @param checksumAlgo checksum algorithm
     * @return repomd index for given channel
     */
private RepomdIndexData loadCompsFile(Channel channel, String checksumAlgo) {
    String compsMount = Config.get().getString(ConfigDefaults.MOUNT_POINT);
    String relativeFilename = getCompsRelativeFilename(channel);
    if (relativeFilename == null) {
        return null;
    }
    File compsFile = new File(compsMount + File.separator + relativeFilename);
    FileInputStream stream;
    try {
        stream = new FileInputStream(compsFile);
    } catch (FileNotFoundException e) {
        return null;
    }
    DigestInputStream digestStream;
    try {
        digestStream = new DigestInputStream(stream, MessageDigest.getInstance(checksumAlgo));
    } catch (NoSuchAlgorithmException nsae) {
        throw new RepomdRuntimeException(nsae);
    }
    byte[] bytes = new byte[10];
    try {
        while (digestStream.read(bytes) != -1) {
        // no-op
        }
    } catch (IOException e) {
        return null;
    }
    Date timeStamp = new Date(compsFile.lastModified());
    return new RepomdIndexData(StringUtil.getHexString(digestStream.getMessageDigest().digest()), null, timeStamp);
}

19. TestDigestIOStream#testMDShare()

Project: openjdk
File: TestDigestIOStream.java
/**
     * Test DigestInputStream and DigestOutputStream digest function when use
     * same message digest object.
     *
     * @param algo
     *            Message Digest algorithm
     * @param dataLength
     *            plain test data length.
     * @exception Exception
     *                throw unexpected exception
     */
public boolean testMDShare(String algo, int dataLength) throws Exception {
    MessageDigest mdCommon = MessageDigest.getInstance(algo);
    // Generate the DigestInputStream/DigestOutputStream object
    try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DigestInputStream dis = new DigestInputStream(bais, mdCommon);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(baos, mdCommon)) {
        // Perform the update using all available/possible update methods
        int k = 0;
        byte[] buffer = new byte[10];
        // use both read() and read(byte[], int, int)
        while (k < data.length) {
            int len = dis.read(buffer, 0, buffer.length);
            if (len != -1) {
                k += len;
                if (k < data.length) {
                    dos.write(data[k]);
                    k++;
                    dis.skip(1);
                }
            }
        }
        // Get the output and the "correct" digest values
        byte[] output = mdCommon.digest();
        byte[] standard = md.digest(data);
        // Compare generated digest values
        return MessageDigest.isEqual(output, standard);
    } catch (Exception ex) {
        out.println("TestMDShare failed at:" + algo + "/" + dataLength + " with unexpected exception");
        throw ex;
    }
}

20. TestDigestIOStream#testMDChange()

Project: openjdk
File: TestDigestIOStream.java
/**
     * Test DigestInputStream and DigestOutputStream digest function when Swap
     * the message digest engines between DigestIn/OutputStream
     *
     * @param algo
     *            Message Digest algorithm
     * @param dataLength
     *            plain test data length.
     * @exception Exception
     *                throw unexpected exception
     */
public boolean testMDChange(String algo, int dataLength) throws Exception {
    // Generate the DigestInputStream/DigestOutputStream object
    MessageDigest mdIn = MessageDigest.getInstance(algo);
    MessageDigest mdOut = MessageDigest.getInstance(algo);
    try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DigestInputStream dis = new DigestInputStream(bais, mdIn);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(baos, mdOut)) {
        // Perform the update using all available/possible update methods
        int k = 0;
        byte[] buffer = new byte[10];
        // use both read() and read(byte[], int, int)
        while ((k = dis.read()) != -1) {
            dos.write(k);
            if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
                dos.write(buffer, 0, k);
            }
            // Swap the message digest engines between
            // DigestIn/OutputStream objects
            dis.setMessageDigest(mdOut);
            dos.setMessageDigest(mdIn);
            mdIn = dis.getMessageDigest();
            mdOut = dos.getMessageDigest();
        }
        // Get the output and the "correct" digest values
        byte[] output1 = mdIn.digest();
        byte[] output2 = mdOut.digest();
        byte[] standard = md.digest(data);
        // Compare generated digest values
        return MessageDigest.isEqual(output1, standard) && MessageDigest.isEqual(output2, standard);
    } catch (Exception ex) {
        out.println("testMDChange failed at:" + algo + "/" + dataLength + " with unexpected exception");
        throw ex;
    }
}

21. TestDigestIOStream#testDigestOnOff()

Project: openjdk
File: TestDigestIOStream.java
/**
     * Test DigestInputStream and DigestOutputStream digest function when digest
     * set on and off
     *
     * @param algo
     *            Message Digest algorithm
     * @param readModel
     *            which read method used(READ, BUFFER_READ, MIX_READ)
     * @param on
     *            digest switch(on and off)
     * @param dataLength
     *            plain test data length.
     * @exception Exception
     *                throw unexpected exception
     */
public boolean testDigestOnOff(String algo, ReadModel readModel, boolean on, int dataLength) throws Exception {
    // Generate the DigestInputStream/DigestOutputStream object
    try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DigestInputStream dis = new DigestInputStream(bais, MessageDigest.getInstance(algo));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(baos, MessageDigest.getInstance(algo));
        ByteArrayOutputStream baOut = new ByteArrayOutputStream()) {
        // Perform the update using all available/possible update methods
        int k = 0;
        byte[] buffer = new byte[5];
        boolean enDigest = true;
        // Make sure the digest function is on (default)
        dis.on(enDigest);
        dos.on(enDigest);
        switch(readModel) {
            case // use only read()
            READ:
                while ((k = dis.read()) != -1) {
                    if (on) {
                        dos.write(k);
                    } else {
                        dos.write(k);
                        if (enDigest) {
                            baOut.write(k);
                        }
                        enDigest = !enDigest;
                        dos.on(enDigest);
                        dis.on(enDigest);
                    }
                }
                break;
            case // use only read(byte[], int, int)
            BUFFER_READ:
                while ((k = dis.read(buffer, 0, buffer.length)) != -1) {
                    if (on) {
                        dos.write(buffer, 0, k);
                    } else {
                        dos.write(buffer, 0, k);
                        if (enDigest) {
                            baOut.write(buffer, 0, k);
                        }
                        enDigest = !enDigest;
                        dis.on(enDigest);
                        dos.on(enDigest);
                    }
                }
                break;
            case // use both read() and read(byte[], int, int)
            MIX_READ:
                while ((k = dis.read()) != -1) {
                    if (on) {
                        dos.write(k);
                        if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
                            dos.write(buffer, 0, k);
                        }
                    } else {
                        dos.write(k);
                        if (enDigest) {
                            baOut.write(k);
                        }
                        enDigest = !enDigest;
                        dis.on(enDigest);
                        dos.on(enDigest);
                        if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
                            dos.write(buffer, 0, k);
                            if (enDigest) {
                                baOut.write(buffer, 0, k);
                            }
                            enDigest = !enDigest;
                            dis.on(enDigest);
                            dos.on(enDigest);
                        }
                    }
                }
                break;
            default:
                out.println("ERROR: Invalid read/write combination choice!");
                return false;
        }
        // Get the output and the "correct" digest values
        byte[] output1 = dis.getMessageDigest().digest();
        byte[] output2 = dos.getMessageDigest().digest();
        byte[] standard;
        if (on) {
            standard = md.digest(data);
        } else {
            byte[] dataDigested = baOut.toByteArray();
            standard = md.digest(dataDigested);
        }
        // Compare the output byte array value to the input data
        if (!MessageDigest.isEqual(data, baos.toByteArray())) {
            out.println("ERROR of " + readModel + ": output and input data unexpectedly changed");
            return false;
        }
        // Compare generated digest values
        if (!MessageDigest.isEqual(output1, standard) || !MessageDigest.isEqual(output2, standard)) {
            out.println("ERROR" + readModel + ": generated digest data unexpectedly changed");
            return false;
        }
        return true;
    } catch (Exception ex) {
        out.println("testDigestOnOff failed at:" + algo + "/" + readModel + "/" + dataLength + " with unexpected exception");
        throw ex;
    }
}

22. FileBlobStore#writeBlob()

Project: jackrabbit-oak
File: FileBlobStore.java
@Override
public String writeBlob(String tempFilePath) throws IOException {
    File file = new File(tempFilePath);
    InputStream in = new FileInputStream(file);
    MessageDigest messageDigest;
    try {
        messageDigest = MessageDigest.getInstance(HASH_ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        throw new IOException(e);
    }
    DigestInputStream din = new DigestInputStream(in, messageDigest);
    long length = file.length();
    try {
        while (true) {
            int len = din.read(buffer, 0, buffer.length);
            if (len < 0) {
                break;
            }
        }
    } finally {
        din.close();
    }
    ByteArrayOutputStream idStream = new ByteArrayOutputStream();
    idStream.write(TYPE_HASH);
    IOUtils.writeVarInt(idStream, 0);
    IOUtils.writeVarLong(idStream, length);
    byte[] digest = messageDigest.digest();
    File f = getFile(digest, false);
    if (f.exists()) {
        file.delete();
    } else {
        File parent = f.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        file.renameTo(f);
    }
    IOUtils.writeVarInt(idStream, digest.length);
    idStream.write(digest);
    byte[] id = idStream.toByteArray();
    String blobId = StringUtils.convertBytesToHex(id);
    usesBlobId(blobId);
    return blobId;
}

23. HashUtil#hash()

Project: Hive2Hive
File: HashUtil.java
/**
	 * Generates a MD5 hash of an input stream (can take a while)
	 *
	 * @param file
	 * @return the hash of the file
	 * @throws IOException
	 */
public static byte[] hash(File file) throws IOException {
    if (file == null) {
        return new byte[0];
    } else if (file.isDirectory()) {
        return new byte[0];
    } else if (!file.exists()) {
        return new byte[0];
    }
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance(HASH_ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        logger.error("Invalid hash algorithm {}", HASH_ALGORITHM, e);
        return new byte[0];
    }
    FileInputStream fis;
    try {
        // open the stream
        fis = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        logger.error("File {} not found to generate the hash", file, e);
        return new byte[0];
    }
    DigestInputStream dis = new DigestInputStream(fis, digest);
    try {
        byte[] buffer = new byte[1024];
        int numRead;
        do {
            numRead = dis.read(buffer);
        } while (numRead != -1);
    } finally {
        if (dis != null) {
            dis.close();
        }
        if (fis != null) {
            fis.close();
        }
    }
    return digest.digest();
}

24. IoUtil#countFileHash()

Project: che
File: IoUtil.java
public static String countFileHash(File file, MessageDigest digest) throws IOException {
    byte[] b = new byte[8192];
    try (DigestInputStream dis = new DigestInputStream(new FileInputStream(file), digest)) {
        while (dis.read(b) != -1) ;
        return toHex(digest.digest());
    }
}

25. CCNVersionedOutputStreamTest#readFile()

Project: ccnx
File: CCNVersionedOutputStreamTest.java
public static byte[] readFile(InputStream inputStream) throws IOException {
    DigestInputStream dis = null;
    try {
        dis = new DigestInputStream(inputStream, MessageDigest.getInstance("SHA1"));
    } catch (NoSuchAlgorithmException e) {
        Log.severe("No SHA1 available!");
        Assert.fail("No SHA1 available!");
    }
    int elapsed = 0;
    int read = 0;
    byte[] bytes = new byte[BUF_SIZE];
    while (true) {
        read = dis.read(bytes);
        if (read < 0) {
            System.out.println("EOF read at " + elapsed + " bytes.");
            break;
        } else if (read == 0) {
            System.out.println("0 bytes read at " + elapsed + " bytes.");
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
            }
        }
        elapsed += read;
        System.out.println(" read " + elapsed + " bytes.");
    }
    return dis.getMessageDigest().digest();
}

26. CCNVersionedInputStreamTest#readFile()

Project: ccnx
File: CCNVersionedInputStreamTest.java
public static byte[] readFile(InputStream inputStream, int fileLength) throws IOException {
    DigestInputStream dis = null;
    try {
        dis = new DigestInputStream(inputStream, MessageDigest.getInstance("SHA1"));
    } catch (NoSuchAlgorithmException e) {
        Log.severe(Log.FAC_TEST, "No SHA1 available!");
        Assert.fail("No SHA1 available!");
    }
    int elapsed = 0;
    int read = 0;
    byte[] bytes = new byte[BUF_SIZE];
    while (elapsed < fileLength) {
        read = dis.read(bytes);
        if (read < 0) {
            Log.info(Log.FAC_TEST, "EOF read at " + elapsed + " bytes out of " + fileLength);
            break;
        } else if (read == 0) {
            Log.info(Log.FAC_TEST, "0 bytes read at " + elapsed + " bytes out of " + fileLength);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
            }
        }
        elapsed += read;
        Log.info(Log.FAC_TEST, " read " + elapsed + " bytes out of " + fileLength);
    }
    return dis.getMessageDigest().digest();
}

27. ReposTemplateLoaderTest#testAlternateDelimiters()

Project: bndtools
File: ReposTemplateLoaderTest.java
public void testAlternateDelimiters() throws Exception {
    List<Template> templates = loader.findTemplates("test2", new ProgressMonitorReporter(new NullProgressMonitor(), "")).getValue();
    assertEquals(1, templates.size());
    Template template = templates.get(0);
    Map<String, List<Object>> parameters = new HashMap<>();
    parameters.put("projectName", Collections.<Object>singletonList("org.example.foo"));
    parameters.put("srcDir", Collections.<Object>singletonList("src/main/java"));
    parameters.put("basePackageDir", Collections.<Object>singletonList("org/example/foo"));
    ResourceMap outputs = template.generateOutputs(parameters);
    assertEquals(5, outputs.size());
    Iterator<Entry<String, Resource>> iter = outputs.entries().iterator();
    Entry<String, Resource> entry;
    entry = iter.next();
    assertEquals("src/main/java/org/example/foo/Activator.java", entry.getKey());
    assertEquals("package org.example.foo; public class Activator {}", IO.collect(entry.getValue().getContent()));
    entry = iter.next();
    assertEquals("pic.jpg", entry.getKey());
    // Check the digest of the pic to ensure it didn't get damaged by the templating engine
    DigestInputStream digestStream = new DigestInputStream(entry.getValue().getContent(), MessageDigest.getInstance("SHA-256"));
    IO.drain(digestStream);
    byte[] digest = digestStream.getMessageDigest().digest();
    assertEquals("ea5d770bc2deddb1f9a20df3ad337bdc1490ba7b35fa41c33aa4e9a534e82ada", Hex.toHexString(digest).toLowerCase());
    entry = iter.next();
    assertEquals("src/main/java/", entry.getKey());
    entry = iter.next();
    assertEquals("src/main/java/org/example/foo/", entry.getKey());
    entry = iter.next();
    assertEquals("bnd.bnd", entry.getKey());
    assertEquals("Bundle-SymbolicName: org.example.foo", IO.collect(entry.getValue().getContent()));
}

28. ReposTemplateLoaderTest#testProcessTemplate()

Project: bndtools
File: ReposTemplateLoaderTest.java
public void testProcessTemplate() throws Exception {
    List<Template> templates = loader.findTemplates("test1", new ProgressMonitorReporter(new NullProgressMonitor(), "")).getValue();
    assertEquals(1, templates.size());
    Template template = templates.get(0);
    Map<String, List<Object>> parameters = new HashMap<>();
    parameters.put("projectName", Collections.<Object>singletonList("org.example.foo"));
    parameters.put("srcDir", Collections.<Object>singletonList("src/main/java"));
    parameters.put("basePackageDir", Collections.<Object>singletonList("org/example/foo"));
    ResourceMap outputs = template.generateOutputs(parameters);
    assertEquals(5, outputs.size());
    Entry<String, Resource> entry;
    Iterator<Entry<String, Resource>> iter = outputs.entries().iterator();
    entry = iter.next();
    assertEquals("src/main/java/org/example/foo/Activator.java", entry.getKey());
    assertEquals("package org.example.foo; public class Activator {}", IO.collect(entry.getValue().getContent()));
    entry = iter.next();
    assertEquals("pic.jpg", entry.getKey());
    // Check the digest of the pic to ensure it didn't get damaged by the templating engine
    DigestInputStream digestStream = new DigestInputStream(entry.getValue().getContent(), MessageDigest.getInstance("SHA-256"));
    IO.drain(digestStream);
    byte[] digest = digestStream.getMessageDigest().digest();
    assertEquals("ea5d770bc2deddb1f9a20df3ad337bdc1490ba7b35fa41c33aa4e9a534e82ada", Hex.toHexString(digest).toLowerCase());
    entry = iter.next();
    assertEquals("src/main/java/", entry.getKey());
    entry = iter.next();
    assertEquals("src/main/java/org/example/foo/", entry.getKey());
    entry = iter.next();
    assertEquals("bnd.bnd", entry.getKey());
    assertEquals("Bundle-SymbolicName: org.example.foo", IO.collect(entry.getValue().getContent()));
}

29. AetherRepository#put()

Project: bnd
File: AetherRepository.java
@Override
public PutResult put(InputStream stream, PutOptions options) throws Exception {
    init();
    DigestInputStream digestStream = new DigestInputStream(stream, MessageDigest.getInstance("SHA-1"));
    final File tmpFile = IO.createTempFile(cacheDir, "put", ".bnd");
    try {
        IO.copy(digestStream, tmpFile);
        byte[] digest = digestStream.getMessageDigest().digest();
        if (options.digest != null && !Arrays.equals(options.digest, digest))
            throw new IOException("Retrieved artifact digest doesn't match specified digest");
        // Get basic info about the bundle we're deploying
        Jar jar = new Jar(tmpFile);
        Artifact artifact = ConversionUtils.fromBundleJar(jar);
        artifact = artifact.setFile(tmpFile);
        // Setup the Aether repo session and create the deployment request
        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
        session.setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(session, localRepo));
        final DeployRequest request = new DeployRequest();
        request.addArtifact(artifact);
        request.setRepository(remoteRepo);
        // Capture the result including remote resource URI
        final ResultHolder resultHolder = new ResultHolder();
        session.setTransferListener(new AbstractTransferListener() {

            @Override
            public void transferSucceeded(TransferEvent event) {
                TransferResource resource = event.getResource();
                if (event.getRequestType() == RequestType.PUT && tmpFile.equals(resource.getFile())) {
                    PutResult result = new PutResult();
                    result.artifact = URI.create(resource.getRepositoryUrl() + resource.getResourceName());
                    resultHolder.result = result;
                    System.out.println("UPLOADED to: " + URI.create(resource.getRepositoryUrl() + resource.getResourceName()));
                }
            }

            @Override
            public void transferFailed(TransferEvent event) {
                if (event.getRequestType() == RequestType.PUT && tmpFile.equals(event.getResource().getFile()))
                    resultHolder.error = event.getException();
            }

            @Override
            public void transferCorrupted(TransferEvent event) throws TransferCancelledException {
                if (event.getRequestType() == RequestType.PUT && tmpFile.equals(event.getResource().getFile()))
                    resultHolder.error = event.getException();
            }
        });
        // Do the deploy and report results
        repoSystem.deploy(session, request);
        if (resultHolder.result != null) {
            if (indexedRepo != null)
                indexedRepo.reset();
            return resultHolder.result;
        } else if (resultHolder.error != null) {
            throw new Exception("Error during artifact upload", resultHolder.error);
        } else {
            throw new Exception("Artifact was not uploaded");
        }
    } finally {
        if (tmpFile != null && tmpFile.isFile())
            IO.delete(tmpFile);
    }
}

30. LocalIndexedRepo#put()

Project: bnd
File: LocalIndexedRepo.java
/* NOTE: this is a straight copy of FileRepo.put */
@Override
public synchronized PutResult put(InputStream stream, PutOptions options) throws Exception {
    /* determine if the put is allowed */
    if (readOnly) {
        throw new IOException("Repository is read-only");
    }
    if (options == null)
        options = DEFAULTOPTIONS;
    /* both parameters are required */
    if (stream == null)
        throw new IllegalArgumentException("No stream and/or options specified");
    /* the root directory of the repository has to be a directory */
    if (!storageDir.isDirectory()) {
        throw new IOException("Repository directory " + storageDir + " is not a directory");
    }
    /*
		 * setup a new stream that encapsulates the stream and calculates (when
		 * needed) the digest
		 */
    DigestInputStream dis = new DigestInputStream(stream, MessageDigest.getInstance("SHA-1"));
    File tmpFile = null;
    try {
        /*
			 * copy the artifact from the (new/digest) stream into a temporary
			 * file in the root directory of the repository
			 */
        tmpFile = IO.createTempFile(storageDir, "put", ".bnd");
        IO.copy(dis, tmpFile);
        /* beforeGet the digest if available */
        byte[] disDigest = dis.getMessageDigest().digest();
        if (options.digest != null && !Arrays.equals(options.digest, disDigest))
            throw new IOException("Retrieved artifact digest doesn't match specified digest");
        /* put the artifact into the repository (from the temporary file) */
        File file = putArtifact(tmpFile);
        PutResult result = new PutResult();
        if (file != null) {
            result.digest = disDigest;
            result.artifact = file.toURI();
        }
        return result;
    } finally {
        if (tmpFile != null && tmpFile.exists()) {
            IO.delete(tmpFile);
        }
    }
}

31. NexusOBR#put()

Project: bnd
File: NexusOBR.java
@Override
public synchronized PutResult put(InputStream stream, PutOptions options) throws Exception {
    /* determine if the put is allowed */
    if (readOnly) {
        throw new IOException("Repository is read-only");
    }
    if (options == null)
        options = DEFAULTOPTIONS;
    /* both parameters are required */
    if (stream == null)
        throw new IllegalArgumentException("No stream and/or options specified");
    /*
		 * setup a new stream that encapsulates the stream and calculates (when
		 * needed) the digest
		 */
    DigestInputStream dis = new DigestInputStream(stream, MessageDigest.getInstance("SHA-1"));
    File tmpFile = null;
    try {
        /*
			 * copy the artifact from the (new/digest) stream into a temporary
			 * file in the root directory of the repository
			 */
        tmpFile = IO.createTempFile(null, "put", ".bnd");
        IO.copy(dis, tmpFile);
        /* beforeGet the digest if available */
        byte[] disDigest = dis.getMessageDigest().digest();
        if (options.digest != null && !Arrays.equals(options.digest, disDigest))
            throw new IOException("Retrieved artifact digest doesn't match specified digest");
        /* put the artifact into the repository (from the temporary file) */
        URL url = putArtifact(tmpFile);
        PutResult result = new PutResult();
        if (url != null) {
            result.digest = disDigest;
            result.artifact = url.toURI();
        }
        return result;
    } finally {
        if (tmpFile != null && tmpFile.exists()) {
            IO.delete(tmpFile);
        }
    }
}

32. Macro#_digest()

Project: bnd
File: Macro.java
public String _digest(String... args) throws NoSuchAlgorithmException, IOException {
    verifyCommand(args, "${digest;<algo>;<in>}, get a digest (e.g. MD5, SHA-256) of a file", null, 3, 3);
    MessageDigest digester = MessageDigest.getInstance(args[1]);
    File f = domain.getFile(args[2]);
    DigestInputStream in = new DigestInputStream(new FileInputStream(f), digester);
    IO.drain(in);
    byte[] digest = digester.digest();
    return Hex.toHexString(digest);
}

33. CAFS#write()

Project: bnd
File: CAFS.java
/**
	 * Store an input stream in the CAFS while calculating and returning the
	 * SHA-1 code.
	 * 
	 * @param in The input stream to store.
	 * @return The SHA-1 code.
	 * @throws Exception if anything goes wrong
	 */
public SHA1 write(InputStream in) throws Exception {
    Deflater deflater = new Deflater();
    MessageDigest md = MessageDigest.getInstance(ALGORITHM);
    DigestInputStream din = new DigestInputStream(in, md);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DeflaterOutputStream dout = new DeflaterOutputStream(bout, deflater);
    copy(din, dout);
    synchronized (store) {
        // First check if it already exists
        SHA1 sha1 = new SHA1(md.digest());
        long search = index.search(sha1.digest());
        if (search > 0)
            return sha1;
        byte[] compressed = bout.toByteArray();
        // we need to append this file to our store,
        // which requires a lock. However, we are in a race
        // so others can get the lock between us getting
        // the length and someone else getting the lock.
        // So we must verify after we get the lock that the
        // length was unchanged.
        FileLock lock = null;
        try {
            long insertPoint;
            int recordLength = compressed.length + HEADERLENGTH;
            while (true) {
                insertPoint = store.length();
                lock = channel.lock(insertPoint, recordLength, false);
                if (store.length() == insertPoint)
                    break;
                // We got the wrong lock, someone else
                // got in between reading the length
                // and locking
                lock.release();
            }
            int totalLength = deflater.getTotalIn();
            store.seek(insertPoint);
            update(sha1.digest(), compressed, totalLength);
            index.insert(sha1.digest(), insertPoint);
            return sha1;
        } finally {
            if (lock != null)
                lock.release();
        }
    }
}

34. AbstractPersistentStreamStore#storeBlocksAndGetFinalMetadata()

Project: atlasdb
File: AbstractPersistentStreamStore.java
protected final StreamMetadata storeBlocksAndGetFinalMetadata(@Nullable Transaction t, long id, InputStream stream) {
    // Set up for finding hash and length
    MessageDigest digest = Sha256Hash.getMessageDigest();
    stream = new DigestInputStream(stream, digest);
    CountingInputStream countingStream = new CountingInputStream(stream);
    // Try to store the bytes to the stream and get length
    try {
        storeBlocksFromStream(t, id, countingStream);
    } catch (IOException e) {
        long length = countingStream.getCount();
        StreamMetadata metadata = StreamMetadata.newBuilder().setStatus(Status.FAILED).setLength(length).setHash(com.google.protobuf.ByteString.EMPTY).build();
        storeMetadataAndIndex(id, metadata);
        log.error("Could not store stream " + id + ". Failed after " + length + " bytes.", e);
        throw Throwables.rewrapAndThrowUncheckedException("Failed to store stream.", e);
    }
    // Get hash and length
    ByteString hashByteString = ByteString.copyFrom(digest.digest());
    long length = countingStream.getCount();
    // Return the final metadata.
    StreamMetadata metadata = StreamMetadata.newBuilder().setStatus(Status.STORED).setLength(length).setHash(hashByteString).build();
    return metadata;
}

35. AbstractExpiringStreamStore#storeBlocksAndGetFinalMetadata()

Project: atlasdb
File: AbstractExpiringStreamStore.java
protected final StreamMetadata storeBlocksAndGetFinalMetadata(ID id, InputStream stream, long duration, TimeUnit durationUnit) {
    // Set up for finding hash and length
    MessageDigest digest = Sha256Hash.getMessageDigest();
    stream = new DigestInputStream(stream, digest);
    CountingInputStream countingStream = new CountingInputStream(stream);
    // Try to store the bytes to the stream and get length
    try {
        storeBlocksFromStream(id, countingStream, duration, durationUnit);
    } catch (IOException e) {
        long length = countingStream.getCount();
        StreamMetadata metadata = StreamMetadata.newBuilder().setStatus(Status.FAILED).setLength(length).setHash(com.google.protobuf.ByteString.EMPTY).build();
        storeMetadataAndIndex(id, metadata, duration, durationUnit);
        log.error("Could not store stream " + id + ". Failed after " + length + " bytes.", e);
        throw Throwables.rewrapAndThrowUncheckedException("Failed to store stream.", e);
    }
    // Get hash and length
    ByteString hashByteString = ByteString.copyFrom(digest.digest());
    long length = countingStream.getCount();
    // Return the final metadata.
    StreamMetadata metadata = StreamMetadata.newBuilder().setStatus(Status.STORED).setLength(length).setHash(hashByteString).build();
    return metadata;
}

36. Checksum#update()

Project: archiva
File: Checksum.java
public Checksum update(InputStream stream) throws IOException {
    try (DigestInputStream dig = new DigestInputStream(stream, md)) {
        IOUtils.copy(dig, new NullOutputStream());
    }
    return this;
}