use of java.security.DigestOutputStream in project jdk8u_jdk by JetBrains.
the class CipherStreamClose method streamEncrypt.
public static byte[] streamEncrypt(String message, SecretKey key, MessageDigest digest) throws Exception {
byte[] data;
Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
encCipher.init(Cipher.ENCRYPT_MODE, key);
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
DigestOutputStream dos = new DigestOutputStream(bos, digest);
CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
oos.writeObject(message);
}
data = bos.toByteArray();
}
if (debug) {
System.out.println(DatatypeConverter.printHexBinary(data));
}
return data;
}
use of java.security.DigestOutputStream in project jackrabbit-oak by apache.
the class ConsolidatedDataStoreStatsTest method getIdForInputStream.
private String getIdForInputStream(final InputStream in) throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
OutputStream output = new DigestOutputStream(new NullOutputStream(), digest);
try {
IOUtils.copyLarge(in, output);
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(in);
}
return encodeHexString(digest.digest());
}
use of java.security.DigestOutputStream in project jackrabbit-oak by apache.
the class CachingDataStoreTest method getIdForInputStream.
private String getIdForInputStream(File f) throws Exception {
FileInputStream in = null;
OutputStream output = null;
try {
in = new FileInputStream(f);
MessageDigest digest = MessageDigest.getInstance("SHA-256");
output = new DigestOutputStream(new NullOutputStream(), digest);
IOUtils.copyLarge(in, output);
return encodeHexString(digest.digest());
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(in);
}
}
use of java.security.DigestOutputStream in project robovm by robovm.
the class DigestOutputStreamTest method test_write$BII_2.
/**
* Test #2 for <code>write(byte[],int,int)</code> method<br>
*
* Assertion: put bytes into output stream<br>
*
* Assertion: updates associated digest<br>
*/
public final void test_write$BII_2() throws IOException {
// check precondition
assertEquals(0, MY_MESSAGE_LEN % CHUNK_SIZE);
for (int k = 0; k < algorithmName.length; k++) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
MessageDigest md = MessageDigest.getInstance(algorithmName[k]);
DigestOutputStream dos = new DigestOutputStream(bos, md);
// write message by chunks
for (int i = 0; i < MY_MESSAGE_LEN / CHUNK_SIZE; i++) {
dos.write(myMessage, i * CHUNK_SIZE, CHUNK_SIZE);
}
// check write
assertTrue("write", Arrays.equals(myMessage, bos.toByteArray()));
// check that associated digest has been updated properly
assertTrue("update", Arrays.equals(dos.getMessageDigest().digest(), MDGoldenData.getDigest(algorithmName[k])));
return;
} catch (NoSuchAlgorithmException e) {
// allowed failure
}
}
fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
use of java.security.DigestOutputStream in project robovm by robovm.
the class DigestOutputStreamTest method testWriteint05.
/**
* Test #5 for <code>write(int)</code> method<br>
* Test #2 for <code>on(boolean)</code> method<br>
*
* Assertion: broken <code>DigestOutputStream</code>instance:
* associated <code>MessageDigest</code> not set.
* <code>write(int)</code> must work when digest
* functionality is off
*/
public final void testWriteint05() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
DigestOutputStream dos = new DigestOutputStream(bos, null);
// set digest functionality to off
dos.on(false);
// the following must pass without any exception
for (int i = 0; i < MY_MESSAGE_LEN; i++) {
dos.write(myMessage[i]);
}
// check that bytes have been written correctly
assertTrue(Arrays.equals(MDGoldenData.getMessage(), bos.toByteArray()));
}
Aggregations