Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.OutputStream;
- import javax.crypto.Cipher;
- import javax.crypto.CipherOutputStream;
- import javax.crypto.Mac;
- public class CipherHmacOutputStream_PLAINTEXT_HMAC extends CipherOutputStream {
- private Cipher cipher;
- private Mac mac;
- private byte[] iv;
- public CipherHmacOutputStream_PLAINTEXT_HMAC(OutputStream os, Cipher c, Mac m, byte[] iv) {
- super(os, c);
- cipher = c;
- mac = m;
- }
- @Override
- public void write(int b) throws IOException {
- mac.update((byte) b);
- super.write(b);
- }
- @Override
- public void write(byte[] b) throws IOException {
- mac.update(b);
- super.write(b);
- }
- @Override
- public void write(byte[] b, int off, int len) throws IOException {
- mac.update(b, off, len);
- super.write(b, off, len);
- }
- @Override
- public void close() throws IOException {
- writeHmac();
- super.close();
- }
- private void writeHmac() throws IOException {
- // Add IV & algorithm details
- try {
- mac.update(iv);
- mac.update(cipher.getAlgorithm().getBytes("UTF8"));
- mac.update(cipher.getParameters().getEncoded());
- byte[] macBytes = mac.doFinal();
- super.write(macBytes);
- }
- catch (Exception e) {
- throw new IOException(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement