Exemplo n.º 1
0
        private static void Hash(HashOptions hashOptions)
        {
            if (false == File.Exists(hashOptions.SourceFilename))
            {
                throw new FileNotFoundException("Hash source was not found", hashOptions.SourceFilename);
            }

            var localHash = new LocalHash();
            var stopwatch = new Stopwatch();
            if (hashOptions.MeasureTime)
            {
                stopwatch.Start();
            }

            HashFile(localHash, hashOptions);

            if (hashOptions.MeasureTime)
            {
                stopwatch.Stop();
                Console.WriteLine(string.Format("Hash took {0} minutes ({1} seconds)",
                    stopwatch.Elapsed.TotalMinutes, stopwatch.Elapsed.TotalSeconds));
            }
        }
Exemplo n.º 2
0
 private static HashOptions ParseArgs(string[] args)
 {
     var hashOptions = new HashOptions();
     hashOptions.SourceFilename = args[0];
     OptionValidators.ValidatePath("source filename", hashOptions.SourceFilename);
     Program.HashOptionDescriptorSet.Apply(hashOptions, args.Skip(1));
     return hashOptions;
 }
Exemplo n.º 3
0
 private static void HashFile(LocalHash localHash, HashOptions hashOptions)
 {
     FileMode fileMode = FileMode.Create;
     if (false == hashOptions.Overwrite)
     {
         fileMode = FileMode.CreateNew;
     }
     var hashAlgorithm = HashAlgorithmNames.GetHashAlgorithm(hashOptions.HashAlgorithm);
     if (string.IsNullOrEmpty(hashOptions.OutputFilename))
     {
         if (string.IsNullOrEmpty(hashOptions.ExpectedHash))
         {
             byte[] hash = localHash.ComputeHash(hashOptions.SourceFilename,
                 hashAlgorithm, hashOptions.BufferSize);
             ReportHash(hashOptions.HashAlgorithm, localHash.GetHashString(hash));
         }
         else
         {
             localHash.ValidateHash(hashOptions.SourceFilename,
                 hashAlgorithm, hashOptions.BufferSize, localHash.GetHashArray(hashOptions.ExpectedHash));
         }
     }
     else
     {
         if (string.IsNullOrEmpty(hashOptions.ExpectedHash))
         {
             localHash.ComputeHash(hashOptions.SourceFilename, hashOptions.OutputFilename,
                 fileMode, hashAlgorithm, hashOptions.BufferSize);
         }
         else
         {
             localHash.ValidateHash(hashOptions.SourceFilename, hashOptions.OutputFilename, fileMode,
                 hashAlgorithm, hashOptions.BufferSize, localHash.GetHashArray(hashOptions.ExpectedHash));
         }
     }
 }