Search in sources :

Example 1 with ChecksumException

use of com.alibaba.otter.node.etl.common.io.signature.ChecksumException in project otter by alibaba.

the class AbstractHttpPipe method decodeFile.

protected void decodeFile(File file, String key, String crc) {
    // 读取校验信息
    RandomAccessFile raf = null;
    try {
        raf = new RandomAccessFile(file, "rw");
        long totallength = file.length();
        int keyLength = ByteUtils.stringToBytes(key).length;
        int crcLength = ByteUtils.stringToBytes(crc).length;
        // 长度字段起始位
        long pos = totallength - keyLength - crcLength;
        // 游标
        raf.seek(pos);
        // 读取key内容
        byte[] keyBytes = new byte[keyLength];
        raf.read(keyBytes, 0, keyLength);
        String keystr = ByteUtils.bytesToString(keyBytes);
        if (!key.equals(keystr)) {
            throw new ChecksumException("unmatch garble key with[" + key + "],[" + keystr + "]");
        }
        // 读取校验码长度
        raf.seek(pos + keyLength);
        byte[] crcBytes = new byte[crcLength];
        raf.read(crcBytes, 0, crcLength);
        String crcStr = ByteUtils.bytesToString(crcBytes);
        if (!crc.equals(crcStr)) {
            throw new ChecksumException("unmatch crc with[" + crc + "],[" + crcStr + "]");
        }
        // 设置文件长度
        raf.setLength(pos);
    } catch (Exception e) {
        throw new PipeException("read_encrypted_error", e);
    } finally {
        IOUtils.closeQuietly(raf);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ChecksumException(com.alibaba.otter.node.etl.common.io.signature.ChecksumException) PipeException(com.alibaba.otter.node.etl.common.pipe.exception.PipeException) PipeException(com.alibaba.otter.node.etl.common.pipe.exception.PipeException) ChecksumException(com.alibaba.otter.node.etl.common.io.signature.ChecksumException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with ChecksumException

use of com.alibaba.otter.node.etl.common.io.signature.ChecksumException in project otter by alibaba.

the class EncryptUtils method decrypt.

public static byte[] decrypt(EncryptedData encode) {
    String destCrc = ChecksumUtils.checksum(encode.getData());
    // 验证sign
    if (false == StringUtils.equals(encode.getCrc(), destCrc)) {
        throw new ChecksumException(String.format("orig: %s, parsed: %s not match", encode.getCrc(), destCrc));
    }
    // 调用加密工具类
    AESUtils aes = new AESUtils();
    aes.setSecretKeyString(encode.getKey());
    // 解密并解压数据
    return COMPRESSOR.decompress(aes.decrypt(encode.getData()));
}
Also used : AESUtils(com.alibaba.otter.node.etl.common.io.crypto.AESUtils) ChecksumException(com.alibaba.otter.node.etl.common.io.signature.ChecksumException)

Aggregations

ChecksumException (com.alibaba.otter.node.etl.common.io.signature.ChecksumException)2 AESUtils (com.alibaba.otter.node.etl.common.io.crypto.AESUtils)1 PipeException (com.alibaba.otter.node.etl.common.pipe.exception.PipeException)1 RandomAccessFile (java.io.RandomAccessFile)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1