示例#1
0
        private static void Main(string[] args)
        {
            var configuration = new ConfigurationSettings();
            parse_arguments_and_set_up_configuration(configuration, args);

            #if DEBUG
            Console.WriteLine("FilePath='{0}'", configuration.FilePath);
            Console.WriteLine("HashType='{0}'", configuration.HashType);
            Console.WriteLine("HashToCheck={0}", configuration.HashToCheck);
            #endif

            configuration.FilePath = Path.GetFullPath(configuration.FilePath);
            if (!File.Exists(configuration.FilePath))
            {
                Console.WriteLine("File '{0}' doesn't exist.", configuration.FilePath);
                Environment.Exit(1);
            }

            HashAlgorithm hash_util = new MD5CryptoServiceProvider();

            if (configuration.HashType.ToLowerSafe() == "sha1")
            {
                hash_util = new SHA1CryptoServiceProvider();
            }

            //todo: Wonder if we need to flip this for perf on very large files: http://stackoverflow.com/a/13926809
            var hash = hash_util.ComputeHash(File.Open(configuration.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));

            string hash_string = BitConverter.ToString(hash).Replace("-",string.Empty);

            if (string.IsNullOrWhiteSpace(configuration.HashToCheck))
            {
                Console.WriteLine(hash_string);
                pause_execution_if_debug();
                Environment.Exit(0);
            }

            var result = string.Compare(configuration.HashToCheck, hash_string, ignoreCase: true,culture:CultureInfo.InvariantCulture);

            if (result != 0)
            {
                Console.WriteLine("Error - hashes do not match. Actual value was '{0}'.", hash_string);
                Environment.ExitCode = 1;
            }
            else
            {
                Console.WriteLine("Hashes match.");
            }

            pause_execution_if_debug();
            Environment.Exit(Environment.ExitCode);
        }
示例#2
0
        /// <summary>
        /// Parses the arguments and sets up the configuration
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <param name="args">The arguments.</param>
        private static void parse_arguments_and_set_up_configuration(ConfigurationSettings configuration, string[] args)
        {
            bool help = false;

            OptionSet option_set = new OptionSet()
                .Add("?|help|h",
                     "Prints out the options.",
                     option => help = option != null)
                .Add("f=|file=",
                     "REQUIRED: file - The is the name of the file. The file should exist. You do not need to specify -f or -file in front of this argument.",
                     option => configuration.FilePath = option)
                .Add("t=|type=|hashtype=",
                     "Optional: hashtype - 'md5' or 'sha1' Defaults to 'md5'.",
                     option => configuration.HashType = option)
                 .Add("c=|check=",
                     "check - the signature you want to check. Not case sensitive.",
                     option => configuration.HashToCheck = option)
                ;

            try
            {
                var extra_args = option_set.Parse(args);
                if (extra_args != null && extra_args.Count != 0)
                {
                    if (string.IsNullOrWhiteSpace(configuration.FilePath))
                    {
                        configuration.FilePath = extra_args[0];
                    }
                }
            }
            catch (OptionException)
            {
                show_help(option_set);
            }
            if (help) show_help(option_set);
            if (string.IsNullOrWhiteSpace(configuration.FilePath)) show_help(option_set);

            configuration.FilePath = configuration.FilePath.Replace("'", string.Empty).Replace("\"", string.Empty).Trim();
            if (!string.IsNullOrWhiteSpace(configuration.HashType))
            {
                configuration.HashType = configuration.HashType.Trim();
            }
            else
            {
                configuration.HashType = "md5";
            }
        }