diff --git a/src/main/java/io/github/tap30/hiss/HissEncryptor.java b/src/main/java/io/github/tap30/hiss/HissEncryptor.java index 72e5c15..8090e19 100644 --- a/src/main/java/io/github/tap30/hiss/HissEncryptor.java +++ b/src/main/java/io/github/tap30/hiss/HissEncryptor.java @@ -33,7 +33,7 @@ public HissEncryptor(Set encryptors, String defaultEncryptionAlgorithm, String defaultEncryptionKeyId) { Objects.requireNonNull(encryptors); - this.encryptors = encryptors.stream().collect(Collectors.toMap(Encryptor::getName, e -> e)); + this.encryptors = encryptors.stream().collect(Collectors.toMap(e -> e.getName().toLowerCase(), e -> e)); this.keys = Objects.requireNonNull(keys); this.defaultEncryptionAlgorithm = StringUtils.requireNonBlank(defaultEncryptionAlgorithm); this.defaultEncryptionKeyId = StringUtils.requireNonBlank(defaultEncryptionKeyId); diff --git a/src/main/java/io/github/tap30/hiss/HissHasher.java b/src/main/java/io/github/tap30/hiss/HissHasher.java index db7334e..097c5eb 100644 --- a/src/main/java/io/github/tap30/hiss/HissHasher.java +++ b/src/main/java/io/github/tap30/hiss/HissHasher.java @@ -25,7 +25,7 @@ public HissHasher(Set hashers, Map keys, String defaultHashingAlgorithm, String defaultHashingKeyId) { - this.hashers = hashers.stream().collect(Collectors.toMap(Hasher::getName, h -> h)); + this.hashers = hashers.stream().collect(Collectors.toMap(h -> h.getName().toLowerCase(), h -> h)); this.keys = keys; this.defaultHashingAlgorithm = defaultHashingAlgorithm; this.defaultHashingKeyId = defaultHashingKeyId; diff --git a/src/main/java/io/github/tap30/hiss/encryptor/Encryptor.java b/src/main/java/io/github/tap30/hiss/encryptor/Encryptor.java index 7f88066..7345084 100644 --- a/src/main/java/io/github/tap30/hiss/encryptor/Encryptor.java +++ b/src/main/java/io/github/tap30/hiss/encryptor/Encryptor.java @@ -3,16 +3,12 @@ public interface Encryptor { /** * Encrypts content using key. - * @param key - * @param content * @return encrypted content. */ byte[] encrypt(byte[] key, byte[] content) throws Exception; /** * Decrypts content using key - * @param key - * @param content * @return plain content. */ byte[] decrypt(byte[] key, byte[] content) throws Exception; @@ -24,5 +20,5 @@ public interface Encryptor { * '{', '}', ':', and '#$$#'. * @return encryptor name. */ - String getName(); // todo: names should be lowercased? + String getName(); } diff --git a/src/main/java/io/github/tap30/hiss/hasher/Hasher.java b/src/main/java/io/github/tap30/hiss/hasher/Hasher.java index 99c3f33..866fbdc 100644 --- a/src/main/java/io/github/tap30/hiss/hasher/Hasher.java +++ b/src/main/java/io/github/tap30/hiss/hasher/Hasher.java @@ -3,8 +3,6 @@ public interface Hasher { /** * Calculates hash of provided content. - * @param key - * @param content * @return hash of content. */ byte[] hash(byte[] key, byte[] content) throws Exception; @@ -16,5 +14,5 @@ public interface Hasher { * '{', '}', ':', and '#$$#'. * @return hasher name. */ - String getName(); // todo: should names be lowercased? + String getName(); } 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 dc27653..cc66c35 100644 --- a/src/main/java/io/github/tap30/hiss/properties/HissProperties.java +++ b/src/main/java/io/github/tap30/hiss/properties/HissProperties.java @@ -23,7 +23,7 @@ public String getDefaultEncryptionKeyId() { } public String getDefaultEncryptionAlgorithm() { - return this.getProperty("DefaultEncryptionAlgorithm", this::loadDefaultEncryptionAlgorithm); + return this.getProperty("DefaultEncryptionAlgorithm", () -> loadDefaultEncryptionAlgorithm().toLowerCase()); } public String getDefaultHashingKeyId() { @@ -31,7 +31,7 @@ public String getDefaultHashingKeyId() { } public String getDefaultHashingAlgorithm() { - return this.getProperty("DefaultHashingAlgorithm", this::loadDefaultHashingAlgorithm); + return this.getProperty("DefaultHashingAlgorithm", () -> loadDefaultHashingAlgorithm().toLowerCase()); } public boolean isKeyHashGenerationEnabled() { diff --git a/src/test/java/io/github/tap30/hiss/encryptor/BaseEncryptorTest.java b/src/test/java/io/github/tap30/hiss/encryptor/BaseEncryptorTest.java new file mode 100644 index 0000000..2daaaf3 --- /dev/null +++ b/src/test/java/io/github/tap30/hiss/encryptor/BaseEncryptorTest.java @@ -0,0 +1,74 @@ +package io.github.tap30.hiss.encryptor; + +import org.junit.jupiter.api.Test; + +import java.util.Base64; + +import static org.junit.jupiter.api.Assertions.*; + +public abstract class BaseEncryptorTest { + + protected final Encryptor encryptor; + protected final String encryptorName; + protected final byte[] key; + + protected final String plainText = "some text"; + protected final byte[] plainTextBytes = plainText.getBytes(); + + protected final String encodedEncryptedText; + protected final byte[] encryptedTextBytes; + + protected BaseEncryptorTest(Encryptor encryptor, + String encryptorName, + byte[] key, + String encodedEncryptedText) { + this.encryptor = encryptor; + this.encryptorName = encryptorName; + this.key = key; + this.encodedEncryptedText = encodedEncryptedText; + this.encryptedTextBytes = base64(encodedEncryptedText); + } + + protected String base64(byte[] bytes) { + return Base64.getEncoder().encodeToString(bytes); + } + + protected byte[] base64(String base64) { + return Base64.getDecoder().decode(base64); + } + + @Test + void testEncrypt() throws Exception { + // When + var encrypted = encryptor.encrypt(key, plainTextBytes); + + // Then + assertNotEquals(plainTextBytes, encrypted); + System.out.printf("Base64 Encoded Encrypted Content: %s\n", base64(encrypted)); + } + + @Test + void testDecrypt() throws Exception { + // When + var plain = encryptor.decrypt(key, encryptedTextBytes); + + // Then + assertArrayEquals(plainTextBytes, plain); + } + + @Test + void testEncryptAndDecrypt() throws Exception { + // When + var encrypted = encryptor.encrypt(key, plainTextBytes); + var plain = encryptor.decrypt(key, encrypted); + + // Then + assertArrayEquals(plainTextBytes, plain); + } + + @Test + void testGetName() { + assertEquals(encryptorName, encryptor.getName()); + } + +} \ No newline at end of file diff --git a/src/test/java/io/github/tap30/hiss/encryptor/impl/AesCbcPkcs5PaddingEncryptorTest.java b/src/test/java/io/github/tap30/hiss/encryptor/impl/AesCbcPkcs5PaddingEncryptorTest.java new file mode 100644 index 0000000..9a93739 --- /dev/null +++ b/src/test/java/io/github/tap30/hiss/encryptor/impl/AesCbcPkcs5PaddingEncryptorTest.java @@ -0,0 +1,16 @@ +package io.github.tap30.hiss.encryptor.impl; + +import io.github.tap30.hiss.encryptor.BaseEncryptorTest; + +class AesCbcPkcs5PaddingEncryptorTest extends BaseEncryptorTest { + + protected AesCbcPkcs5PaddingEncryptorTest() { + super( + new AesCbcPkcs5PaddingEncryptor(), + "AES/CBC/PKCS5Padding", + new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, + "bzoCDPV5ddz6GEOm1PRt4V9nQJs4Dc6xRFcMea5xB9I=" + ); + } + +} \ No newline at end of file diff --git a/src/test/java/io/github/tap30/hiss/encryptor/impl/AesGcmNoPaddingEncryptorTest.java b/src/test/java/io/github/tap30/hiss/encryptor/impl/AesGcmNoPaddingEncryptorTest.java new file mode 100644 index 0000000..ceec29d --- /dev/null +++ b/src/test/java/io/github/tap30/hiss/encryptor/impl/AesGcmNoPaddingEncryptorTest.java @@ -0,0 +1,16 @@ +package io.github.tap30.hiss.encryptor.impl; + +import io.github.tap30.hiss.encryptor.BaseEncryptorTest; + +class AesGcmNoPaddingEncryptorTest extends BaseEncryptorTest { + + protected AesGcmNoPaddingEncryptorTest() { + super( + new AesGcmNoPaddingEncryptor(), + "AES/GCM/NoPadding", + new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, + "2TtYw+dUzrPOPmvgorLoJAWSgXMDbrmz4BvcFA4+wnX1P6661DlbgrI=" + ); + } + +} \ No newline at end of file diff --git a/src/test/java/io/github/tap30/hiss/encryptor/impl/TapsiAesCbcEncryptorTest.java b/src/test/java/io/github/tap30/hiss/encryptor/impl/TapsiAesCbcEncryptorTest.java new file mode 100644 index 0000000..aa20cfd --- /dev/null +++ b/src/test/java/io/github/tap30/hiss/encryptor/impl/TapsiAesCbcEncryptorTest.java @@ -0,0 +1,16 @@ +package io.github.tap30.hiss.encryptor.impl; + +import io.github.tap30.hiss.encryptor.BaseEncryptorTest; + +class TapsiAesCbcEncryptorTest extends BaseEncryptorTest { + + protected TapsiAesCbcEncryptorTest() { + super( + new TapsiAesCbcEncryptor(), + "aes-128-cbc", + new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, + "bzoCDPV5ddz6GEOm1PRt4V9nQJs4Dc6xRFcMea5xB9I=" + ); + } + +} \ No newline at end of file diff --git a/src/test/java/io/github/tap30/hiss/encryptor/impl/TapsiAesGcmEncryptorTest.java b/src/test/java/io/github/tap30/hiss/encryptor/impl/TapsiAesGcmEncryptorTest.java new file mode 100644 index 0000000..2f33866 --- /dev/null +++ b/src/test/java/io/github/tap30/hiss/encryptor/impl/TapsiAesGcmEncryptorTest.java @@ -0,0 +1,16 @@ +package io.github.tap30.hiss.encryptor.impl; + +import io.github.tap30.hiss.encryptor.BaseEncryptorTest; + +class TapsiAesGcmEncryptorTest extends BaseEncryptorTest { + + protected TapsiAesGcmEncryptorTest() { + super( + new TapsiAesGcmEncryptor(), + "aes-128-gcm", + new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, + "2TtYw+dUzrPOPmvgorLoJAWSgXMDbrmz4BvcFA4+wnX1P6661DlbgrI=" + ); + } + +} \ No newline at end of file diff --git a/src/test/java/io/github/tap30/hiss/hasher/BaseHasherTest.java b/src/test/java/io/github/tap30/hiss/hasher/BaseHasherTest.java new file mode 100644 index 0000000..68c3fe1 --- /dev/null +++ b/src/test/java/io/github/tap30/hiss/hasher/BaseHasherTest.java @@ -0,0 +1,66 @@ +package io.github.tap30.hiss.hasher; + +import org.junit.jupiter.api.Test; + +import java.util.Base64; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public abstract class BaseHasherTest { + + protected final Hasher hasher; + protected final String hasherName; + protected final byte[] key; + + protected final String plainText = "some text"; + protected final byte[] plainTextBytes = plainText.getBytes(); + + protected final String encodedHashedText; + protected final byte[] hashedTextBytes; + + protected BaseHasherTest(Hasher hasher, String hasherName, byte[] key, String encodedHashedText) { + this.hasher = hasher; + this.hasherName = hasherName; + this.key = key; + this.encodedHashedText = encodedHashedText; + this.hashedTextBytes = base64(encodedHashedText); + } + + protected String base64(byte[] bytes) { + return Base64.getEncoder().encodeToString(bytes); + } + + protected byte[] base64(String base64) { + return Base64.getDecoder().decode(base64); + } + + @Test + void testHash() throws Exception { + // When + var hash = hasher.hash(key, plainTextBytes); + + // Then + System.out.println(base64(hash)); + assertArrayEquals(hashedTextBytes, hash); + } + + @Test + void testHash_producesSameHashForSameInput() throws Exception { + // When + var hash1 = hasher.hash(key, plainTextBytes); + var hash2 = hasher.hash(key, plainTextBytes); + var hash3 = hasher.hash(key, plainTextBytes); + + // Then + assertArrayEquals(hashedTextBytes, hash1); + assertArrayEquals(hashedTextBytes, hash2); + assertArrayEquals(hashedTextBytes, hash3); + } + + @Test + void testGetName() { + assertEquals(hasherName, hasher.getName()); + } + +} \ No newline at end of file diff --git a/src/test/java/io/github/tap30/hiss/hasher/impl/HmacSha256HasherTest.java b/src/test/java/io/github/tap30/hiss/hasher/impl/HmacSha256HasherTest.java new file mode 100644 index 0000000..cf0161d --- /dev/null +++ b/src/test/java/io/github/tap30/hiss/hasher/impl/HmacSha256HasherTest.java @@ -0,0 +1,15 @@ +package io.github.tap30.hiss.hasher.impl; + +import io.github.tap30.hiss.hasher.BaseHasherTest; + +class HmacSha256HasherTest extends BaseHasherTest { + + public HmacSha256HasherTest() { + super( + new HmacSha256Hasher(), + "HmacSHA256", + new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}, + "ZjSgZLB+ebSU/dD72P6HULVSl6HoRFIEZNoYP9aqIRU=" + ); + } +} \ No newline at end of file diff --git a/src/test/java/io/github/tap30/hiss/hasher/impl/TapsiHmacSha256HasherTest.java b/src/test/java/io/github/tap30/hiss/hasher/impl/TapsiHmacSha256HasherTest.java new file mode 100644 index 0000000..b473d3c --- /dev/null +++ b/src/test/java/io/github/tap30/hiss/hasher/impl/TapsiHmacSha256HasherTest.java @@ -0,0 +1,16 @@ +package io.github.tap30.hiss.hasher.impl; + +import io.github.tap30.hiss.hasher.BaseHasherTest; + +class TapsiHmacSha256HasherTest extends BaseHasherTest { + + public TapsiHmacSha256HasherTest() { + super( + new TapsiHmacSha256Hasher(), + "hmac-sha256", + new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}, + "ZjSgZLB+ebSU/dD72P6HULVSl6HoRFIEZNoYP9aqIRU=" + ); + } + +} \ No newline at end of file