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;
import org.kohsuke.args4j.ParserProperties;
import org.xml.sax.InputSource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
......@@ -282,9 +281,9 @@ public class ADESchemaCompiler {
log(LogLevel.INFO, "Running in strict mode. Checking sanity of subfolder 'schemas'");
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.");
}
......
/*
* ade-xjc - XML Schema binding compiler for CityGML ADEs
* https://github.com/citygml4j/ade-xjc
*
*
* ade-xjc is part of the citygml4j project
*
*
* Copyright 2013-2017 Claus Nagel <claus.nagel@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
......@@ -20,58 +20,54 @@
*/
package org.citygml4j.ade_xjc;
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Set;
import java.util.TreeSet;
public class Util {
public static BigInteger dir2md5(File file, BigInteger md5) throws NoSuchAlgorithmException {
md5 = string2md5(file.getName(), md5);
public static BigInteger dir2md5(Path dir, BigInteger md5) throws IOException, NoSuchAlgorithmException {
// 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()));
Files.walk(dir)
.filter(p -> Files.isDirectory(p)
|| p.getFileName().toString().endsWith(".xsd")
|| p.getFileName().toString().endsWith(".xjb"))
.forEach(files::add);
if (file.isDirectory()) {
File[] children = file.listFiles(pathname -> pathname.isDirectory()
|| pathname.getName().endsWith("xsd")
|| pathname.getName().endsWith("xjb"));
if (children != null) {
for (File next : children)
md5 = dir2md5(next, md5);
}
} else
md5 = file2md5(file.getAbsolutePath(), md5);
for (Path file : files) {
md5 = string2md5(file.getFileName().toString(), md5);
if (Files.isRegularFile(file))
md5 = file2md5(file, 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");
byte[] buffer = new byte[8192];
int read;
try (InputStream is = new FileInputStream(new File(fileName))) {
while( (read = is.read(buffer)) > 0)
digest.update(buffer, 0, read);
byte[] md5sum = digest.digest();
return md5.xor(new BigInteger(1, md5sum));
} catch(IOException e) {
throw new RuntimeException("Unable to process file for MD5", e);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(file)))) {
String line;
while ((line = reader.readLine()) != null)
digest.update(line.getBytes());
}
byte[] md5sum = digest.digest();
return md5.xor(new BigInteger(1, md5sum));
}
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());
return md5.xor(new BigInteger(1, md5sum));
}
......@@ -93,5 +89,5 @@ public class Util {
}
}
}
}
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