Fixed bugs when computing directory digest. The order was being lost when adding to the second List, which was producing different hashes on different systems. It also doesn't make sense to convert paths to lowercase, given that we want our validation to be case sensitive.

This commit is contained in:
CalDescent 2021-08-18 07:38:08 +01:00
parent 1968496ce1
commit c3b44cee94

View File

@ -10,9 +10,10 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class ArbitraryDataDigest {
@ -26,8 +27,8 @@ public class ArbitraryDataDigest {
}
public void compute() throws IOException {
List<Path> allPaths = new ArrayList<>();
Files.walk(path).filter(Files::isRegularFile).forEachOrdered(p -> allPaths.add(p));
List<Path> allPaths = Files.walk(path).filter(Files::isRegularFile).collect(Collectors.toList());
Collections.sort(allPaths);
Path basePathAbsolute = this.path.toAbsolutePath();
MessageDigest sha256 = null;
@ -49,7 +50,7 @@ public class ArbitraryDataDigest {
}
// Hash path
byte[] filePathBytes = relativePath.toString().toLowerCase().getBytes(StandardCharsets.UTF_8);
byte[] filePathBytes = relativePath.toString().getBytes(StandardCharsets.UTF_8);
sha256.update(filePathBytes);
// Hash contents