Remove RIPEMD160, SHA1 and SHA256 classes.
[NovacoinLibrary.git] / Novacoin / Hash256.cs
index 8a4f983..3d3a360 100644 (file)
@@ -1,56 +1,97 @@
-\feffusing System;
-using System.Text;
-using System.Linq;
-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 Org.BouncyCastle.Crypto.Digests;
 
 namespace Novacoin
 {
-       /// <summary>
-       /// Representation of SHA-256 hash
-       /// </summary>
-    public class Hash256
+    /// <summary>
+    /// Representation of Double SHA-256 hash
+    /// </summary>
+    public class Hash256 : Hash
     {
+        private static Sha256Digest _hasher256 = new Sha256Digest();
+
         // 32 bytes
-        const int hashSize = 32;
+        public override int hashSize
+        {
+            get { return _hasher256.GetDigestSize(); }
+        }
 
-        /// <summary>
-        /// Array of digest bytes.
-        /// </summary>
-        private byte[] hashBytes = new byte[hashSize];
+        public Hash256() : base() { }
+        public Hash256(byte[] bytes, int offset=0) : base(bytes, offset) { }
+        public Hash256(Hash256 h) : base(h) { }
 
-        /// <summary>
-        /// Initializes an empty instance of the Hash256 class.
-        /// </summary>
-        public Hash256()
+        public static Hash256 Compute256(byte[] dataBytes)
         {
-            hashBytes = Enumerable.Repeat<byte>(0, hashSize).ToArray();
+            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);
         }
 
-        /// <summary>
-        /// Initializes a new instance of Hash256 class with first 32 bytes from supplied list
-        /// </summary>
-        /// <param name="bytesList">List of bytes</param>
-        public Hash256(List<byte> bytesList)
+        public static Hash256 Compute256(ref byte[] input1, ref byte[] input2)
         {
-            hashBytes = bytesList.Take<byte>(hashSize).ToArray<byte>();
+            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 Hash256(byte[] bytesArray)
+        public static byte[] ComputeRaw256(byte[] dataBytes)
         {
-            hashBytes = bytesArray;
+            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 override string ToString()
+        public static byte[] ComputeRaw256(ref byte[] input1, ref byte[] input2)
         {
-            StringBuilder sb = new StringBuilder(hashSize * 2);
-            foreach (byte b in hashBytes)
-            {
-                sb.AppendFormat("{0:x2}", b);
-            }
-            return sb.ToString();
-        }
+            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;
+        }
     }
 }
-