diff --git a/pom.xml b/pom.xml
index 33ca979..f268f98 100644
--- a/pom.xml
+++ b/pom.xml
@@ -68,6 +68,12 @@
${junit-jupiter.version}
test
+
+ uk.org.webcompere
+ system-stubs-jupiter
+ 2.1.6
+ test
+
org.mockito
mockito-junit-jupiter
diff --git a/src/main/java/io/github/tap30/hiss/Encrypted.java b/src/main/java/io/github/tap30/hiss/Encrypted.java
index 12f662f..4d31418 100644
--- a/src/main/java/io/github/tap30/hiss/Encrypted.java
+++ b/src/main/java/io/github/tap30/hiss/Encrypted.java
@@ -11,7 +11,7 @@
/**
* Fields annotated using this will be encrypted.
*/
-// todo: add example
+// Todo: add example
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Encrypted {
diff --git a/src/main/java/io/github/tap30/hiss/EncryptedInside.java b/src/main/java/io/github/tap30/hiss/EncryptedInside.java
index d08972f..efb6942 100644
--- a/src/main/java/io/github/tap30/hiss/EncryptedInside.java
+++ b/src/main/java/io/github/tap30/hiss/EncryptedInside.java
@@ -5,11 +5,11 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
-// todo: improve doc
+// Todo: improve doc
/**
* Fields annotated with this will be scanned
*/
-// todo: add example
+// Todo: add example
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface EncryptedInside {
diff --git a/src/main/java/io/github/tap30/hiss/Hiss.java b/src/main/java/io/github/tap30/hiss/Hiss.java
index 8253ed0..29be267 100644
--- a/src/main/java/io/github/tap30/hiss/Hiss.java
+++ b/src/main/java/io/github/tap30/hiss/Hiss.java
@@ -6,6 +6,7 @@
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.Nullable;
+import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -17,21 +18,13 @@ public class Hiss {
private final HissObjectEncryptor hissObjectEncryptor;
Hiss(HissProperties hissProperties, KeyHashGenerator keyHashGenerator) {
+ Objects.requireNonNull(hissProperties);
+ Objects.requireNonNull(keyHashGenerator);
+
this.hissEncryptor = new HissEncryptor(hissProperties);
this.hissObjectEncryptor = new HissObjectEncryptor(this.hissEncryptor);
- logger.log(Level.INFO, "Hiss initialized:\n" +
- " Loaded Keys: {0}\n" +
- " Default Encryption Key ID: {1}\n" +
- " Default Encryption Algorithm: {2}\n" +
- " Default Hashing Key ID: {3}\n" +
- " Default Hashing Algorithm: {4}",
- new Object[]{
- hissProperties.getKeys().keySet(),
- hissProperties.getDefaultEncryptionKeyId(),
- hissProperties.getDefaultEncryptionAlgorithm(),
- hissProperties.getDefaultHashingKeyId(),
- hissProperties.getDefaultHashingAlgorithm()
- });
+
+ logHissInitialized(hissProperties);
if (hissProperties.isKeyHashGenerationEnabled()) {
keyHashGenerator.generateAndLogHashes(hissProperties.getKeys().values());
}
@@ -44,7 +37,7 @@ public class Hiss {
* @return encrypted content or null if the content is null.
*/
public String encrypt(@Nullable String content) {
- return this.encrypt(content, "");
+ return encrypt(content, "");
}
/**
@@ -56,7 +49,7 @@ public String encrypt(@Nullable String content) {
* @return encrypted content or null if the content is null.
*/
public String encrypt(@Nullable String content, @Language("regexp") @Nullable String pattern) {
- return this.hissEncryptor.encrypt(content, pattern);
+ return hissEncryptor.encrypt(content, pattern);
}
/**
@@ -69,7 +62,7 @@ public String encrypt(@Nullable String content, @Language("regexp") @Nullable St
* @throws IllegalArgumentException if key ID or algorithm is not loaded/supported.
*/
public String decrypt(@Nullable String content) {
- return this.hissEncryptor.decrypt(content);
+ return hissEncryptor.decrypt(content);
}
/**
@@ -79,7 +72,7 @@ public String decrypt(@Nullable String content) {
* @return hashed content or null if the content is null.
*/
public String hash(@Nullable String content) {
- return this.hash(content, "");
+ return hash(content, "");
}
/**
@@ -91,7 +84,7 @@ public String hash(@Nullable String content) {
* @return hashed content or null if the content is null.
*/
public String hash(@Nullable String content, @Language("regexp") @Nullable String pattern) {
- return this.hissEncryptor.hash(content, pattern);
+ return hissEncryptor.hash(content, pattern);
}
/**
@@ -120,7 +113,7 @@ public boolean isHashed(@Nullable String content) {
* @param object the annotated object; no action is taken on null objects.
*/
public void encryptObject(@Nullable Object object) {
- this.hissObjectEncryptor.encryptObject(object);
+ hissObjectEncryptor.encryptObject(object);
}
/**
@@ -129,7 +122,23 @@ public void encryptObject(@Nullable Object object) {
* @param object the annotated object; no action is taken on null objects.
*/
public void decryptObject(@Nullable Object object) {
- this.hissObjectEncryptor.decryptObject(object);
+ hissObjectEncryptor.decryptObject(object);
+ }
+
+ private void logHissInitialized(HissProperties hissProperties) {
+ logger.log(Level.INFO, "Hiss initialized:\n" +
+ " Loaded Keys: {0}\n" +
+ " Default Encryption Key ID: {1}\n" +
+ " Default Encryption Algorithm: {2}\n" +
+ " Default Hashing Key ID: {3}\n" +
+ " Default Hashing Algorithm: {4}",
+ new Object[]{
+ hissProperties.getKeys().keySet(),
+ hissProperties.getDefaultEncryptionKeyId(),
+ hissProperties.getDefaultEncryptionAlgorithm(),
+ hissProperties.getDefaultHashingKeyId(),
+ hissProperties.getDefaultHashingAlgorithm()
+ });
}
}
diff --git a/src/main/java/io/github/tap30/hiss/properties/HissProperties.java b/src/main/java/io/github/tap30/hiss/properties/HissProperties.java
index 4bb881f..dc27653 100644
--- a/src/main/java/io/github/tap30/hiss/properties/HissProperties.java
+++ b/src/main/java/io/github/tap30/hiss/properties/HissProperties.java
@@ -1,26 +1,21 @@
package io.github.tap30.hiss.properties;
import io.github.tap30.hiss.key.Key;
-import io.github.tap30.hiss.utils.StringUtils;
import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
import java.util.function.Supplier;
+import java.util.stream.Collectors;
public abstract class HissProperties {
private final Map properties = new HashMap<>();
public Map getKeys() {
- return this.getProperty("Keys", () -> {
- var keys = loadKeys();
- keys.forEach((k, v) -> {
- if (v != null && !StringUtils.hasText(v.getId())) {
- throw new RuntimeException("Key ID must not be empty");
- }
- });
- return keys;
- });
+ return this.getProperty("Keys", () -> loadKeys()
+ .stream()
+ .collect(Collectors.toMap(Key::getId, k -> k)));
}
public String getDefaultEncryptionKeyId() {
@@ -43,11 +38,16 @@ public boolean isKeyHashGenerationEnabled() {
return this.getProperty("KeyHashGenerationEnabled", this::loadKeyHashGenerationEnabled);
}
- protected abstract Map loadKeys();
+ protected abstract Set loadKeys();
+
protected abstract String loadDefaultEncryptionKeyId();
+
protected abstract String loadDefaultEncryptionAlgorithm();
+
protected abstract String loadDefaultHashingKeyId();
+
protected abstract String loadDefaultHashingAlgorithm();
+
protected abstract boolean loadKeyHashGenerationEnabled();
@SuppressWarnings("unchecked")
diff --git a/src/main/java/io/github/tap30/hiss/properties/HissPropertiesFromEnv.java b/src/main/java/io/github/tap30/hiss/properties/HissPropertiesFromEnv.java
index 7dd172a..c7e013f 100644
--- a/src/main/java/io/github/tap30/hiss/properties/HissPropertiesFromEnv.java
+++ b/src/main/java/io/github/tap30/hiss/properties/HissPropertiesFromEnv.java
@@ -1,11 +1,11 @@
package io.github.tap30.hiss.properties;
import io.github.tap30.hiss.key.Key;
-import lombok.Setter;
import java.util.Base64;
-import java.util.HashMap;
+import java.util.HashSet;
import java.util.Map;
+import java.util.Set;
import java.util.function.Supplier;
/**
@@ -30,19 +30,17 @@ public class HissPropertiesFromEnv extends HissProperties {
private static final String KEY_ENV_PREFIX = "HISS_KEYS_";
private static final String KEY_HASH_ENV_POSTFIX = "___HASH";
- @Setter // todo: make it package private
- private Supplier