package cn.wisenergy.chnmuseum.party.common.video;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class VideoEncryptUtil {

    //此为AES128位,如果要求AES256位,需要更新jdk内的包,jdk8发布版本默认不支持
    public static final String cipher = "3348c95c60520be7";
    private static final int dataLength = 4096;

    public static InputStream encrypt(InputStream fis, String cipher) throws IOException {
        final ByteArrayOutputStream[] outputStream = new ByteArrayOutputStream[1];
        AesCipherDataSink encryptingDataSink = new AesCipherDataSink(cipher.getBytes(StandardCharsets.UTF_8),
                new DataSink() {
                    @Override
                    public void open() {
                        outputStream[0] = new ByteArrayOutputStream();
                    }

                    @Override
                    public void write(byte[] buffer, int offset, int length) {
                        outputStream[0].write(buffer, offset, length);
                    }

                    @Override
                    public void close() throws IOException {
                        outputStream[0].close();
                    }
                });
        encryptingDataSink.open();
        int len;
        byte[] buffer = new byte[dataLength];
        while ((len = fis.read(buffer)) != -1) {
            encryptingDataSink.write(buffer, 0, len);
        }
        encryptingDataSink.close();
        fis.close();
        return new ByteArrayInputStream(outputStream[0].toByteArray());
    }

}