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);

View file

@ -3,7 +3,7 @@ package io.xpipe.ext.base.actions;
import io.xpipe.app.comp.source.store.GuiDsStoreCreator;
import io.xpipe.app.ext.ActionProvider;
import io.xpipe.app.storage.DataStoreEntry;
import io.xpipe.app.util.SecretHelper;
import io.xpipe.app.util.DefaultSecretValue;
import io.xpipe.core.store.DataStore;
import io.xpipe.core.util.JacksonMapper;
import lombok.Value;
@ -43,7 +43,7 @@ public class AddStoreAction implements ActionProvider {
@Override
public Action createAction(List<String> args) throws Exception {
var storeString = SecretHelper.encryptInPlace(args.get(0));
var storeString = DefaultSecretValue.builder().encryptedValue(args.get(0)).build();
var store = JacksonMapper.parse(storeString.getSecretValue(), DataStore.class);
return new Action(store);
}