mirror of
https://github.com/Qortal/qortal.git
synced 2025-07-22 04:14:18 +00:00
Merge pull request #9 from Philreact/master-kenny3
fix in digest, was putting whole file in memory.
This commit is contained in:
commit
7ccd06e5c3
@ -4,6 +4,7 @@ import org.qortal.repository.DataException;
|
|||||||
import org.qortal.utils.Base58;
|
import org.qortal.utils.Base58;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.FileSystem;
|
import java.nio.file.FileSystem;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@ -25,47 +26,53 @@ public class ArbitraryDataDigest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void compute() throws IOException, DataException {
|
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();
|
Path basePathAbsolute = this.path.toAbsolutePath();
|
||||||
|
|
||||||
MessageDigest sha256;
|
MessageDigest sha256;
|
||||||
try {
|
try {
|
||||||
sha256 = MessageDigest.getInstance("SHA-256");
|
sha256 = MessageDigest.getInstance("SHA-256");
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
throw new DataException("SHA-256 hashing algorithm unavailable");
|
throw new DataException("SHA-256 hashing algorithm unavailable");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Path path : allPaths) {
|
for (Path path : allPaths) {
|
||||||
// We need to work with paths relative to the base path, to ensure the same hash
|
// We need to work with paths relative to the base path, to ensure the same hash
|
||||||
// is generated on different systems
|
// is generated on different systems
|
||||||
Path relativePath = basePathAbsolute.relativize(path.toAbsolutePath());
|
Path relativePath = basePathAbsolute.relativize(path.toAbsolutePath());
|
||||||
|
|
||||||
// Exclude Qortal folder since it can be different each time
|
// Exclude Qortal folder since it can be different each time
|
||||||
// We only care about hashing the actual user data
|
// We only care about hashing the actual user data
|
||||||
if (relativePath.startsWith(".qortal/")) {
|
if (relativePath.startsWith(".qortal/")) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Account for \ VS / : Linux VS Windows
|
// Account for \ VS / : Linux VS Windows
|
||||||
String pathString = relativePath.toString();
|
String pathString = relativePath.toString();
|
||||||
|
if (relativePath.getFileSystem().toString().contains("Windows")) {
|
||||||
if(relativePath.getFileSystem().toString().contains("Windows")) {
|
pathString = pathString.replace("\\", "/");
|
||||||
pathString = pathString.replace("\\","/");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash path
|
// Hash path
|
||||||
byte[] filePathBytes = pathString.getBytes(StandardCharsets.UTF_8);
|
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);
|
sha256.update(filePathBytes);
|
||||||
|
|
||||||
// Hash contents
|
try (InputStream in = Files.newInputStream(path)) {
|
||||||
byte[] fileContent = Files.readAllBytes(path);
|
byte[] buffer = new byte[65536]; // 64 KB
|
||||||
System.out.printf("File Content: %s \n", Arrays.toString(fileContent));
|
int bytesRead;
|
||||||
sha256.update(fileContent);
|
while ((bytesRead = in.read(buffer)) != -1) {
|
||||||
|
sha256.update(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.hash = sha256.digest();
|
this.hash = sha256.digest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean isHashValid(byte[] hash) {
|
public boolean isHashValid(byte[] hash) {
|
||||||
return Arrays.equals(hash, this.hash);
|
return Arrays.equals(hash, this.hash);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user