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);
}
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');
}
}
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);
}
}
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);
}
}
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;
}
}
Aggregations