mirror of
https://github.com/xpipe-io/xpipe.git
synced 2024-11-21 23:20:23 +00:00
Rework data encryption for raw data
This commit is contained in:
parent
da42eb578f
commit
51121d2301
4 changed files with 36 additions and 7 deletions
|
@ -4,6 +4,12 @@ import lombok.EqualsAndHashCode;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import lombok.experimental.SuperBuilder;
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
|
import javax.crypto.SecretKeyFactory;
|
||||||
|
import javax.crypto.spec.GCMParameterSpec;
|
||||||
|
import javax.crypto.spec.PBEKeySpec;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
@ -11,12 +17,6 @@ import java.security.SecureRandom;
|
||||||
import java.security.spec.InvalidKeySpecException;
|
import java.security.spec.InvalidKeySpecException;
|
||||||
import java.security.spec.KeySpec;
|
import java.security.spec.KeySpec;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import javax.crypto.Cipher;
|
|
||||||
import javax.crypto.SecretKey;
|
|
||||||
import javax.crypto.SecretKeyFactory;
|
|
||||||
import javax.crypto.spec.GCMParameterSpec;
|
|
||||||
import javax.crypto.spec.PBEKeySpec;
|
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
|
||||||
|
|
||||||
@SuperBuilder
|
@SuperBuilder
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@ -41,6 +41,10 @@ public abstract class AesSecretValue extends EncryptedSecretValue {
|
||||||
super(secret);
|
super(secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AesSecretValue(byte[] b) {
|
||||||
|
super(b);
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract int getIterationCount();
|
protected abstract int getIterationCount();
|
||||||
|
|
||||||
protected byte[] getNonce(int numBytes) {
|
protected byte[] getNonce(int numBytes) {
|
||||||
|
|
|
@ -15,6 +15,10 @@ public abstract class EncryptedSecretValue implements SecretValue {
|
||||||
|
|
||||||
String encryptedValue;
|
String encryptedValue;
|
||||||
|
|
||||||
|
public EncryptedSecretValue(byte[] b) {
|
||||||
|
encryptedValue = SecretValue.toBase64e(encrypt(b));
|
||||||
|
}
|
||||||
|
|
||||||
public EncryptedSecretValue(char[] c) {
|
public EncryptedSecretValue(char[] c) {
|
||||||
var utf8 = StandardCharsets.UTF_8.encode(CharBuffer.wrap(c));
|
var utf8 = StandardCharsets.UTF_8.encode(CharBuffer.wrap(c));
|
||||||
var bytes = new byte[utf8.limit()];
|
var bytes = new byte[utf8.limit()];
|
||||||
|
@ -27,6 +31,17 @@ public abstract class EncryptedSecretValue implements SecretValue {
|
||||||
return "<encrypted secret>";
|
return "<encrypted secret>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getSecretRaw() {
|
||||||
|
try {
|
||||||
|
var bytes = SecretValue.fromBase64e(getEncryptedValue());
|
||||||
|
bytes = decrypt(bytes);
|
||||||
|
return bytes;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
return new byte[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public char[] getSecret() {
|
public char[] getSecret() {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -5,9 +5,9 @@ import lombok.EqualsAndHashCode;
|
||||||
import lombok.experimental.SuperBuilder;
|
import lombok.experimental.SuperBuilder;
|
||||||
import lombok.extern.jackson.Jacksonized;
|
import lombok.extern.jackson.Jacksonized;
|
||||||
|
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
import java.security.spec.InvalidKeySpecException;
|
import java.security.spec.InvalidKeySpecException;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import javax.crypto.SecretKey;
|
|
||||||
|
|
||||||
@JsonTypeName("default")
|
@JsonTypeName("default")
|
||||||
@SuperBuilder
|
@SuperBuilder
|
||||||
|
@ -15,6 +15,10 @@ import javax.crypto.SecretKey;
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
public class InPlaceSecretValue extends AesSecretValue {
|
public class InPlaceSecretValue extends AesSecretValue {
|
||||||
|
|
||||||
|
public InPlaceSecretValue(byte[] b) {
|
||||||
|
super(b);
|
||||||
|
}
|
||||||
|
|
||||||
public InPlaceSecretValue(char[] secret) {
|
public InPlaceSecretValue(char[] secret) {
|
||||||
super(secret);
|
super(secret);
|
||||||
}
|
}
|
||||||
|
@ -27,6 +31,10 @@ public class InPlaceSecretValue extends AesSecretValue {
|
||||||
return new InPlaceSecretValue(c);
|
return new InPlaceSecretValue(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static InPlaceSecretValue of(byte[] b) {
|
||||||
|
return new InPlaceSecretValue(b);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int getIterationCount() {
|
protected int getIterationCount() {
|
||||||
return 2048;
|
return 2048;
|
||||||
|
|
|
@ -41,6 +41,8 @@ public interface SecretValue {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
byte[] getSecretRaw();
|
||||||
|
|
||||||
char[] getSecret();
|
char[] getSecret();
|
||||||
|
|
||||||
default String getSecretValue() {
|
default String getSecretValue() {
|
||||||
|
|
Loading…
Reference in a new issue