Skip to content
Snippets Groups Projects
Commit 5a691ec3 authored by Martin Vogel's avatar Martin Vogel
Browse files

#414 Extend util by hashFromFile

parent cd912069
No related branches found
No related tags found
2 merge requests!2#414 Remaining changes from MR,!1planning#414 Methoden Signaturen (Zwischenstand)
...@@ -2,16 +2,20 @@ package de.fitko.fitconnect.impl.crypto; ...@@ -2,16 +2,20 @@ package de.fitko.fitconnect.impl.crypto;
import de.fitko.fitconnect.api.exceptions.internal.InitializationException; 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.MessageDigest;
import java.security.NoSuchAlgorithmException; 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 static final String DEFAULT_ALGORITHM = "SHA-512"; // Currently, only SHA-512 is supported.
private final MessageDigest messageDigest; private final MessageDigest messageDigest;
public MetadataVerifier() { public HashUtil() {
try { try {
this.messageDigest = MessageDigest.getInstance(DEFAULT_ALGORITHM); this.messageDigest = MessageDigest.getInstance(DEFAULT_ALGORITHM);
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
...@@ -19,7 +23,7 @@ public class MetadataVerifier { ...@@ -19,7 +23,7 @@ public class MetadataVerifier {
} }
} }
public MetadataVerifier(final MessageDigest messageDigest) { public HashUtil(final MessageDigest messageDigest) {
this.messageDigest = messageDigest; this.messageDigest = messageDigest;
} }
...@@ -33,6 +37,15 @@ public class MetadataVerifier { ...@@ -33,6 +37,15 @@ public class MetadataVerifier {
return compareHashes(originalHash, newHash); 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) { private boolean compareHashes(byte[] originalHash, byte[] comparisonHash) {
int diff = originalHash.length ^ comparisonHash.length; int diff = originalHash.length ^ comparisonHash.length;
for (int i = 0; i < originalHash.length && i < comparisonHash.length; i++) { for (int i = 0; i < originalHash.length && i < comparisonHash.length; i++) {
...@@ -40,4 +53,5 @@ public class MetadataVerifier { ...@@ -40,4 +53,5 @@ public class MetadataVerifier {
} }
return diff == 0; return diff == 0;
} }
} }
package fitconnect.impl.crypto; 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 org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class MetadataVerifierTest { class HashUtilTest {
MetadataVerifier underTest = new MetadataVerifier(); HashUtil underTest = new HashUtil();
@Test @Test
public void testMessageDigestForDataAndAttachments() throws IOException { public void testMessageDigestForDataAndAttachments() throws IOException {
...@@ -24,7 +24,7 @@ class MetadataVerifierTest { ...@@ -24,7 +24,7 @@ class MetadataVerifierTest {
} }
private byte[] getExampleFileContent(final String path) throws IOException { 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment