Merge pull request #9 from Philreact/master-kenny3

fix in digest, was putting whole file in memory.
This commit is contained in:
kennycud 2025-06-01 10:43:35 -07:00 committed by GitHub
commit 7ccd06e5c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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,7 +26,11 @@ public class ArbitraryDataDigest {
}
public void compute() throws IOException, DataException {
List<Path> allPaths = Files.walk(path).filter(Files::isRegularFile).sorted().collect(Collectors.toList());
List<Path> allPaths = Files.walk(path)
.filter(Files::isRegularFile)
.sorted()
.collect(Collectors.toList());
Path basePathAbsolute = this.path.toAbsolutePath();
MessageDigest sha256;
@ -48,25 +53,27 @@ public class ArbitraryDataDigest {
// 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);
}