mirror of
https://github.com/Qortal/qortal.git
synced 2025-07-19 02:41:22 +00:00
in memory to stream
This commit is contained in:
parent
fa8b9f2cee
commit
517f7b92d5
@ -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<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;
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user