-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This does away with EncryptionTranslator and its cached buffers, instead allocating what it needs and leaning on the cipher itself to produce the resulting buffer.
- Loading branch information
Showing
3 changed files
with
51 additions
and
74 deletions.
There are no files selected for viewing
39 changes: 26 additions & 13 deletions
39
mod/common/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,44 @@ | ||
package gjum.minecraft.mapsync.common.net.encryption; | ||
|
||
import gjum.minecraft.mapsync.common.net.Packet; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.MessageToMessageDecoder; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.ShortBufferException; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import java.security.GeneralSecurityException; | ||
import java.security.Key; | ||
import java.util.List; | ||
import javax.crypto.Cipher; | ||
import javax.crypto.ShortBufferException; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class EncryptionDecoder extends MessageToMessageDecoder<ByteBuf> { | ||
private final EncryptionTranslator decryptionCodec; | ||
private final Cipher cipher; | ||
|
||
public EncryptionDecoder(Key key) { | ||
public EncryptionDecoder( | ||
final @NotNull Key key | ||
) { | ||
try { | ||
Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding"); | ||
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(key.getEncoded())); | ||
decryptionCodec = new EncryptionTranslator(cipher); | ||
} catch (GeneralSecurityException e) { | ||
throw new RuntimeException(e); | ||
this.cipher = Cipher.getInstance("AES/CFB8/NoPadding"); | ||
this.cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(key.getEncoded())); | ||
} | ||
catch (final GeneralSecurityException thrown) { | ||
throw new IllegalStateException(thrown); | ||
} | ||
} | ||
|
||
@Override | ||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws ShortBufferException { | ||
out.add(decryptionCodec.decipher(ctx, in)); | ||
protected void decode( | ||
final ChannelHandlerContext ctx, | ||
final ByteBuf in, | ||
final List<Object> out | ||
) throws ShortBufferException { | ||
final byte[] receivedBytes = Packet.readByteArrayOfSize(in, in.readableBytes()); | ||
final byte[] decryptedBytes = this.cipher.update(receivedBytes); | ||
if (ArrayUtils.getLength(decryptedBytes) > 0) { | ||
out.add(Unpooled.wrappedBuffer(decryptedBytes)); | ||
} | ||
} | ||
} |
38 changes: 25 additions & 13 deletions
38
mod/common/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,42 @@ | ||
package gjum.minecraft.mapsync.common.net.encryption; | ||
|
||
import gjum.minecraft.mapsync.common.net.Packet; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.MessageToByteEncoder; | ||
|
||
import java.security.GeneralSecurityException; | ||
import java.security.Key; | ||
import javax.crypto.Cipher; | ||
import javax.crypto.ShortBufferException; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import java.security.GeneralSecurityException; | ||
import java.security.Key; | ||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class EncryptionEncoder extends MessageToByteEncoder<ByteBuf> { | ||
private final EncryptionTranslator encryptionCodec; | ||
private final Cipher cipher; | ||
|
||
public EncryptionEncoder(Key key) { | ||
public EncryptionEncoder( | ||
final @NotNull Key key | ||
) { | ||
try { | ||
Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding"); | ||
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(key.getEncoded())); | ||
encryptionCodec = new EncryptionTranslator(cipher); | ||
} catch (GeneralSecurityException e) { | ||
throw new RuntimeException(e); | ||
this.cipher = Cipher.getInstance("AES/CFB8/NoPadding"); | ||
this.cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(key.getEncoded())); | ||
} | ||
} | ||
catch (final GeneralSecurityException thrown) { | ||
throw new IllegalStateException(thrown); | ||
} | ||
} | ||
|
||
@Override | ||
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws ShortBufferException { | ||
encryptionCodec.encipher(in, out); | ||
protected void encode( | ||
final ChannelHandlerContext ctx, | ||
final ByteBuf in, | ||
final ByteBuf out | ||
) throws ShortBufferException { | ||
final byte[] sendingBytes = Packet.readByteArrayOfSize(in, in.readableBytes()); | ||
final byte[] encryptedBytes = this.cipher.update(sendingBytes); | ||
if (ArrayUtils.getLength(encryptedBytes) > 0) { | ||
out.writeBytes(encryptedBytes); | ||
} | ||
} | ||
} |
48 changes: 0 additions & 48 deletions
48
...mmon/src/main/java/gjum/minecraft/mapsync/common/net/encryption/EncryptionTranslator.java
This file was deleted.
Oops, something went wrong.