Author: alanmc
Date: 2007-04-03 18:33:00 -0400 (Tue, 03 Apr 2007)
New Revision: 75366

Modified:
   trunk/bitsharp/src/MonoTorrent.Common/Hashes.cs
Log:
Patch from David Muir for the hashes class to add a little more functionality

Modified: trunk/bitsharp/src/MonoTorrent.Common/Hashes.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Common/Hashes.cs     2007-04-03 22:20:57 UTC 
(rev 75365)
+++ trunk/bitsharp/src/MonoTorrent.Common/Hashes.cs     2007-04-03 22:33:00 UTC 
(rev 75366)
@@ -6,6 +6,14 @@
 {
     public class Hashes
     {
+        #region Constants
+        /// <summary>
+        /// Hash code length (in bytes)
+        /// </summary>
+        public const int HashCodeLength = 20;
+        #endregion
+
+
         #region Private Fields
 
         private int count;
@@ -16,6 +24,9 @@
 
         #region Properties
 
+        /// <summary>
+        /// Number of Hashes (equivalent to number of Pieces)
+        /// </summary>
         public int Count
         {
             get { return this.count; }
@@ -26,7 +37,7 @@
 
         #region Constructors
 
-        public Hashes(byte[] hashData, int count)
+        internal Hashes(byte[] hashData, int count)
         {
             this.hashData = hashData;
             this.count = count;
@@ -37,16 +48,45 @@
 
         #region Methods
 
+        /// <summary>
+        /// Determine whether a calculated hash is equal to our stored hash
+        /// </summary>
+        /// <param name="hash">Hash code to check</param>
+        /// <param name="hashIndex">Index of hash/piece to verify 
against</param>
+        /// <returns>true iff hash is equal to our stored hash, false 
otherwise</returns>
         public bool IsValid(byte[] hash, int hashIndex)
         {
-            int start = hashIndex * 20;
-            for (int i = 0; i < 20; i++)
+                       if (hash == null)
+                               throw new ArgumentNullException("hash");
+
+                       if (hash.Length != HashCodeLength)
+                               throw new ArgumentException(string.Format("Hash 
must be {0} bytes in length", HashCodeLength), "hash");
+
+            int start = hashIndex * HashCodeLength;
+            for (int i = 0; i < HashCodeLength; i++)
                 if (hash[i] != this.hashData[i + start])
                     return false;
 
             return true;
         }
 
+        /// <summary>
+        /// Return hash for individual piece
+        /// </summary>
+        /// <param name="hashIndex">Piece/hash index to return</param>
+        /// <returns>byte[] (length HashCodeLength) containing 
hashdata</returns>
+        public byte[] ReadHash(int hashIndex)
+        {
+                       if (hashIndex < 0 || hashIndex >= count)
+                               throw new 
ArgumentOutOfRangeException("hashIndex");
+
+            // Read out our specified piece's hash data
+            byte[] hash = new byte[HashCodeLength];
+                       Buffer.BlockCopy(this.hashData, hashIndex * 
HashCodeLength, hash, 0, HashCodeLength);
+
+            return hash;
+        }
+
         #endregion Methods
     }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to