diff --git a/src/main/java/org/qortal/arbitrary/ArbitraryDataDigest.java b/src/main/java/org/qortal/arbitrary/ArbitraryDataDigest.java index b07fd396..004bda6e 100644 --- a/src/main/java/org/qortal/arbitrary/ArbitraryDataDigest.java +++ b/src/main/java/org/qortal/arbitrary/ArbitraryDataDigest.java @@ -4,6 +4,7 @@ import org.qortal.repository.DataException; import org.qortal.utils.Base58; import java.io.IOException; +import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.Files; @@ -25,47 +26,53 @@ public class ArbitraryDataDigest { } public void compute() throws IOException, DataException { - List allPaths = Files.walk(path).filter(Files::isRegularFile).sorted().collect(Collectors.toList()); + List allPaths = Files.walk(path) + .filter(Files::isRegularFile) + .sorted() + .collect(Collectors.toList()); + Path basePathAbsolute = this.path.toAbsolutePath(); - + MessageDigest sha256; try { sha256 = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { throw new DataException("SHA-256 hashing algorithm unavailable"); } - + for (Path path : allPaths) { // We need to work with paths relative to the base path, to ensure the same hash // is generated on different systems Path relativePath = basePathAbsolute.relativize(path.toAbsolutePath()); - + // Exclude Qortal folder since it can be different each time // We only care about hashing the actual user data if (relativePath.startsWith(".qortal/")) { continue; } - + // Account for \ VS / : Linux VS Windows String pathString = relativePath.toString(); - - if(relativePath.getFileSystem().toString().contains("Windows")) { - pathString = pathString.replace("\\","/"); + if (relativePath.getFileSystem().toString().contains("Windows")) { + pathString = pathString.replace("\\", "/"); } - + // Hash path byte[] filePathBytes = pathString.getBytes(StandardCharsets.UTF_8); - System.out.printf("Path: %s \n", pathString); - System.out.printf("Path Byte Array: %s \n", Arrays.toString(filePathBytes)); sha256.update(filePathBytes); - - // Hash contents - byte[] fileContent = Files.readAllBytes(path); - System.out.printf("File Content: %s \n", Arrays.toString(fileContent)); - sha256.update(fileContent); + + try (InputStream in = Files.newInputStream(path)) { + byte[] buffer = new byte[65536]; // 64 KB + int bytesRead; + while ((bytesRead = in.read(buffer)) != -1) { + sha256.update(buffer, 0, bytesRead); + } + } } + this.hash = sha256.digest(); } + public boolean isHashValid(byte[] hash) { return Arrays.equals(hash, this.hash);