示例#1
0
        public void Security_Hash_File_Test()
        {
            string hashHex;

             using (var h1 = new Hash(HashProvider.CRC32))
             using (var stream = File.OpenRead(Path.Combine(Environment.CurrentDirectory, @"TestFiles\gettysburg.txt")))
             {
            hashHex = h1.Calculate(stream).Bytes.ToHex();
             }
             Assert.AreEqual(hashHex, "E37F6423");

             using (var h2 = new Hash(HashProvider.MD5))
             using (var stream = File.OpenRead(Path.Combine(Environment.CurrentDirectory, @"TestFiles\sample.doc")))
             {
            hashHex = h2.Calculate(stream).Bytes.ToHex();
             }
             Assert.AreEqual(hashHex, "4F32AB797F0FCC782AAC0B4F4E5B1693");
        }
示例#2
0
        public void Security_Hash_File_WithProgress_Test()
        {
            string hashHex;

             bool progressRaised = false;
             var progress = new TaskSchedulerProgress<int>(new CurrentThreadTaskScheduler(), value => progressRaised = true);
             using (var h1 = new Hash(HashProvider.CRC32))
             using (var stream = File.OpenRead(Path.Combine(Environment.CurrentDirectory, @"TestFiles\gettysburg.txt")))
             {
            hashHex = h1.Calculate(stream, progress).Bytes.ToHex();
             }
             Assert.AreEqual(hashHex, "E37F6423");
             Assert.IsTrue(progressRaised);

             progressRaised = false;
             progress = new TaskSchedulerProgress<int>(new CurrentThreadTaskScheduler(), value => progressRaised = true);
             using (var h2 = new Hash(HashProvider.MD5))
             using (var stream = File.OpenRead(Path.Combine(Environment.CurrentDirectory, @"TestFiles\sample.doc")))
             {
            hashHex = h2.Calculate(stream, progress).Bytes.ToHex();
             }
             Assert.AreEqual(hashHex, "4F32AB797F0FCC782AAC0B4F4E5B1693");
             Assert.IsTrue(progressRaised);
        }
示例#3
0
 private Data DoSaltedHash(HashProvider p, Data salt)
 {
     using (var h = new Hash(p))
      {
     return h.Calculate(new Data(_targetString), salt);
      }
 }
示例#4
0
 private Data DoHash(HashProvider p)
 {
     using (var h = new Hash(p))
      {
     return h.Calculate(new Data(_targetString));
      }
 }
示例#5
0
        private static string SymmetricFilePrivate(SymmetricProvider p, string fileName, string key)
        {
            string encryptedFilePath = Path.GetFileNameWithoutExtension(fileName) + ".encrypted";
             string decryptedFilePath = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(fileName)) + "-decrypted" + Path.GetExtension(fileName);

             // encrypt the file to memory
             Data encryptedData;
             using (var sym = new Symmetric(p))
             {
            sym.Key = new SymmetricKeyData(key);
            using (var stream = File.OpenRead(fileName))
            {
               encryptedData = sym.Encrypt(stream);
            }
             }

             // write encrypted data to a new binary file
             using (var stream = File.Open(encryptedFilePath, FileMode.Create))
             using (var bw = new BinaryWriter(stream))
             {
            bw.Write(encryptedData.Bytes);
             }

             // decrypt this binary file
             Data decryptedData;
             using (var sym2 = new Symmetric(p))
             {
            sym2.Key = new SymmetricKeyData(key);
            using (var stream = File.OpenRead(encryptedFilePath))
            {
               decryptedData = sym2.Decrypt(stream);
            }
             }

             // write decrypted data to a new binary file
             using (var stream = File.Open(decryptedFilePath, FileMode.Create))
             using (var bw = new BinaryWriter(stream))
             {
            bw.Write(decryptedData.Bytes);
             }

             // get the MD5 hash of the returned data
             using (var h = new Hash(HashProvider.MD5))
             {
            return h.Calculate(decryptedData).Bytes.ToHex();
             }
        }
        public static void VerifyDownload(string localFilePath, ApplicationUpdateFile selectedUpdate)
        {
            Stream stream = null;
             try
             {
            FileInfo fileInfo = new FileInfo(localFilePath);
            if (selectedUpdate.Size != fileInfo.Length)
            {
               throw new IOException(String.Format(CultureInfo.CurrentCulture,
                  "File length is '{0}', expected length is '{1}'.", fileInfo.Length, selectedUpdate.Size));
            }

            stream = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);
            if (selectedUpdate.SHA1.Length != 0)
            {
               Hash hash = new Hash(HashProvider.SHA1);
               Data hashData = hash.Calculate(stream);
               if (String.Compare(selectedUpdate.SHA1, hashData.Bytes.ToHex(), StringComparison.OrdinalIgnoreCase) != 0)
               {
                  throw new IOException("SHA1 file hash is not correct.");
               }
            }
            stream.Position = 0;
            if (selectedUpdate.MD5.Length != 0)
            {
               Hash hash = new Hash(HashProvider.MD5);
               Data hashData = hash.Calculate(stream);
               if (String.Compare(selectedUpdate.MD5, hashData.Bytes.ToHex(), StringComparison.OrdinalIgnoreCase) != 0)
               {
                  throw new IOException("MD5 file hash is not correct.");
               }
            }
             }
             catch (Exception)
             {
            TryToDelete(localFilePath);
            throw;
             }
             finally
             {
            if (stream != null)
            {
               stream.Dispose();
            }
             }
        }