Comments
[NovacoinLibrary.git] / Novacoin / Hash.cs
index c5622d2..990ec7b 100644 (file)
@@ -1,9 +1,25 @@
-\feffusing System;
+\feff/**
+ *  Novacoin classes library
+ *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as
+ *  published by the Free Software Foundation, either version 3 of the
+ *  License, or (at your option) any later version.
+
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using System;
 using System.Security.Cryptography;
 using System.Collections.Generic;
 using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
 
 namespace Novacoin
 {
@@ -22,10 +38,9 @@ namespace Novacoin
         /// <summary>
         /// Hash size, must be overriden
         /// </summary>
-        public virtual int hashSize 
+        public abstract int hashSize 
         {
             get; 
-            private set;
         }
 
         public byte[] hashBytes
@@ -42,27 +57,42 @@ namespace Novacoin
         }
 
         /// <summary>
-        /// Initializes a new instance of Hash class with first 20 bytes from supplied list
+        /// Initializes a new instance of Hash class
         /// </summary>
         /// <param name="bytesList">List of bytes</param>
-        public Hash(IList<byte> bytesList)
+        public Hash(IEnumerable<byte> bytes, int skip = 0)
         {
-            _hashBytes = bytesList.Take<byte>(hashSize).ToArray<byte>();
+            _hashBytes = bytes.Skip(skip).Take(hashSize).ToArray();
         }
 
-        public Hash(byte[] bytesArray)
+        /// <summary>
+        /// Initializes a new instance of Hash class
+        /// </summary>
+        /// <param name="bytesList">Array of bytes</param>
+        public Hash(byte[] bytes, int offset = 0)
+        {
+            _hashBytes = new byte[hashSize];
+            Array.Copy(bytes, offset, _hashBytes, 0, hashSize);
+        }
+
+        /// <summary>
+        /// Initializes a new instance of Hash class as a copy of another one
+        /// </summary>
+        /// <param name="bytesList">Instance of hash class</param>
+        public Hash(Hash h)
         {
-            _hashBytes = bytesArray;
+            _hashBytes = new byte[h.hashSize];
+            h._hashBytes.CopyTo(_hashBytes, 0);
         }
 
-        public bool IsZero()
+        public bool IsZero
         {
-            return !_hashBytes.Any(b => b != 0);
+            get { return !_hashBytes.Any(b => b != 0); }
         }
 
         public override string ToString()
         {
-            return Interop.ToHex(_hashBytes);
+            return Interop.ToHex(Interop.ReverseBytes(_hashBytes));
         }
     }
 }