[hyena] [CryptoUtil] Add stream based hashing.



commit 1d173113fa4c23d3e9a6b2295cce3291470056cc
Author: Ruben Vermeersch <ruben savanne be>
Date:   Tue Jun 8 19:52:07 2010 +0200

    [CryptoUtil] Add stream based hashing.
    
    Needed to add Md5EncodeStream method since naming it Md5Encode caused an
    ambiguous match warning.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=620999

 src/Hyena/Hyena/CryptoUtil.cs            |   16 ++++++++++++++++
 src/Hyena/Hyena/Tests/CryptoUtilTests.cs |   16 ++++++++++++++++
 2 files changed, 32 insertions(+), 0 deletions(-)
---
diff --git a/src/Hyena/Hyena/CryptoUtil.cs b/src/Hyena/Hyena/CryptoUtil.cs
index f9fae15..dbc2039 100644
--- a/src/Hyena/Hyena/CryptoUtil.cs
+++ b/src/Hyena/Hyena/CryptoUtil.cs
@@ -27,6 +27,7 @@
 //
 
 using System;
+using System.IO;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Security.Cryptography;
@@ -60,6 +61,21 @@ namespace Hyena
                 hash = md5.ComputeHash (encoding.GetBytes (text));
             }
 
+            return ToHex (hash);
+        }
+
+        public static string Md5EncodeStream (Stream stream)
+        {
+            byte [] hash;
+            lock (md5) {
+                hash = md5.ComputeHash (stream);
+            }
+
+            return ToHex (hash);
+        }
+
+        private static string ToHex (byte [] hash)
+        {
             StringBuilder shash = new StringBuilder ();
             for (int i = 0; i < hash.Length; i++) {
                 shash.Append (hash[i].ToString ("x2"));
diff --git a/src/Hyena/Hyena/Tests/CryptoUtilTests.cs b/src/Hyena/Hyena/Tests/CryptoUtilTests.cs
index 27866ba..b2901b7 100644
--- a/src/Hyena/Hyena/Tests/CryptoUtilTests.cs
+++ b/src/Hyena/Hyena/Tests/CryptoUtilTests.cs
@@ -29,6 +29,7 @@
 #if ENABLE_TESTS
 
 using System;
+using System.IO;
 using NUnit.Framework;
 using Hyena;
 
@@ -56,6 +57,21 @@ namespace Hyena.Tests
             Assert.IsFalse (CryptoUtil.IsMd5Encoded (""));
         }
 
+        [Test]
+        public void Md5EncodeStream ()
+        {
+            var file = Path.GetTempFileName ();
+            var tw = new StreamWriter (file);
+            tw.Write ("testing");
+            tw.Close ();
+
+            var stream = new FileStream (file, FileMode.Open);
+            Assert.AreEqual ("ae2b1fca515949e5d54fb22b8ed95575", CryptoUtil.Md5EncodeStream (stream));
+            stream.Close ();
+
+            File.Delete (file);
+        }
+
         /*[Test]
         public void Md5Performance ()
         {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]