private void ProcessRequest(Options commandLineOptions) { StreamHasher fileHashMaker; switch (commandLineOptions.HashAgorithm.ToUpper()) { case "MD160": fileHashMaker = new MD160Hasher(); break; case "SHA1": fileHashMaker = new SHA1Hasher(); break; case "SHA256": fileHashMaker = new SHA256Hasher(); break; case "SHA384": fileHashMaker = new SHA384Hasher(); break; case "SHA512": fileHashMaker = new SHA512Hasher(); break; case "MD5": default: fileHashMaker = new MD5Hasher(); break; } List<String[]> inputFiles = new List<String[]>(); if (commandLineOptions.Concatenate) { // Files will be treated as a single stream - // copy all filenames into a string array, // then add the array to the List String[] files = new String[commandLineOptions.Items.Count]; for (int loop = 0; loop < commandLineOptions.Items.Count; loop++) { files[loop] = commandLineOptions.Items[loop]; } inputFiles.Add(files); } else { // Each file treated as a separate entity - // copy each filename into a separate string array, // then add each array to the List foreach (String fileToProcess in commandLineOptions.Items) { String[] file = new String[] { fileToProcess }; inputFiles.Add(file); } } foreach (String[] fileEntry in inputFiles) { byte[] fileHash = fileHashMaker.ComputeFileHash(fileEntry, (int)commandLineOptions.BlockSize); Console.WriteLine(commandLineOptions.HashAgorithm.ToUpper() + ": " + BitConverter.ToString(fileHash)); } }
static void Main(string[] args) { Options commandLineOptions = new Options(); ICommandLineParser commandParser = new CommandLineParser(); if (commandParser.ParseArguments(args, commandLineOptions, Console.Error)) { if (ValidateOptions(commandLineOptions)) { RequestProcessor procesor = new RequestProcessor(commandLineOptions); } } else { // Command line params could not be parsed, // or help was requested Environment.ExitCode = -1; } }
public RequestProcessor(Options commandLineOptions) { ProcessRequest(commandLineOptions); }
private static bool ValidateOptions(Options commandLineOptions) { bool validatedOK = false; String errorMessage = null; if (commandLineOptions.BlockSize >= 512) { switch (commandLineOptions.HashAgorithm.ToUpper()) { case "MD5": case "MD160": case "SHA1": case "SHA256": case "SHA384": case "SHA512": if (commandLineOptions.Items.Count > 0) { // Make sure all the files exist and // are accessible String fileNotFoundMessage = null; foreach (String inputItem in commandLineOptions.Items) { try { using (FileStream inputFile = new FileStream(inputItem, FileMode.Open, FileAccess.Read)) { inputFile.Close(); } } catch { fileNotFoundMessage = String.Format(messageFileNotFound, inputItem); break; } } if (String.IsNullOrEmpty(fileNotFoundMessage)) { validatedOK = true; } else { errorMessage = fileNotFoundMessage; } } else { errorMessage = messageNoInputFileSpecifed; } break; default: errorMessage = messageUnknownAlgorithm; break; } } else { errorMessage = messageMinimumBlockSize; } if (!String.IsNullOrEmpty(errorMessage)) { Console.Error.WriteLine(errorMessage); Environment.ExitCode = 1; } return validatedOK; }