Use BouncyCastle hashing functions
[NovacoinLibrary.git] / Novacoin / Hash.cs
index 8cf0b55..b9e6d0c 100644 (file)
@@ -1,5 +1,22 @@
-\feffusing System.Security.Cryptography;
-using System.Collections.Generic;
+\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.Linq;
 
 namespace Novacoin
@@ -7,11 +24,6 @@ namespace Novacoin
     public abstract class Hash
     {
         /// <summary>
-        /// Computes the SHA256 hash for the input data using the managed library.
-        /// </summary>
-        protected static SHA256Managed _hasher256 = new SHA256Managed();
-        
-        /// <summary>
         /// Array of digest bytes.
         /// </summary>
         protected byte[] _hashBytes = null;
@@ -34,25 +46,26 @@ namespace Novacoin
         /// </summary>
         public Hash()
         {
-            _hashBytes = Enumerable.Repeat<byte>(0, hashSize).ToArray();
+            _hashBytes = new byte[hashSize];
         }
 
         /// <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(IEnumerable<byte> bytes)
+        /// <param name="bytesList">Array of bytes</param>
+        public Hash(byte[] bytes, int offset = 0)
         {
-            _hashBytes = bytes.Take<byte>(hashSize).ToArray<byte>();
-        }
-
-        public Hash(byte[] bytes)
-        {
-            _hashBytes = bytes;
+            _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 = new byte[h.hashSize];
             h._hashBytes.CopyTo(_hashBytes, 0);
         }
 
@@ -61,6 +74,11 @@ namespace Novacoin
             get { return !_hashBytes.Any(b => b != 0); }
         }
 
+        /*public static implicit operator BigInteger(Hash h)
+        {
+            return new BigInteger(h._hashBytes);
+        }*/
+
         public override string ToString()
         {
             return Interop.ToHex(Interop.ReverseBytes(_hashBytes));