Example #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));
            }
        }
Example #2
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));
         }
     }
 }