Java 8 DigestOutputStream - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 DigestOutputStream class.

This code example shows how to use the following methods:getMessageDigest, on, write
    /** 
     * java.security.DigestOutputStream#on(boolean) 
     */ 
    public void test_onZ() throws Exception { 
        // Test for method void java.security.DigestOutputStream.on(boolean) 
        DigestOutputStream dos = new DigestOutputStream(
                new ByteArrayOutputStream(), MessageDigest.getInstance("SHA"));
        dos.on(false);
        byte digestArray[] = { 23, 43, 44 };
        dos.write(digestArray, 1, 1);
        byte digestResult[] = dos.getMessageDigest().digest();
        byte expected[] = { -38, 57, -93, -18, 94, 107, 75, 13, 50, 85,
                -65, -17, -107, 96, 24, -112, -81, -40, 7, 9 }; 
        assertTrue("Digest did not return expected result.",
                   Arrays.equals(digestResult, expected));
        // now turn on processing and re-run 
        dos.on(true);
        dos.write(digestArray, 1, 1);
        digestResult = dos.getMessageDigest().digest();
        byte expected1[] = { -87, 121, -17, 16, -52, 111, 106, 54, -33,

6
CodeRank
This code example shows how to use the following methods:getMessageDigest
            s.close();
        } 
    } 
 
    private static byte[] digest(InputStream in) throws NoSuchAlgorithmException, IOException {
        DigestOutputStream dos = new DigestOutputStream(new NullOutputStream(), MessageDigest.getInstance("MD5"));
        IOUtils.copy(in, dos);
        return dos.getMessageDigest().digest();
    } 
 
    private static byte[] getRandomSequence() { 
        byte[] buf = new byte[10*1024*1024];
        new Random(0).nextBytes(buf);
        return buf;
    } 
} 
6
CodeRank
This code example shows how to use the following methods:
      catch (NoSuchAlgorithmException e)
      { 
         throw new IllegalArgumentException(e);
      } 
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      DigestOutputStream digestStream = new DigestOutputStream(buffer, digest);
      OutputStream old = context.getOutputStream();
      context.setOutputStream(digestStream);
 
      try 
      { 
         context.proceed();
 
         byte[] hash = digest.digest();
         String encodedHash = Base64.encodeBytes(hash);
         context.getHeaders().putSingle("Content-MD5", encodedHash);
 
         byte[] content = buffer.toByteArray();
         old.write(content);
      } 
6
CodeRank
This code example shows how to use the following methods:flush
     * {@link DigestOutputStream} class, which allows to compute progressively 
     * the digest value. 
     */ 
    @Override 
    public void write(OutputStream outputStream) throws IOException {
        OutputStream dos = new DigestOutputStream(outputStream,
                this.computedDigest);
        getWrappedRepresentation().write(dos);
        dos.flush();
    } 
 
    @Override 
    public void write(WritableByteChannel writableChannel) throws IOException {
        OutputStream os = IoUtils.getStream(writableChannel);
        write(os);
        os.flush();
    } 
 
    @Override 
    public void write(Writer writer) throws IOException {
6
CodeRank
This code example shows how to use the following methods:getMessageDigest, write
        return formatDigestAsPaddedHex(digestStream, MD5_SUM_ZEROS);
    } 
 
    private DigestOutputStream createMd5DigestStream() {
        try { 
            return new DigestOutputStream(new NoopOutputStream(), MessageDigest.getInstance("MD5"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } 
    } 
 
    private void writeBufferedContent(final DigestOutputStream digestStream) throws IOException {
        byte[] content = byteBuffer.toByteArray();
        digestStream.write(content, 0, content.length);
    } 
 
    private String formatDigestAsPaddedHex(final DigestOutputStream digestStream, String padding) {
        byte[] digest = digestStream.getMessageDigest().digest();
        String digestHex = new BigInteger(1, digest).toString(16);
        String paddedDigestHex = padding.substring(0, padding.length() - digestHex.length()) + digestHex;

6
CodeRank
This code example shows how to use the following methods:getMessageDigest, on, write
    /** 
     * java.security.DigestOutputStream#on(boolean) 
     */ 
    public void test_onZ() throws Exception { 
        // Test for method void java.security.DigestOutputStream.on(boolean) 
        DigestOutputStream dos = new DigestOutputStream(
                new ByteArrayOutputStream(), MessageDigest.getInstance("SHA"));
        dos.on(false);
        byte digestArray[] = { 23, 43, 44 };
        dos.write(digestArray, 1, 1);
        byte digestResult[] = dos.getMessageDigest().digest();
        byte expected[] = { -38, 57, -93, -18, 94, 107, 75, 13, 50, 85,
                -65, -17, -107, 96, 24, -112, -81, -40, 7, 9 }; 
        assertTrue("Digest did not return expected result.",
                   Arrays.equals(digestResult, expected));
        // now turn on processing and re-run 
        dos.on(true);
        dos.write(digestArray, 1, 1);
        digestResult = dos.getMessageDigest().digest();
        byte expected1[] = { -87, 121, -17, 16, -52, 111, 106, 54, -33,

5
CodeRank
This code example shows how to use the following methods:close
        throws BlobStoreException 
    { 
        try 
        { 
            final MessageDigest digest = createMessageDigest();
            final DigestOutputStream digestOut = new DigestOutputStream( out, digest );
 
            try 
            { 
                ByteStreams.copy( in, digestOut );
            } 
            finally 
            { 
                digestOut.close();
                in.close();
            } 
 
            return new BlobKey( digest.digest() );
        } 
        catch ( IOException e )
5
CodeRank
This code example shows how to use the following methods:close
        throws BlobStoreException 
    { 
        try 
        { 
            final MessageDigest digest = createMessageDigest();
            final DigestOutputStream digestOut = new DigestOutputStream( out, digest );
 
            try 
            { 
                ByteStreams.copy( in, digestOut );
            } 
            finally 
            { 
                digestOut.close();
                in.close();
            } 
 
            return new BlobKey( digest.digest() );
        } 
        catch ( IOException e )
4
CodeRank
This code example shows how to use the following methods:write, getMessageDigest, on
	 * 
	 * @throws IOException 
	 *             an error occurred while writing to the output stream. 
	 */ 
	protected void writeChecksumFooter() throws IOException {
		out.write(packChecksum);
		out.on(false);
		out.write(out.getMessageDigest().digest());
	} 
} 
4
CodeRank
This code example shows how to use the following methods:close
    public void write(final WriteAction action) {
        File tmpFile = null;
        try { 
            tmpFile = File.createTempFile("config-", ".tmp", _configFile.getParentFile());
            final MessageDigest outputDigest = MessageDigest.getInstance("MD5");
            final OutputStream out = new DigestOutputStream(new FileOutputStream(tmpFile), outputDigest);
            try { 
                action.writeConfiguration(out);
            } finally { 
                out.close();
            } 
            if (_inputDigest == null || !_configFile.exists() || !MessageDigest.isEqual(_inputDigest, outputDigest.digest())) {
                Logger.debug("non equal.. write file '%s'", _configFile);
                tmpFile.renameTo(_configFile);
            } 
        } catch (final Exception e) {
            Logger.error("Could not write config. Error occured: ", e);
        } finally { 
            if (tmpFile != null) {
                tmpFile.delete();
4
CodeRank
See Code Examples for Java 8 DigestOutputStream Methods: