set up NoUtil to use a env secret key
This commit is contained in:
@@ -3,7 +3,7 @@ package nodash.core;
|
||||
import java.security.PublicKey;
|
||||
import java.util.Collection;
|
||||
|
||||
import nodash.core.exceptions.NoAdapterException;
|
||||
import nodash.exceptions.NoAdapterException;
|
||||
import nodash.exceptions.NoUserAlreadyOnlineException;
|
||||
import nodash.exceptions.NoUserNotValidException;
|
||||
import nodash.models.NoByteSet;
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
package nodash.core;
|
||||
|
||||
import nodash.core.exceptions.NoAdapterException;
|
||||
import nodash.exceptions.NoAdapterException;
|
||||
import nodash.exceptions.NoByteSetBadDecryptionException;
|
||||
import nodash.exceptions.NoDashFatalException;
|
||||
import nodash.exceptions.NoSessionAlreadyAwaitingConfirmationException;
|
||||
|
||||
@@ -18,7 +18,7 @@ import javax.crypto.IllegalBlockSizeException;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import nodash.core.exceptions.NoAdapterException;
|
||||
import nodash.exceptions.NoAdapterException;
|
||||
import nodash.exceptions.NoDashFatalException;
|
||||
import nodash.exceptions.NoUserAlreadyOnlineException;
|
||||
import nodash.exceptions.NoUserNotValidException;
|
||||
|
||||
@@ -28,15 +28,19 @@ import java.security.spec.KeySpec;
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.KeyGenerator;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.SecretKeyFactory;
|
||||
import javax.crypto.spec.PBEKeySpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import nodash.exceptions.NoDashFatalException;
|
||||
|
||||
public final class NoUtil {
|
||||
public static final SecretKey SECRET_KEY = setupSecretKey();
|
||||
public static final String CIPHER_TYPE = "AES/ECB/PKCS5PADDING";
|
||||
public static final String CIPHER_KEY_SPEC = "AES";
|
||||
public static final String DIGEST_TYPE = "SHA-512";
|
||||
@@ -49,6 +53,17 @@ public final class NoUtil {
|
||||
public static final int AES_STRENGTH = 256;
|
||||
public static final byte BLANK_BYTE = 'A';
|
||||
|
||||
private static SecretKey setupSecretKey() {
|
||||
System.out.println(System.getenv());
|
||||
String secretEnv = System.getenv("NODASH_SECRET");
|
||||
if (secretEnv == null) {
|
||||
throw new RuntimeException("Can't find NODASH_SECRET.");
|
||||
} else {
|
||||
byte[] encoded= Base64.decodeBase64(secretEnv);
|
||||
return new SecretKeySpec(encoded, 0, encoded.length, NoUtil.CIPHER_KEY_SPEC);
|
||||
}
|
||||
}
|
||||
|
||||
public static char[] bytesToChars(byte[] array) {
|
||||
char[] result = new char[array.length];
|
||||
for (int x = 0; x < array.length; x++) {
|
||||
@@ -84,7 +99,7 @@ public final class NoUtil {
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new NoDashFatalException("Value for PBE_TYPE is not valid.", e);
|
||||
}
|
||||
KeySpec spec = new PBEKeySpec(password, NoCore.config.getSecretKey().getEncoded(), 65536, 256);
|
||||
KeySpec spec = new PBEKeySpec(password, SECRET_KEY.getEncoded(), 65536, 256);
|
||||
SecretKey key;
|
||||
try {
|
||||
key = skf.generateSecret(spec);
|
||||
@@ -145,7 +160,7 @@ public final class NoUtil {
|
||||
}
|
||||
|
||||
public static byte[] encrypt(byte[] data) {
|
||||
return NoUtil.encrypt(data, NoCore.config.getSecretKey().getEncoded());
|
||||
return NoUtil.encrypt(data, SECRET_KEY.getEncoded());
|
||||
}
|
||||
|
||||
public static byte[] decrypt(byte[] data, byte[] key) throws IllegalBlockSizeException,
|
||||
@@ -169,7 +184,7 @@ public final class NoUtil {
|
||||
}
|
||||
|
||||
public static byte[] decrypt(byte[] data) throws IllegalBlockSizeException, BadPaddingException {
|
||||
return NoUtil.decrypt(data, NoCore.config.getSecretKey().getEncoded());
|
||||
return NoUtil.decrypt(data, SECRET_KEY.getEncoded());
|
||||
}
|
||||
|
||||
public static byte[] encryptRsa(byte[] data, PublicKey publicKey) {
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package nodash.core.exceptions;
|
||||
|
||||
import nodash.exceptions.NoDashException;
|
||||
|
||||
public class NoAdapterException extends NoDashException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public NoAdapterException(String message, Throwable e) {
|
||||
super(message, e);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user