All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rx.net.AESCodec Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.net;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.MessageToMessageCodec;
import lombok.RequiredArgsConstructor;
import org.rx.codec.AESUtil;
import org.rx.core.Constants;

import java.util.List;

@RequiredArgsConstructor
public class AESCodec extends MessageToMessageCodec {
    final byte[] key;

    public ChannelHandler[] channelHandlers() {
        return new ChannelHandler[]{
                new LengthFieldBasedFrameDecoder(Constants.MAX_HEAP_BUF_SIZE, 0, 4, 0, 4),
                Sockets.INT_LENGTH_PREPENDER, this
        };
    }

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) {
        ByteBuf encrypt = AESUtil.encrypt(byteBuf, key);
        list.add(encrypt);
    }

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List list) {
        ByteBuf decrypt = AESUtil.decrypt(byteBuf, key);
        list.add(decrypt);
    }
}