[#6] cover the cryptography module with junit tests

Signed-off-by: Ori Bruk <o.bruk@yadro.com>
This commit is contained in:
Ori Bruk 2024-08-21 10:57:47 +03:00 committed by Bruk Ori
parent 0d76332f01
commit c9a54d56fb
8 changed files with 432 additions and 34 deletions

View file

@ -11,6 +11,8 @@ import java.security.NoSuchAlgorithmException;
import static java.util.Objects.isNull;
public class Helper {
private static final String ERROR_MESSAGE = "Input value is missing";
private static final String SHA256 = "SHA-256";
private static final int RIPEMD_160_HASH_BYTE_LENGTH = 20;
private Helper() {
@ -18,7 +20,7 @@ public class Helper {
public static byte[] getRipemd160(byte[] value) {
if (isNull(value)) {
throw new IllegalArgumentException("Input value is missing");
throw new IllegalArgumentException(ERROR_MESSAGE);
}
var hash = new byte[RIPEMD_160_HASH_BYTE_LENGTH];
@ -30,7 +32,7 @@ public class Helper {
public static MessageDigest getSha256Instance() {
try {
return MessageDigest.getInstance("SHA-256");
return MessageDigest.getInstance(SHA256);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
@ -38,7 +40,7 @@ public class Helper {
public static byte[] getSha256(byte[] value) {
if (isNull(value)) {
throw new IllegalArgumentException("Input value is missing");
throw new IllegalArgumentException(ERROR_MESSAGE);
}
return getSha256Instance().digest(value);
@ -46,15 +48,15 @@ public class Helper {
public static ByteString getSha256(Message value) {
if (isNull(value)) {
throw new IllegalArgumentException("Input value is missing");
throw new IllegalArgumentException(ERROR_MESSAGE);
}
return ByteString.copyFrom(getSha256(value.toByteArray()));
}
public static String getHexString(byte[] value) {
if (isNull(value)) {
throw new IllegalArgumentException("Input value is missing");
if (isNull(value) || value.length == 0) {
throw new IllegalArgumentException(ERROR_MESSAGE);
}
return String.format("%0" + (value.length << 1) + "x", new BigInteger(1, value));