diff --git a/impl/src/main/java/de/fitko/fitconnect/impl/crypto/MetadataVerifier.java b/impl/src/main/java/de/fitko/fitconnect/impl/crypto/HashUtil.java similarity index 72% rename from impl/src/main/java/de/fitko/fitconnect/impl/crypto/MetadataVerifier.java rename to impl/src/main/java/de/fitko/fitconnect/impl/crypto/HashUtil.java index 192e99ed7e1614994ff785becb5f1aca08c49dcf..94e6daa2c7a8af579a943ccfdcde8278c1fb520a 100644 --- a/impl/src/main/java/de/fitko/fitconnect/impl/crypto/MetadataVerifier.java +++ b/impl/src/main/java/de/fitko/fitconnect/impl/crypto/HashUtil.java @@ -2,16 +2,20 @@ package de.fitko.fitconnect.impl.crypto; import de.fitko.fitconnect.api.exceptions.internal.InitializationException; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -public class MetadataVerifier { +public class HashUtil { private static final String DEFAULT_ALGORITHM = "SHA-512"; // Currently, only SHA-512 is supported. private final MessageDigest messageDigest; - public MetadataVerifier() { + public HashUtil() { try { this.messageDigest = MessageDigest.getInstance(DEFAULT_ALGORITHM); } catch (NoSuchAlgorithmException e) { @@ -19,7 +23,7 @@ public class MetadataVerifier { } } - public MetadataVerifier(final MessageDigest messageDigest) { + public HashUtil(final MessageDigest messageDigest) { this.messageDigest = messageDigest; } @@ -33,6 +37,15 @@ public class MetadataVerifier { return compareHashes(originalHash, newHash); } + public String createHashFromFile(File file) throws IOException { + try { + byte[] rawData = Files.readAllBytes(Paths.get(file.getPath())); + return new String(rawData); + } catch (IOException e) { + throw e; + } + } + private boolean compareHashes(byte[] originalHash, byte[] comparisonHash) { int diff = originalHash.length ^ comparisonHash.length; for (int i = 0; i < originalHash.length && i < comparisonHash.length; i++) { @@ -40,4 +53,5 @@ public class MetadataVerifier { } return diff == 0; } + } diff --git a/impl/src/test/java/fitconnect/impl/crypto/MetadataVerifierTest.java b/impl/src/test/java/fitconnect/impl/crypto/HashUtilTest.java similarity index 77% rename from impl/src/test/java/fitconnect/impl/crypto/MetadataVerifierTest.java rename to impl/src/test/java/fitconnect/impl/crypto/HashUtilTest.java index 50f7da798f1e02fd903e4501e7ba1c8e803e9b04..03217768575bef9a01d80537822d617a4438d2b2 100644 --- a/impl/src/test/java/fitconnect/impl/crypto/MetadataVerifierTest.java +++ b/impl/src/test/java/fitconnect/impl/crypto/HashUtilTest.java @@ -1,15 +1,15 @@ package fitconnect.impl.crypto; -import de.fitko.fitconnect.impl.crypto.MetadataVerifier; +import de.fitko.fitconnect.impl.crypto.HashUtil; import org.junit.jupiter.api.Test; import java.io.IOException; import static org.junit.jupiter.api.Assertions.*; -class MetadataVerifierTest { +class HashUtilTest { - MetadataVerifier underTest = new MetadataVerifier(); + HashUtil underTest = new HashUtil(); @Test public void testMessageDigestForDataAndAttachments() throws IOException { @@ -24,7 +24,7 @@ class MetadataVerifierTest { } private byte[] getExampleFileContent(final String path) throws IOException { - return MetadataVerifier.class.getResourceAsStream(path).readAllBytes(); + return HashUtil.class.getResourceAsStream(path).readAllBytes(); } } \ No newline at end of file