use of javax.crypto.CipherOutputStream in project camel by apache.
the class CryptoDataFormat method marshal.
public void marshal(Exchange exchange, Object graph, OutputStream outputStream) throws Exception {
byte[] iv = getInitializationVector(exchange);
Key key = getKey(exchange);
InputStream plaintextStream = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, graph);
HMACAccumulator hmac = getMessageAuthenticationCode(key);
if (plaintextStream != null) {
inlineInitVector(outputStream, iv);
byte[] buffer = new byte[bufferSize];
int read;
CipherOutputStream cipherStream = null;
try {
cipherStream = new CipherOutputStream(outputStream, initializeCipher(ENCRYPT_MODE, key, iv));
while ((read = plaintextStream.read(buffer)) > 0) {
cipherStream.write(buffer, 0, read);
cipherStream.flush();
hmac.encryptUpdate(buffer, read);
}
// only write if there is data to write (IBM JDK throws exception if no data)
byte[] mac = hmac.getCalculatedMac();
if (mac != null && mac.length > 0) {
cipherStream.write(mac);
}
} finally {
IOHelper.close(cipherStream, "cipher", LOG);
IOHelper.close(plaintextStream, "plaintext", LOG);
}
}
}
use of javax.crypto.CipherOutputStream in project robovm by robovm.
the class CipherOutputStream1Test method testFlush.
/**
* flush() method testing. Tests that method flushes the data to the
* underlying output stream.
*/
public void testFlush() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestOutputStream tos = new TestOutputStream();
CipherOutputStream cos = new CipherOutputStream(tos) {
};
cos.write(data);
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream did not flush the data.");
}
}
use of javax.crypto.CipherOutputStream in project robovm by robovm.
the class CipherOutputStream1Test method testWrite3.
/**
* write(byte[] b, int off, int len) method testing.
*/
public void testWrite3() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestOutputStream tos = new TestOutputStream();
CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
for (int i = 0; i < data.length; i++) {
cos.write(data, i, 1);
}
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream wrote incorrect data.");
}
}
use of javax.crypto.CipherOutputStream in project robovm by robovm.
the class CipherOutputStream1Test method testCipherOutputStream.
/**
* CipherOutputStream(OutputStream os) method testing. Tests that
* CipherOutputStream uses NullCipher if Cipher is not specified
* in the constructor.
*/
public void testCipherOutputStream() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestOutputStream tos = new TestOutputStream();
CipherOutputStream cos = new CipherOutputStream(tos) {
};
cos.write(data);
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("NullCipher should be used " + "if Cipher is not specified.");
}
}
use of javax.crypto.CipherOutputStream in project robovm by robovm.
the class CipherOutputStream1Test method test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher.
public void test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(56, new SecureRandom());
Key key = kg.generateKey();
Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, key);
CipherOutputStream cos = new CipherOutputStream(baos, c);
assertNotNull(cos);
}
Aggregations