Package java.security

Examples of java.security.DigestInputStream


    @Override
    public String addBlob(String tempFilePath) throws Exception {
        File file = new File(tempFilePath);
        InputStream in = new FileInputStream(file);
        MessageDigest messageDigest = MessageDigest.getInstance(HASH_ALGORITHM);
        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);
View Full Code Here


                log.error(msg);
                throw new DataStoreException(msg);
            }
            temporaryInUse.add(tempId);
            MessageDigest digest = getDigest();
            DigestInputStream dIn = new DigestInputStream(stream, digest);
            TrackingInputStream in = new TrackingInputStream(dIn);
            StreamWrapper wrapper;
            if (STORE_SIZE_MINUS_ONE.equals(storeStream)) {
                wrapper = new StreamWrapper(in, -1);
            } else if (STORE_SIZE_MAX.equals(storeStream)) {
View Full Code Here

  /**
   * Calculate the MD5 hash of the newly-merged fsimage.
   * @return the checksum of the newly-merged fsimage.
   */
  MD5Hash getNewChecksum() throws IOException {
    DigestInputStream imageIn = null;
    try {
      MessageDigest digester = MD5Hash.getDigester();
      imageIn = new DigestInputStream(
          new FileInputStream(checkpointImage.getFsImageName()), digester);
      byte[] in = new byte[BUFFER_SIZE];
      int totalRead = 0;
      int read = 0;
      while ((read = imageIn.read(in)) > 0) {
        totalRead += read;
        LOG.debug("Computing fsimage checksum. Read " + totalRead + " bytes so far.");
      }
      return new MD5Hash(digester.digest());
    } finally {
      if (imageIn != null) {
        imageIn.close();
      }
    }
  }
View Full Code Here

    }

    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;
    }
View Full Code Here

    public final void testDigestInputStream01()  {
        for (int i=0; i<algorithmName.length; i++) {
            try {
                MessageDigest md = MessageDigest.getInstance(algorithmName[i]);
                InputStream is = new ByteArrayInputStream(myMessage);
                InputStream dis = new DigestInputStream(is, md);
                assertTrue(dis instanceof DigestInputStream);
                return;
            } catch (NoSuchAlgorithmException e) {
                // allowed failure
            }
View Full Code Here

     *
     * Assertion: creates new <code>DigestInputStream</code> instance
     * using valid parameters (both <code>null</code>)
     */
    public final void testDigestInputStream02() {
        InputStream dis = new DigestInputStream(null, null);
        assertTrue(dis instanceof DigestInputStream);
    }
View Full Code Here

        throws IOException {
        for (int ii=0; ii<algorithmName.length; ii++) {
            try {
                MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
                InputStream is = new ByteArrayInputStream(myMessage);
                DigestInputStream dis = new DigestInputStream(is, md);
                for (int i=0; i<MY_MESSAGE_LEN; i++) {
                    // check that read() returns valid values
                    assertTrue("retval", ((byte)dis.read() == myMessage[i]));
                }
                // check that associated digest has been updated properly
                assertTrue("update",
                        Arrays.equals(
                                dis.getMessageDigest().digest(),
                                MDGoldenData.getDigest(algorithmName[ii])));
                return;
            } catch (NoSuchAlgorithmException e) {
                // allowed failure
            }
View Full Code Here

        throws IOException {
        for (int ii=0; ii<algorithmName.length; ii++) {
            try {
                MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
                InputStream is = new ByteArrayInputStream(myMessage);
                DigestInputStream dis = new DigestInputStream(is, md);
                for (int i=0; i<MY_MESSAGE_LEN; i++) {
                    dis.read();
                }
                // check that subsequent read() calls return -1 (eos)
                assertEquals("retval1", -1, dis.read());
                assertEquals("retval2", -1, dis.read());
                assertEquals("retval3", -1, dis.read());
                // check that 3 previous read() calls did not update digest
                assertTrue("update",
                        Arrays.equals(dis.getMessageDigest().digest(),
                                MDGoldenData.getDigest(algorithmName[ii])));
                return;
            } catch (NoSuchAlgorithmException e) {
                // allowed failure
            }
View Full Code Here

        throws IOException {
        for (int ii=0; ii<algorithmName.length; ii++) {
            try {
                MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
                InputStream is = new ByteArrayInputStream(myMessage);
                DigestInputStream dis = new DigestInputStream(is, md);
               
                // turn digest off
                dis.on(false);
               
                for (int i=0; i<MY_MESSAGE_LEN; i++) {
                    dis.read();
                }
               
                // check that digest value has not been updated by read()
                assertTrue(Arrays.equals(dis.getMessageDigest().digest(),
                        MDGoldenData.getDigest(algorithmName[ii]+"_NU")));
                return;
            } catch (NoSuchAlgorithmException e) {
                // allowed failure
            }
View Full Code Here

     */
    public final void testRead04() throws IOException {
        for (int ii=0; ii<algorithmName.length; ii++) {
            try {
                MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
                DigestInputStream dis = new DigestInputStream(null, md);
                // must result in an exception
                try {
                    for (int i=0; i<MY_MESSAGE_LEN; i++) {
                        dis.read();
                    }
                } catch (Exception e) {
                    return;
                }

View Full Code Here

TOP

Related Classes of java.security.DigestInputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.