AesCipherDataSink.java 960 Bytes
Newer Older
liqin's avatar
liqin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
package cn.wisenergy.chnmuseum.party.common.video;

import javax.crypto.Cipher;
import java.io.IOException;

public final class AesCipherDataSink implements DataSink {

    private final DataSink wrappedDataSink;
    private final byte[] secretKey;

    private AesFlushingCipher cipher;

    public AesCipherDataSink(byte[] secretKey, DataSink wrappedDataSink) {
        this.wrappedDataSink = wrappedDataSink;
        this.secretKey = secretKey;
    }

    @Override
    public void open() throws IOException {
        wrappedDataSink.open();
        cipher = new AesFlushingCipher(Cipher.ENCRYPT_MODE, secretKey, 0, 0);
    }

    @Override
    public void write(byte[] data, int offset, int length) throws IOException {
        cipher.updateInPlace(data, offset, length);
        wrappedDataSink.write(data, offset, length);
    }

    @Override
    public void close() throws IOException {
        cipher = null;
        wrappedDataSink.close();
    }
}