Remove RIPEMD160, SHA1 and SHA256 classes.
[NovacoinLibrary.git] / Novacoin / Hash256.cs
index 2615855..3d3a360 100644 (file)
  */
 
 
-using System.Linq;
-using System.Collections.Generic;
+using Org.BouncyCastle.Crypto.Digests;
 
 namespace Novacoin
 {
-       /// <summary>
-       /// Representation of Double SHA-256 hash
-       /// </summary>
+    /// <summary>
+    /// Representation of Double SHA-256 hash
+    /// </summary>
     public class Hash256 : Hash
     {
+        private static Sha256Digest _hasher256 = new Sha256Digest();
+
         // 32 bytes
         public override int hashSize
         {
-            get { return 32; }
+            get { return _hasher256.GetDigestSize(); }
         }
 
         public Hash256() : base() { }
         public Hash256(byte[] bytes, int offset=0) : base(bytes, offset) { }
-        public Hash256(IEnumerable<byte> bytes, int skip=0) : base(bytes, skip) { }
         public Hash256(Hash256 h) : base(h) { }
 
+        public static Hash256 Compute256(byte[] dataBytes)
+        {
+            var digest1 = new byte[32];
+            var digest2 = new byte[32];
+
+            _hasher256.BlockUpdate(dataBytes, 0, dataBytes.Length);            
+            _hasher256.DoFinal(digest1, 0);
+            _hasher256.BlockUpdate(digest1, 0, digest1.Length);            
+            _hasher256.DoFinal(digest2, 0);
+
+            return new Hash256(digest2);
+        }
 
-        public static Hash256 Compute256(IEnumerable<byte> inputBytes)
+        public static Hash256 Compute256(ref byte[] input1, ref byte[] input2)
         {
-            var dataBytes = inputBytes.ToArray();
-            var digest1 = _hasher256.ComputeHash(dataBytes, 0, dataBytes.Length);
-            var digest2 = _hasher256.ComputeHash(digest1, 0, digest1.Length);
+            var digest1 = new byte[_hasher256.GetDigestSize()];
+            var digest2 = new byte[_hasher256.GetDigestSize()];
+
+            _hasher256.BlockUpdate(input1, 0, input1.Length);
+            _hasher256.BlockUpdate(input2, 0, input2.Length);
+            _hasher256.DoFinal(digest1, 0);
+
+            _hasher256.BlockUpdate(digest1, 0, digest1.Length);
+            _hasher256.DoFinal(digest2, 0);
 
             return new Hash256(digest2);
         }
+
+        public static byte[] ComputeRaw256(byte[] dataBytes)
+        {
+            var digest1 = new byte[32];
+            var digest2 = new byte[32];
+
+            _hasher256.BlockUpdate(dataBytes, 0, dataBytes.Length);
+            _hasher256.DoFinal(digest1, 0);
+            _hasher256.BlockUpdate(digest1, 0, digest1.Length);
+            _hasher256.DoFinal(digest2, 0);
+
+            return digest2;
+        }
+
+        public static byte[] ComputeRaw256(ref byte[] input1, ref byte[] input2)
+        {
+            var digest1 = new byte[_hasher256.GetDigestSize()];
+            var digest2 = new byte[_hasher256.GetDigestSize()];
+
+            _hasher256.BlockUpdate(input1, 0, input1.Length);
+            _hasher256.BlockUpdate(input2, 0, input2.Length);
+            _hasher256.DoFinal(digest1, 0);
+
+            _hasher256.BlockUpdate(digest1, 0, digest1.Length);
+            _hasher256.DoFinal(digest2, 0);
+
+            return digest2;
+        }
     }
 }
-