Refactor cipher logic.

This commit is contained in:
Andrew Steinborn
2018-08-04 00:09:25 -04:00
parent 6f3397f76f
commit 9438d087e2
6 changed files with 37 additions and 3 deletions

View File

@@ -10,10 +10,22 @@ import javax.crypto.spec.IvParameterSpec;
import java.security.GeneralSecurityException;
public class JavaVelocityCipher implements VelocityCipher {
public static final VelocityCipherFactory FACTORY = new VelocityCipherFactory() {
@Override
public VelocityCipher forEncryption(SecretKey key) throws GeneralSecurityException {
return new JavaVelocityCipher(true, key);
}
@Override
public VelocityCipher forDecryption(SecretKey key) throws GeneralSecurityException {
return new JavaVelocityCipher(false, key);
}
};
private final Cipher cipher;
private boolean disposed = false;
public JavaVelocityCipher(boolean encrypt, SecretKey key) throws GeneralSecurityException {
private JavaVelocityCipher(boolean encrypt, SecretKey key) throws GeneralSecurityException {
this.cipher = Cipher.getInstance("AES/CFB8/NoPadding");
this.cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, key, new IvParameterSpec(key.getEncoded()));
}

View File

@@ -0,0 +1,10 @@
package com.velocitypowered.natives.encryption;
import javax.crypto.SecretKey;
import java.security.GeneralSecurityException;
public interface VelocityCipherFactory {
VelocityCipher forEncryption(SecretKey key) throws GeneralSecurityException;
VelocityCipher forDecryption(SecretKey key) throws GeneralSecurityException;
}

View File

@@ -5,6 +5,8 @@ import com.velocitypowered.natives.compression.JavaVelocityCompressor;
import com.velocitypowered.natives.compression.NativeVelocityCompressor;
import com.velocitypowered.natives.compression.VelocityCompressor;
import com.velocitypowered.natives.compression.VelocityCompressorFactory;
import com.velocitypowered.natives.encryption.JavaVelocityCipher;
import com.velocitypowered.natives.encryption.VelocityCipherFactory;
import java.io.IOException;
import java.nio.file.Files;
@@ -46,4 +48,10 @@ public class Natives {
new NativeCodeLoader.Variant<>(NativeCodeLoader.ALWAYS, () -> {}, "Java compression", JavaVelocityCompressor.FACTORY)
)
);
public static final NativeCodeLoader<VelocityCipherFactory> cipher = new NativeCodeLoader<>(
ImmutableList.of(
new NativeCodeLoader.Variant<>(NativeCodeLoader.ALWAYS, () -> {}, "Java cipher", JavaVelocityCipher.FACTORY)
)
);
}