-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
241 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/test/java/io/github/tap30/hiss/encryptor/BaseEncryptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/test/java/io/github/tap30/hiss/encryptor/impl/AesCbcPkcs5PaddingEncryptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=" | ||
); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/test/java/io/github/tap30/hiss/encryptor/impl/AesGcmNoPaddingEncryptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=" | ||
); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/test/java/io/github/tap30/hiss/encryptor/impl/TapsiAesCbcEncryptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=" | ||
); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/test/java/io/github/tap30/hiss/encryptor/impl/TapsiAesGcmEncryptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
66
src/test/java/io/github/tap30/hiss/hasher/BaseHasherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/test/java/io/github/tap30/hiss/hasher/impl/HmacSha256HasherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=" | ||
); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/test/java/io/github/tap30/hiss/hasher/impl/TapsiHmacSha256HasherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=" | ||
); | ||
} | ||
|
||
} |