Commit c4295c3b authored by Claus Nagel's avatar Claus Nagel
Browse files

ignore file endings when calculating MD5 hash

parent 2ddec274
...@@ -37,7 +37,6 @@ import org.kohsuke.args4j.Option; ...@@ -37,7 +37,6 @@ import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.ParserProperties; import org.kohsuke.args4j.ParserProperties;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
...@@ -282,9 +281,9 @@ public class ADESchemaCompiler { ...@@ -282,9 +281,9 @@ public class ADESchemaCompiler {
log(LogLevel.INFO, "Running in strict mode. Checking sanity of subfolder 'schemas'"); log(LogLevel.INFO, "Running in strict mode. Checking sanity of subfolder 'schemas'");
BigInteger md5 = new BigInteger("0"); BigInteger md5 = new BigInteger("0");
md5 = Util.dir2md5(schemaDir.toFile(), md5); md5 = Util.dir2md5(schemaDir, md5);
if (!md5.toString(16).equals("94af5d7979d484146bc32810ab6c1577")) if (!md5.toString(16).equals("bb750e0a6e76c779637ebc2dc983253a"))
throw new Exception("Contents of subfolder 'schemas' have been altered. Please restore its original state."); throw new Exception("Contents of subfolder 'schemas' have been altered. Please restore its original state.");
} }
......
...@@ -20,58 +20,54 @@ ...@@ -20,58 +20,54 @@
*/ */
package org.citygml4j.ade_xjc; package org.citygml4j.ade_xjc;
import java.io.File; import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStreamReader;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Set;
import java.util.TreeSet;
public class Util { public class Util {
public static BigInteger dir2md5(File file, BigInteger md5) throws NoSuchAlgorithmException { public static BigInteger dir2md5(Path dir, BigInteger md5) throws IOException, NoSuchAlgorithmException {
md5 = string2md5(file.getName(), md5); // find schema files and make sure that iteration order is deterministic
Set<Path> files = new TreeSet<>((p1, p2) -> p1.getFileName().toString().compareTo(p2.getFileName().toString()));
if (file.isDirectory()) { Files.walk(dir)
File[] children = file.listFiles(pathname -> pathname.isDirectory() .filter(p -> Files.isDirectory(p)
|| pathname.getName().endsWith("xsd") || p.getFileName().toString().endsWith(".xsd")
|| pathname.getName().endsWith("xjb")); || p.getFileName().toString().endsWith(".xjb"))
.forEach(files::add);
if (children != null) {
for (File next : children) for (Path file : files) {
md5 = dir2md5(next, md5); md5 = string2md5(file.getFileName().toString(), md5);
if (Files.isRegularFile(file))
md5 = file2md5(file, md5);
} }
} else
md5 = file2md5(file.getAbsolutePath(), md5);
return md5; return md5;
} }
private static BigInteger file2md5(String fileName, BigInteger md5) throws NoSuchAlgorithmException { private static BigInteger file2md5(Path file, BigInteger md5) throws IOException, NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("MD5"); MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[8192]; try (BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(file)))) {
int read; String line;
while ((line = reader.readLine()) != null)
try (InputStream is = new FileInputStream(new File(fileName))) { digest.update(line.getBytes());
while( (read = is.read(buffer)) > 0) }
digest.update(buffer, 0, read);
byte[] md5sum = digest.digest(); byte[] md5sum = digest.digest();
return md5.xor(new BigInteger(1, md5sum)); return md5.xor(new BigInteger(1, md5sum));
} catch(IOException e) {
throw new RuntimeException("Unable to process file for MD5", e);
}
} }
private static BigInteger string2md5(String input, BigInteger md5) throws NoSuchAlgorithmException { private static BigInteger string2md5(String input, BigInteger md5) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("MD5"); MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] md5sum = digest.digest(input.getBytes()); byte[] md5sum = digest.digest(input.getBytes());
return md5.xor(new BigInteger(1, md5sum)); return md5.xor(new BigInteger(1, md5sum));
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment