Skip to content

Commit

Permalink
Add encryptors and hashers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mkay1375 committed Aug 13, 2024
1 parent 0940976 commit 50a7fbc
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/github/tap30/hiss/HissEncryptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public HissEncryptor(Set<Encryptor> 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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/tap30/hiss/HissHasher.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public HissHasher(Set<Hasher> hashers,
Map<String, Key> 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;
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/io/github/tap30/hiss/encryptor/Encryptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,5 +20,5 @@ public interface Encryptor {
* <code>'{'</code>, <code>'}'</code>, <code>':'</code>, and <code>'#$$#'</code>.
* @return encryptor name.
*/
String getName(); // todo: names should be lowercased?
String getName();
}
4 changes: 1 addition & 3 deletions src/main/java/io/github/tap30/hiss/hasher/Hasher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,5 +14,5 @@ public interface Hasher {
* <code>'{'</code>, <code>'}'</code>, <code>':'</code>, and <code>'#$$#'</code>.
* @return hasher name.
*/
String getName(); // todo: should names be lowercased?
String getName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public String getDefaultEncryptionKeyId() {
}

public String getDefaultEncryptionAlgorithm() {
return this.getProperty("DefaultEncryptionAlgorithm", this::loadDefaultEncryptionAlgorithm);
return this.getProperty("DefaultEncryptionAlgorithm", () -> loadDefaultEncryptionAlgorithm().toLowerCase());
}

public String getDefaultHashingKeyId() {
return this.getProperty("DefaultHashingKeyId", this::loadDefaultHashingKeyId);
}

public String getDefaultHashingAlgorithm() {
return this.getProperty("DefaultHashingAlgorithm", this::loadDefaultHashingAlgorithm);
return this.getProperty("DefaultHashingAlgorithm", () -> loadDefaultHashingAlgorithm().toLowerCase());
}

public boolean isKeyHashGenerationEnabled() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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="
);
}

}
Original file line number Diff line number Diff line change
@@ -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="
);
}

}
Original file line number Diff line number Diff line change
@@ -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="
);
}

}
Original file line number Diff line number Diff line change
@@ -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="
);
}

}
66 changes: 66 additions & 0 deletions src/test/java/io/github/tap30/hiss/hasher/BaseHasherTest.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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="
);
}
}
Original file line number Diff line number Diff line change
@@ -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="
);
}

}

0 comments on commit 50a7fbc

Please sign in to comment.