29import java.io.BufferedInputStream;
30import java.io.FileInputStream;
31import java.io.IOException;
32import java.io.FileNotFoundException;
33import java.io.InputStream;
34import java.net.URISyntaxException;
35import java.security.MessageDigest;
36import java.security.NoSuchAlgorithmException;
37import java.util.ArrayList;
38import java.util.Arrays;
40import java.util.Locale;
41import java.util.regex.Pattern;
43import org.jau.io.IOUtil;
44import org.jau.sys.Debug;
70 private static final boolean DEBUG =
Debug.
debug(
"SHASum");
98 public static long updateDigest(
final MessageDigest digest,
final List<String> filenames)
throws IOException {
100 final byte buffer[] =
new byte[4096];
101 for(
int i=0; i<filenames.size(); i++) {
102 final InputStream in =
new BufferedInputStream(
new FileInputStream(filenames.get(i)));
106 if ((count = in.read(buffer)) == -1) {
109 digest.update(buffer, 0, count);
125 public static StringBuilder
toHexString(
final byte[] shasum, StringBuilder sb) {
127 sb =
new StringBuilder();
129 for(
int i=0; i<shasum.length; i++) {
130 sb.append(String.format((Locale)
null,
"%02x", shasum[i]));
140 public static List<String>
sort(
final ArrayList<String> source) {
141 final String s[] = source.toArray(
new String[source.size()]);
142 Arrays.sort(s, 0, s.length,
null);
143 return Arrays.asList(s);
146 final MessageDigest digest;
147 final List<String> origins;
148 final List<Pattern> excludes, includes;
164 public SHASum(
final MessageDigest digest,
final List<String> origins,
final List<Pattern> excludes,
final List<Pattern> includes) {
165 this.digest = digest;
166 this.origins = origins;
167 this.excludes = excludes;
168 this.includes = includes;
186 public final byte[]
compute(
final boolean verbose)
throws IOException {
189 for(
int i=0; i<fnamesS.size(); i++) {
190 System.err.println(fnamesS.get(i));
194 final byte[] shasum = digest.digest();
196 System.err.println(
"Digested "+numBytes+
" bytes, shasum size "+shasum.length+
" bytes");
197 System.err.println(
"Digested result: "+
SHASum.
toHexString(shasum,
null).toString());
235 public static void main(
final String[] args)
throws IOException {
236 boolean listFilesOnly =
false;
239 final ArrayList<String> pathU =
new ArrayList<String>();
240 final ArrayList<Pattern> excludes =
new ArrayList<Pattern>();
241 final ArrayList<Pattern> includes =
new ArrayList<Pattern>();
243 for(i=0; i<args.length; i++) {
244 if(
null != args[i]) {
245 if( args[i].startsWith(
"--") ) {
247 if( args[i].equals(
"--algorithm")) {
248 shabits = Integer.parseInt(args[++i]);
249 }
else if( args[i].equals(
"--exclude")) {
250 excludes.add(Pattern.compile(args[++i]));
252 System.err.println(
"adding exclude: <"+args[i]+
"> -> <"+excludes.get(excludes.size()-1)+
">");
254 }
else if( args[i].equals(
"--include")) {
255 includes.add(Pattern.compile(args[++i]));
257 System.err.println(
"adding include: <"+args[i]+
"> -> <"+includes.get(includes.size()-1)+
">");
259 }
else if( args[i].equals(
"--listfilesonly")) {
260 listFilesOnly =
true;
262 System.err.println(
"Abort, unknown argument: "+args[i]);
268 System.err.println(
"adding path: <"+args[i]+
">");
273 if( listFilesOnly ) {
275 for(i=0; i<fnamesS.size(); i++) {
276 System.out.println(fnamesS.get(i));
281 final String shaalgo =
"SHA-"+shabits;
282 final MessageDigest digest;
284 digest = MessageDigest.getInstance(shaalgo);
285 }
catch (
final NoSuchAlgorithmException e) {
286 System.err.println(
"Abort, implementation for "+shaalgo+
" not available: "+e.getMessage());
289 final SHASum shaSum =
new SHASum(digest, pathU, excludes, includes);
static ArrayList< String > filesOf(final List< String > paths, final List< Pattern > excludes, final List< Pattern > includes)
Retrieve the list of all filenames traversing through given paths.
Utility class to produce secure hash (SHA) sums over diverse input sources.
static void main(final String[] args)
Main entry point taking var-arg path or gnu-arguments with a leading '–'.
final List< Pattern > getIncludes()
final List< Pattern > getExcludes()
final List< String > getOrigins()
SHASum(final MessageDigest digest, final List< String > origins, final List< Pattern > excludes, final List< Pattern > includes)
Instance to ensure proper compute(boolean) of identical SHA sums over same contents within given path...
static long updateDigest(final MessageDigest digest, final List< String > filenames)
Updates the given digest with the bytes contained by the files denoted by the given filenames in the ...
static List< String > sort(final ArrayList< String > source)
Returns the sorted list of given strings using String#compareTo(String)'s lexicographically compariso...
final byte[] compute(final boolean verbose)
Implementation gathers all files traversing through given paths via IOUtil#filesOf(List,...
static StringBuilder toHexString(final byte[] shasum, StringBuilder sb)
Simple helper to print the given byte-array into a string, here appended to StringBuilder.
Helper routines for logging and debugging.
static final boolean debug(final String subcomponent)