import java.io.File;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Digest {
	private static String[] algorithms = {"MD5", "SHA-1", "SHA-256"};
	public static void main(String[] args) {
		if (args.length < 2 || args[0].equals("--help")) {
			printUsageAndExit();
			return;
		}
		String algorithm = args[0];
		// Main loop.
		for (int i = 1; i < args.length; i++) {
			try {
				File input = new File(args[i]);
				byte[] digest = computeDigest(input, algorithm);
				printDigest(digest, args[i]);
				System.out.println(" " + input);
			} catch (IOException ex) {
				System.err.println("Warning: Error reading file: " + args[i]);
			} catch (NoSuchAlgorithmException ex) {
				System.err.println("Unknown algorithm: " + algorithm);
				printUsageAndExit();
				return;
			}
		}
	}

	private static byte[] computeDigest(File input, String algorithm)
	        throws IOException, NoSuchAlgorithmException {
		try (DigestInputStream stream = new DigestInputStream(
		        new BufferedInputStream(new FileInputStream(input)),
		        MessageDigest.getInstance(algorithm))) {
			while (stream.read() != -1);
			MessageDigest digest = stream.getMessageDigest();
			return digest.digest();
		}
	}

	private static void printDigest(byte[] digest, String fileName) {
		for (int i = 0; i < digest.length; i++) {
			System.out.printf("%02x", digest[i]);
		}
	}

	private static void printUsageAndExit() {
		System.err.println("Usage: java Digest <function_name> <file1> file2> ...\n" +
		                   "    <function_name> options include: ");
		for (String algorithm : algorithms) {
			System.err.println("                                    " + algorithm);
		}
		System.exit(1);
	}
}
@@ -1,24 +1,1170 @@
+/*%0A * The MIT License (MIT)%0A *%0A * Copyright (c) 2015 Henry Elliott%0A *%0A * Permission is hereby granted, free of charge, to any person obtaining a copy%0A * of this software and associated documentation files (the %22Software%22), to deal%0A * in the Software without restriction, including without limitation the rights%0A * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell%0A * copies of the Software, and to permit persons to whom the Software is%0A * furnished to do so, subject to the following conditions:%0A *%0A * The above copyright notice and this permission notice shall be included in all%0A * copies or substantial portions of the Software.%0A *%0A * THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR%0A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0A * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE%0A * SOFTWARE.%0A */%0A
 import java.io.File;%0Aimp

Add license to source file