EvaluateScript and IsCanonicalPubKey implementation, IsCanonicalSignature/CheckSig...
[NovacoinLibrary.git] / Novacoin / Hash256.cs
index 8a4f983..71de8a5 100644 (file)
@@ -3,54 +3,35 @@ using System.Text;
 using System.Linq;
 using System.Collections.Generic;
 
+using System.Security.Cryptography;
 
 namespace Novacoin
 {
        /// <summary>
-       /// Representation of SHA-256 hash
+       /// Representation of Double SHA-256 hash
        /// </summary>
-    public class Hash256
+    public class Hash256 : Hash
     {
         // 32 bytes
-        const int hashSize = 32;
-
-        /// <summary>
-        /// Array of digest bytes.
-        /// </summary>
-        private byte[] hashBytes = new byte[hashSize];
-
-        /// <summary>
-        /// Initializes an empty instance of the Hash256 class.
-        /// </summary>
-        public Hash256()
+        public override int hashSize
         {
-            hashBytes = Enumerable.Repeat<byte>(0, hashSize).ToArray();
+            get { return 32; }
         }
 
-        /// <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)
-        {
-            hashBytes = bytesList.Take<byte>(hashSize).ToArray<byte>();
-        }
+        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 Hash256(byte[] bytesArray)
-        {
-            hashBytes = bytesArray;
-        }
 
-        public override string ToString()
+        public static Hash256 Compute256(IEnumerable<byte> inputBytes)
         {
-            StringBuilder sb = new StringBuilder(hashSize * 2);
-            foreach (byte b in hashBytes)
-            {
-                sb.AppendFormat("{0:x2}", b);
-            }
-            return sb.ToString();
-        }
+            byte[] dataBytes = inputBytes.ToArray();
+            byte[] digest1 = _hasher256.ComputeHash(dataBytes, 0, dataBytes.Length);
+            byte[] digest2 = _hasher256.ComputeHash(digest1, 0, digest1.Length);
 
+            return new Hash256(digest2);
+        }
     }
 }