Skip to content

Commit

Permalink
RsaCipher密钥管理和初始化代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
夜色 committed Jun 9, 2018
1 parent ed14d9c commit 6c2e1b7
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
*
* @author [email protected]
*/
public interface CipherFactory extends Factory<Cipher> {
public interface RsaCipherFactory extends Factory<Cipher> {
static Cipher create() {
return SpiLoader.load(CipherFactory.class).get();
return SpiLoader.load(RsaCipherFactory.class).get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

package com.mpush.common.message;

import com.mpush.api.message.Message;
import com.mpush.api.connection.Cipher;
import com.mpush.api.connection.Connection;
import com.mpush.api.connection.SessionContext;
import com.mpush.api.message.Message;
import com.mpush.api.protocol.Packet;
import com.mpush.tools.Jsons;
import com.mpush.tools.common.IOUtils;
Expand Down Expand Up @@ -86,8 +86,8 @@ private void decodeBinaryBody0() {
//1.解密
byte[] tmp = packet.body;
if (packet.hasFlag(Packet.FLAG_CRYPTO)) {
if (connection.getSessionContext().cipher != null) {
tmp = connection.getSessionContext().cipher.decrypt(tmp);
if (getCipher() != null) {
tmp = getCipher().decrypt(tmp);
}
}
//2.解压
Expand Down Expand Up @@ -121,9 +121,9 @@ private void encodeBinaryBody0() {
}

//2.加密
SessionContext context = connection.getSessionContext();
if (context.cipher != null) {
byte[] result = context.cipher.encrypt(tmp);
Cipher cipher = getCipher();
if (cipher != null) {
byte[] result = cipher.encrypt(tmp);
if (result.length > 0) {
tmp = result;
packet.addFlag(Packet.FLAG_CRYPTO);
Expand Down Expand Up @@ -237,6 +237,10 @@ public void runInRequestThread(Runnable runnable) {
connection.getChannel().eventLoop().execute(runnable);
}

protected Cipher getCipher() {
return connection.getSessionContext().cipher;
}

@Override
public abstract String toString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@

import com.mpush.api.connection.Cipher;
import com.mpush.api.spi.Spi;
import com.mpush.api.spi.core.CipherFactory;
import com.mpush.api.spi.core.RsaCipherFactory;

/**
* Created by yxx on 2016/5/19.
*
* @author [email protected]
*/
@Spi
public class RsaCipherFactory implements CipherFactory {
public class DefaultRsaCipherFactory implements RsaCipherFactory {
private static final RsaCipher RSA_CIPHER = RsaCipher.create();

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.mpush.common.security.DefaultRsaCipherFactory
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@

package com.mpush.netty.connection;

import com.mpush.api.connection.Cipher;
import com.mpush.api.connection.Connection;
import com.mpush.api.connection.SessionContext;
import com.mpush.api.protocol.Packet;
import com.mpush.api.spi.core.CipherFactory;
import com.mpush.netty.codec.PacketEncoder;
import com.mpush.api.spi.core.RsaCipherFactory;
import com.mpush.tools.log.Logs;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.socket.DatagramPacket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -40,7 +37,6 @@
*/
public final class NettyConnection implements Connection, ChannelFutureListener {
private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnection.class);
private static final Cipher RSA_CIPHER = CipherFactory.create();
private SessionContext context;
private Channel channel;
private volatile byte status = STATUS_NEW;
Expand All @@ -54,7 +50,7 @@ public void init(Channel channel, boolean security) {
this.lastReadTime = System.currentTimeMillis();
this.status = STATUS_CONNECTED;
if (security) {
this.context.changeCipher(RSA_CIPHER);
this.context.changeCipher(RsaCipherFactory.create());
}
}

Expand Down

0 comments on commit 6c2e1b7

Please sign in to comment.