Package java.security

Examples of java.security.DigestInputStream


    public final void testToString() {
        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);

                assertNotNull(dis.toString());
                return;
            } catch (NoSuchAlgorithmException e) {
                // allowed failure
            }
        }
View Full Code Here


   *        java.security.MessageDigest)
   */
  public void test_ConstructorLjava_io_InputStreamLjava_security_MessageDigest() {
    // Test for method java.security.DigestInputStream(java.io.InputStream,
    // java.security.MessageDigest)
    DigestInputStream dis = new DigestInputStream(inStream, digest);
    assertNotNull("Constructor returned null instance", dis);
  }
View Full Code Here

   * @tests java.security.DigestInputStream#getMessageDigest()
   */
  public void test_getMessageDigest() {
    // Test for method java.security.MessageDigest
    // java.security.DigestInputStream.getMessageDigest()
    DigestInputStream dis = new DigestInputStream(inStream, digest);
    assertEquals("getMessageDigest returned a bogus result", digest, dis
        .getMessageDigest());
  }
View Full Code Here

   */
    public void test_onZ() throws Exception {
        // Test for method void java.security.DigestInputStream.on(boolean)
        MessageDigest originalDigest = (MessageDigest) (digest.clone());
        MessageDigest noChangeDigest = (MessageDigest) (digest.clone());
        DigestInputStream dis = new DigestInputStream(inStream, noChangeDigest);
        // turn off processing
        dis.on(false);
        // read some data
        int c = dis.read();
        assertEquals('T', c);

        // make sure the digest for the part where it was off has not
        // changed
        assertTrue("MessageDigest changed even though processing was off",
                MessageDigest.isEqual(noChangeDigest.digest(), originalDigest
                        .digest()));
        MessageDigest changeDigest = (MessageDigest) (digest.clone());
        dis = new DigestInputStream(inStream, digest);

        // turn on processing
        dis.on(true);
        c = dis.read();
        assertEquals('h', c);

        // make sure the digest has changed
        assertTrue("MessageDigest did not change with processing on",
                !MessageDigest.isEqual(digest.digest(), changeDigest.digest()));
View Full Code Here

  /**
   * @tests java.security.DigestInputStream#read()
   */
    public void test_read() throws IOException {
        // Test for method int java.security.DigestInputStream.read()
        DigestInputStream dis = new DigestInputStream(inStream, digest);

        // read and compare the data that the inStream has
        int c;
        while ((c = dis.read()) > -1) {
            int d = inStream1.read();
            assertEquals(d, c);
        }// end while
    }
View Full Code Here

   * @tests java.security.DigestInputStream#read(byte[], int, int)
   */
  public void test_read$BII() throws IOException {
    // Test for method int java.security.DigestInputStream.read(byte [],
    // int, int)
    DigestInputStream dis = new DigestInputStream(inStream, digest);
    int bytesToRead = inStream.available();
    byte buf1[] = new byte[bytesToRead + 5];
    byte buf2[] = new byte[bytesToRead + 5];
    // make sure we're actually reading some data
    assertTrue("No data to read for this test", bytesToRead>0);
   
    // read and compare the data that the inStream has
        int bytesRead1 = dis.read(buf1, 5, bytesToRead);
        int bytesRead2 = inStream1.read(buf2, 5, bytesToRead);
        assertEquals("Didn't read the same from each stream", bytesRead1,
                bytesRead2);
        assertEquals("Didn't read the entire", bytesRead1, bytesToRead);
        // compare the arrays
View Full Code Here

   * @tests java.security.DigestInputStream#setMessageDigest(java.security.MessageDigest)
   */
  public void test_setMessageDigestLjava_security_MessageDigest() {
    // Test for method void
    // java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest)
    DigestInputStream dis = new DigestInputStream(inStream, null);
   
    // make sure the digest is null when it's not been set
    assertNull(
        "Uninitialised MessageDigest should have been returned as null",
        dis.getMessageDigest());
    dis.setMessageDigest(digest);
    assertEquals("Wrong MessageDigest was returned.", digest, dis
        .getMessageDigest());
  }
View Full Code Here

            // Create a uniqiue identifier from the content and some random data
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");

            if (content != null) {
                ByteArrayInputStream in = new ByteArrayInputStream(content);
                DigestInputStream dis = new DigestInputStream(in, messageDigest);

                while (dis.read() != -1) {
                    ;
                }

                dis.close();
                in.close();
            }

            // Add some random noise so the id is not generated soley by the
            // file content
View Full Code Here

    }

    protected byte[] hash(InputStream input) throws AmazonClientException {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            DigestInputStream digestInputStream = new DigestInputStream(input, md);
            byte[] buffer = new byte[1024];
            while (digestInputStream.read(buffer) > -1);
            return digestInputStream.getMessageDigest().digest();
        } catch (Exception e) {
            throw new AmazonClientException("Unable to compute hash while signing request: " + e.getMessage(), e);
        }
    }
View Full Code Here

    try
    {
      MessageDigest md = MessageDigest.getInstance("MD5");

      // open JAR archive
      DigestInputStream in =
        new DigestInputStream(new FileInputStream(jarFile), md);

      // read everything, this way the message-digest
      // is calculated
      byte[] readBuffer = new byte[1024];
      while (in.available() > 0)
      {
        in.read(readBuffer);
      }

      md = in.getMessageDigest();
      String sha = toHex(md.digest());

      System.out.println("MD5 = " + sha + "/" + sha_orig);

      return sha.equalsIgnoreCase(sha_orig);
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.