Merge branch 'eugenp:master' into JAVA-20167_1

This commit is contained in:
timis1
2023-06-15 00:43:18 +03:00
committed by GitHub
17 changed files with 474 additions and 24 deletions
@@ -12,10 +12,8 @@ public class Citizen {
public Citizen() {
}
public Citizen(EncryptedCitizen encryptedCitizen) {
if (encryptedCitizen != null) {
this.name = encryptedCitizen.getName();
}
public Citizen(String name) {
this.name = name;
}
public String getName() {
@@ -13,8 +13,8 @@ public class EncryptedCitizen {
public EncryptedCitizen() {
}
public EncryptedCitizen(Citizen citizen) {
this.name = citizen.getName();
public EncryptedCitizen(String name) {
this.name = name;
}
public String getName() {
@@ -39,7 +39,7 @@ public class CitizenService {
if (encryptionConfig.isAutoEncryption()) {
return mongo.save(citizen);
} else {
EncryptedCitizen encryptedCitizen = new EncryptedCitizen(citizen);
EncryptedCitizen encryptedCitizen = new EncryptedCitizen(citizen.getName());
encryptedCitizen.setEmail(encrypt(citizen.getEmail(), DETERMINISTIC_ALGORITHM));
encryptedCitizen.setBirthYear(encrypt(citizen.getBirthYear(), RANDOM_ALGORITHM));
@@ -77,19 +77,10 @@ public class CitizenService {
}
}
public Binary encrypt(Object value, String algorithm) {
if (value == null)
public Binary encrypt(BsonValue bsonValue, String algorithm) {
if (bsonValue == null)
return null;
BsonValue bsonValue;
if (value instanceof Integer) {
bsonValue = new BsonInt32((Integer) value);
} else if (value instanceof String) {
bsonValue = new BsonString((String) value);
} else {
throw new IllegalArgumentException("unsupported type: " + value.getClass());
}
EncryptOptions options = new EncryptOptions(algorithm);
options.keyId(encryptionConfig.getDataKeyId());
@@ -97,6 +88,20 @@ public class CitizenService {
return new Binary(encryptedValue.getType(), encryptedValue.getData());
}
public Binary encrypt(String value, String algorithm) {
if (value == null)
return null;
return encrypt(new BsonString(value), algorithm);
}
public Binary encrypt(Integer value, String algorithm) {
if (value == null)
return null;
return encrypt(new BsonInt32(value), algorithm);
}
public BsonValue decryptProperty(Binary value) {
if (value == null)
return null;
@@ -108,7 +113,7 @@ public class CitizenService {
if (encrypted == null)
return null;
Citizen citizen = new Citizen(encrypted);
Citizen citizen = new Citizen(encrypted.getName());
BsonValue decryptedBirthYear = decryptProperty(encrypted.getBirthYear());
if (decryptedBirthYear != null) {