Search in sources :

Example 56 with DigestOutputStream

use of java.security.DigestOutputStream in project fdroidclient by f-droid.

the class JKS method engineStore.

public void engineStore(OutputStream out, char[] passwd) throws IOException, NoSuchAlgorithmException, CertificateException {
    MessageDigest md = MessageDigest.getInstance("SHA1");
    md.update(charsToBytes(passwd));
    md.update("Mighty Aphrodite".getBytes("UTF-8"));
    DataOutputStream dout = new DataOutputStream(new DigestOutputStream(out, md));
    dout.writeInt(MAGIC);
    dout.writeInt(2);
    dout.writeInt(aliases.size());
    for (Enumeration e = aliases.elements(); e.hasMoreElements(); ) {
        String alias = (String) e.nextElement();
        if (trustedCerts.containsKey(alias)) {
            dout.writeInt(TRUSTED_CERT);
            dout.writeUTF(alias);
            dout.writeLong(((Date) dates.get(alias)).getTime());
            writeCert(dout, (Certificate) trustedCerts.get(alias));
        } else {
            dout.writeInt(PRIVATE_KEY);
            dout.writeUTF(alias);
            dout.writeLong(((Date) dates.get(alias)).getTime());
            byte[] key = (byte[]) privateKeys.get(alias);
            dout.writeInt(key.length);
            dout.write(key);
            Certificate[] chain = (Certificate[]) certChains.get(alias);
            dout.writeInt(chain.length);
            for (int i = 0; i < chain.length; i++) writeCert(dout, chain[i]);
        }
    }
    byte[] digest = md.digest();
    dout.write(digest);
}
Also used : Enumeration(java.util.Enumeration) DataOutputStream(java.io.DataOutputStream) DigestOutputStream(java.security.DigestOutputStream) MessageDigest(java.security.MessageDigest) Certificate(java.security.cert.Certificate)

Example 57 with DigestOutputStream

use of java.security.DigestOutputStream in project bazel by bazelbuild.

the class SignedJarBuilder method writeSignatureFile.

/** Writes a .SF file with a digest to the manifest. */
private void writeSignatureFile(SignatureOutputStream out) throws IOException, GeneralSecurityException {
    Manifest sf = new Manifest();
    Attributes main = sf.getMainAttributes();
    main.putValue("Signature-Version", "1.0");
    main.putValue("Created-By", "1.0 (Android)");
    BASE64Encoder base64 = new BASE64Encoder();
    MessageDigest md = MessageDigest.getInstance(DIGEST_ALGORITHM);
    PrintStream print = new PrintStream(new DigestOutputStream(new ByteArrayOutputStream(), md), true, SdkConstants.UTF_8);
    // Digest of the entire manifest
    mManifest.write(print);
    print.flush();
    main.putValue(DIGEST_MANIFEST_ATTR, base64.encode(md.digest()));
    Map<String, Attributes> entries = mManifest.getEntries();
    for (Map.Entry<String, Attributes> entry : entries.entrySet()) {
        // Digest of the manifest stanza for this entry.
        print.print("Name: " + entry.getKey() + "\r\n");
        for (Map.Entry<Object, Object> att : entry.getValue().entrySet()) {
            print.print(att.getKey() + ": " + att.getValue() + "\r\n");
        }
        print.print("\r\n");
        print.flush();
        Attributes sfAttr = new Attributes();
        sfAttr.putValue(DIGEST_ATTR, base64.encode(md.digest()));
        sf.getEntries().put(entry.getKey(), sfAttr);
    }
    sf.write(out);
    // As a workaround, add an extra CRLF in this case.
    if ((out.size() % 1024) == 0) {
        out.write('\r');
        out.write('\n');
    }
}
Also used : PrintStream(java.io.PrintStream) BASE64Encoder(sun.misc.BASE64Encoder) Attributes(java.util.jar.Attributes) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) DigestOutputStream(java.security.DigestOutputStream) MessageDigest(java.security.MessageDigest) Map(java.util.Map)

Example 58 with DigestOutputStream

use of java.security.DigestOutputStream in project jetty.project by eclipse.

the class GzipTester method getResponseMetadata.

public ContentMetadata getResponseMetadata(Response response) throws Exception {
    long size = response.getContentBytes().length;
    String contentEncoding = response.get("Content-Encoding");
    ByteArrayInputStream bais = null;
    InputStream in = null;
    DigestOutputStream digester = null;
    ByteArrayOutputStream uncompressedStream = null;
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA1");
        bais = new ByteArrayInputStream(response.getContentBytes());
        if (contentEncoding == null) {
            LOG.debug("No response content-encoding");
            in = new PassThruInputStream(bais);
        } else if (contentEncoding.contains(GzipHandler.GZIP)) {
            in = new GZIPInputStream(bais);
        } else if (contentEncoding.contains(GzipHandler.DEFLATE)) {
            in = new InflaterInputStream(bais, new Inflater(true));
        } else {
            assertThat("Unexpected response content-encoding", contentEncoding, isEmptyOrNullString());
        }
        uncompressedStream = new ByteArrayOutputStream((int) size);
        digester = new DigestOutputStream(uncompressedStream, digest);
        IO.copy(in, digester);
        byte[] output = uncompressedStream.toByteArray();
        String actualSha1Sum = Hex.asHex(digest.digest());
        return new ContentMetadata(output.length, actualSha1Sum);
    } finally {
        IO.close(digester);
        IO.close(in);
        IO.close(bais);
        IO.close(uncompressedStream);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestOutputStream(java.security.DigestOutputStream) Inflater(java.util.zip.Inflater) MessageDigest(java.security.MessageDigest)

Example 59 with DigestOutputStream

use of java.security.DigestOutputStream in project jetty.project by eclipse.

the class GzipTester method assertIsResponseNotGzipFiltered.

/**
     * Makes sure that the response contains an unfiltered file contents.
     * <p>
     * This is used to test exclusions and passthroughs in the GzipHandler.
     * <p>
     * An example is to test that it is possible to configure GzipFilter to not recompress content that shouldn't be compressed by the GzipFilter.
     *
     * @param requestedFilename
     *            the filename used to on the GET request,.
     * @param testResourceSha1Sum
     *            the sha1sum file that contains the SHA1SUM checksum that will be used to verify that the response contents are what is intended.
     * @param expectedContentType the expected content type
     * @param expectedContentEncoding
     *            can be non-null in some circumstances, eg when dealing with pre-gzipped .svgz files
     * @throws Exception on test failure
     */
public void assertIsResponseNotGzipFiltered(String requestedFilename, String testResourceSha1Sum, String expectedContentType, String expectedContentEncoding) throws Exception {
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    request.setMethod("GET");
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setHeader("Accept-Encoding", compressionType);
    if (this.userAgent != null)
        request.setHeader("User-Agent", this.userAgent);
    request.setURI("/context/" + requestedFilename);
    // Issue the request
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    dumpHeaders(requestedFilename + " / Response Headers", response);
    // Assert the response headers
    String prefix = requestedFilename + " / Response";
    Assert.assertThat(prefix + ".status", response.getStatus(), is(HttpServletResponse.SC_OK));
    Assert.assertThat(prefix + ".header[Content-Length]", response.get("Content-Length"), notNullValue());
    Assert.assertThat(prefix + ".header[Content-Encoding] (should not be recompressed by GzipHandler)", response.get("Content-Encoding"), expectedContentEncoding == null ? nullValue() : notNullValue());
    if (expectedContentEncoding != null)
        Assert.assertThat(prefix + ".header[Content-Encoding]", response.get("Content-Encoding"), is(expectedContentEncoding));
    Assert.assertThat(prefix + ".header[Content-Type] (should have a Content-Type associated with it)", response.get("Content-Type"), notNullValue());
    Assert.assertThat(prefix + ".header[Content-Type]", response.get("Content-Type"), is(expectedContentType));
    Assert.assertThat(response.get("ETAG"), Matchers.startsWith("W/"));
    ByteArrayInputStream bais = null;
    DigestOutputStream digester = null;
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA1");
        bais = new ByteArrayInputStream(response.getContentBytes());
        digester = new DigestOutputStream(new NoOpOutputStream(), digest);
        IO.copy(bais, digester);
        String actualSha1Sum = Hex.asHex(digest.digest());
        String expectedSha1Sum = loadExpectedSha1Sum(testResourceSha1Sum);
        Assert.assertEquals(requestedFilename + " / SHA1Sum of content", expectedSha1Sum, actualSha1Sum);
    } finally {
        IO.close(digester);
        IO.close(bais);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DigestOutputStream(java.security.DigestOutputStream) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageDigest(java.security.MessageDigest) Response(org.eclipse.jetty.http.HttpTester.Response) HttpTester(org.eclipse.jetty.http.HttpTester)

Example 60 with DigestOutputStream

use of java.security.DigestOutputStream in project robovm by robovm.

the class DigestOutputStreamTest method testWriteint04.

/**
     * Test #4 for <code>write(int)</code> method<br>
     *
     * Assertion: broken <code>DigestOutputStream</code>instance:
     * associated <code>MessageDigest</code> not set.
     * <code>write(int)</code> must not work when digest
     * functionality is on
     */
public final void testWriteint04() throws IOException {
    OutputStream os = new ByteArrayOutputStream(MY_MESSAGE_LEN);
    DigestOutputStream dos = new DigestOutputStream(os, null);
    // must result in an exception
    try {
        for (int i = 0; i < MY_MESSAGE_LEN; i++) {
            dos.write(myMessage[i]);
        }
        fail("OutputStream not set. write(int) must not work");
    } catch (Exception e) {
        return;
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) DigestOutputStream(java.security.DigestOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Support_OutputStream(tests.support.Support_OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

Aggregations

DigestOutputStream (java.security.DigestOutputStream)107 MessageDigest (java.security.MessageDigest)87 ByteArrayOutputStream (java.io.ByteArrayOutputStream)54 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)44 IOException (java.io.IOException)41 OutputStream (java.io.OutputStream)26 FileOutputStream (java.io.FileOutputStream)14 File (java.io.File)13 Support_OutputStream (tests.support.Support_OutputStream)9 NullOutputStream (com.keepassdroid.stream.NullOutputStream)8 BufferedOutputStream (java.io.BufferedOutputStream)8 InputStream (java.io.InputStream)8 Map (java.util.Map)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 NullOutputStream (org.apache.commons.io.output.NullOutputStream)7 Base64OutputStream (android.util.Base64OutputStream)6 DigestInputStream (java.security.DigestInputStream)6 Attributes (java.util.jar.Attributes)6 DataOutputStream (java.io.DataOutputStream)5 FileInputStream (java.io.FileInputStream)5