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;
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;
}
}
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
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