Small fixes [stage]

This commit is contained in:
crschnick
2023-04-20 12:18:59 +00:00
parent 1d998c7863
commit fe6d56d71e
4 changed files with 13 additions and 9 deletions

View File

@@ -11,8 +11,8 @@ import javax.crypto.spec.GCMParameterSpec;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.Random;
@SuperBuilder
@Jacksonized
@@ -31,7 +31,7 @@ public class AesSecretValue extends EncryptedSecretValue {
private static byte[] getFixedNonce(int numBytes) {
byte[] nonce = new byte[numBytes];
new SecureRandom(new byte[] {1, -28, 123}).nextBytes(nonce);
new Random(1 - 28 + 213213).nextBytes(nonce);
return nonce;
}

View File

@@ -8,7 +8,6 @@ import lombok.extern.jackson.Jacksonized;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
@SuperBuilder
@Jacksonized
@@ -22,7 +21,7 @@ public class EncryptedSecretValue implements SecretValue {
var utf8 = StandardCharsets.UTF_8.encode(CharBuffer.wrap(c));
var bytes = new byte[utf8.limit()];
utf8.get(bytes);
encryptedValue = SecretValue.base64e(encrypt(bytes));
encryptedValue = SecretValue.toBase64e(encrypt(bytes));
}
@Override
@@ -33,14 +32,14 @@ public class EncryptedSecretValue implements SecretValue {
@Override
public char[] getSecret() {
try {
var bytes = Base64.getDecoder().decode(encryptedValue.replace("-", "/"));
var bytes = SecretValue.fromBase64e(getEncryptedValue());
bytes = decrypt(bytes);
var charBuffer = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes));
var chars = new char[charBuffer.limit()];
charBuffer.get(chars);
return chars;
} catch (Exception ex) {
throw new IllegalStateException("Unable to decrypt secret");
return new char[0];
}
}

View File

@@ -9,11 +9,16 @@ import java.util.function.Consumer;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public interface SecretValue {
public static String base64e(byte[] b) {
public static String toBase64e(byte[] b) {
var base64 = Base64.getEncoder().encodeToString(b);
return base64.replace("/", "-");
}
public static byte[] fromBase64e(String s) {
var bytes = Base64.getDecoder().decode(s.replace("-", "/"));
return bytes;
}
public default void withSecretValue(Consumer<char[]> con) {
var chars = getSecret();
con.accept(chars);