Improve CryptoUtils with wrappers for managed implementations of standard hashing...
authorCryptoManiac <balthazar.ad@gmail.com>
Wed, 2 Sep 2015 10:16:29 +0000 (13:16 +0300)
committerCryptoManiac <balthazar.ad@gmail.com>
Wed, 2 Sep 2015 10:16:29 +0000 (13:16 +0300)
Novacoin/CBlock.cs
Novacoin/CScript.cs
Novacoin/CryptoUtils.cs
Novacoin/ScriptCode.cs
Novacoin/base_uint.cs

index 3f72c68..a620d26 100644 (file)
@@ -426,7 +426,7 @@ namespace Novacoin
                         var left = merkleTree.GetRange((levelOffset + nLeft) * 32, 32).ToArray();
                         var right = merkleTree.GetRange((levelOffset + nRight) * 32, 32).ToArray();
 
-                        merkleTree.AddRange(Hash256.ComputeRaw256(ref left, ref right));
+                        merkleTree.AddRange(CryptoUtils.ComputeHash256(ref left, ref right));
                     }
                     levelOffset += nLevelSize;
                 }
index cd6d27a..95ca17a 100644 (file)
@@ -73,7 +73,7 @@ namespace Novacoin
         ///    New items are added in this format:
         ///    hash_length_byte hash_bytes
         /// </summary>
-        /// <param name="hash">Hash160 instance</param>
+        /// <param name="hash">uint160 instance</param>
         public void AddHash(uint160 hash)
         {
             codeBytes.Add((byte)hash.Size);
@@ -85,7 +85,7 @@ namespace Novacoin
         ///    New items are added in this format:
         ///    hash_length_byte hash_bytes
         /// </summary>
-        /// <param name="hash">Hash256 instance</param>
+        /// <param name="hash">uint256 instance</param>
         public void AddHash(uint256 hash)
         {
             codeBytes.Add((byte)hash.Size);
index 4ba7ee0..12e4bfd 100644 (file)
  */
 
 using System;
+using System.IO;
 using System.Security.Cryptography;
 
 namespace Novacoin
 {
     public class CryptoUtils
     {
+        private static SHA1Managed _sha1 = new SHA1Managed();
+        private static SHA256Managed _sha256 = new SHA256Managed();
+        private static RIPEMD160Managed _ripe160 = new RIPEMD160Managed();
+
+        /// <summary>
+        /// Sha1 calculation
+        /// </summary>
+        /// <param name="inputBytes">Bytes to hash</param>
+        /// <returns>Hashing result</returns>
+        public static byte[] ComputeSha1(byte[] inputBytes)
+        {
+            return _sha1.ComputeHash(inputBytes, 0, inputBytes.Length);
+        }
+
+        /// <summary>
+        /// Sha256 calculation
+        /// </summary>
+        /// <param name="inputBytes">Bytes to hash</param>
+        /// <returns>Hashing result</returns>
+        public static byte[] ComputeSha256(byte[] inputBytes)
+        {
+            return _sha256.ComputeHash(inputBytes, 0, inputBytes.Length);
+        }
+
+        /// <summary>
+        /// RIPEMD-160 calculation
+        /// </summary>
+        /// <param name="inputBytes">Bytes to hash</param>
+        /// <returns>Hashing result</returns>
+        public static byte[] ComputeRipeMD160(byte[] inputBytes)
+        {
+            return _ripe160.ComputeHash(inputBytes, 0, inputBytes.Length);
+        }
+
+        /// <summary>
+        /// RipeMD160(Sha256(X)) calculation
+        /// </summary>
+        /// <param name="inputBytes">Bytes to hash</param>
+        /// <returns>Hashing result</returns>
+        public static byte[] ComputeHash160(byte[] inputBytes)
+        {
+            var digest1 = _sha256.ComputeHash(inputBytes, 0, inputBytes.Length);
+            return _ripe160.ComputeHash(digest1, 0, digest1.Length);
+        }
+
+        /// <summary>
+        /// Sha256(Sha256(X)) calculation
+        /// </summary>
+        /// <param name="inputBytes">Bytes to hash</param>
+        /// <returns>Hashing result</returns>
+        public static byte[] ComputeHash256(byte[] dataBytes)
+        {
+            var digest1 = _sha256.ComputeHash(dataBytes, 0, dataBytes.Length);
+            return _sha256.ComputeHash(digest1, 0, digest1.Length);
+        }
+
+        /// <summary>
+        /// Sha256(Sha256(X)) calculation
+        /// </summary>
+        /// <param name="input1">Reference to first half of data</param>
+        /// <param name="input2">Reference to second half of data</param>
+        /// <returns>Hashing result</returns>
+        public static byte[] ComputeHash256(ref byte[] input1, ref byte[] input2)
+        {
+            var buffer = new byte[64];
+
+            input1.CopyTo(buffer, 0);
+            input2.CopyTo(buffer, input1.Length);
+
+            var digest1 = _sha256.ComputeHash(buffer, 0, buffer.Length);
+            return _sha256.ComputeHash(digest1, 0, digest1.Length);
+        }
+
         public static byte[] PBKDF2_Sha256(int dklen, byte[] password, byte[] salt, int iterationCount)
         {
             /* Init HMAC state. */
@@ -44,7 +118,7 @@ namespace Novacoin
                 }
                 var extendedkey = new byte[salt.Length + 4];
                 Buffer.BlockCopy(salt, 0, extendedkey, 0, salt.Length);
-                using (var ms = new System.IO.MemoryStream())
+                using (var ms = new MemoryStream())
                 {
                     /* Iterate through the blocks. */
                     for (int i = 0; i < keyLength; i++)
index df1d773..14bc2e5 100644 (file)
@@ -1492,25 +1492,25 @@ namespace Novacoin
                                     {
                                         return false;
                                     }
-                                    Hash hash = null;
+                                    byte[] hash = null;
                                     var data = stacktop(ref stack, -1);
 
                                     switch (opcode)
                                     {
                                         case instruction.OP_HASH160:
-                                            hash = Hash160.Compute160(data);
+                                            hash = CryptoUtils.ComputeHash160(data);
                                             break;
                                         case instruction.OP_HASH256:
-                                            hash = Hash256.Compute256(data);
+                                            hash = CryptoUtils.ComputeHash256(data);
                                             break;
                                         case instruction.OP_SHA1:
-                                            hash = SHA1.Compute1(data);
+                                            hash = CryptoUtils.ComputeSha1(data);
                                             break;
                                         case instruction.OP_SHA256:
-                                            hash = SHA256.Compute256(data);
+                                            hash = CryptoUtils.ComputeSha256(data);
                                             break;
                                         case instruction.OP_RIPEMD160:
-                                            hash = RIPEMD160.Compute160(data);
+                                            hash = CryptoUtils.ComputeRipeMD160(data);
                                             break;
                                     }
                                     popstack(ref stack);
index 072111f..502d871 100644 (file)
@@ -250,6 +250,12 @@ namespace Novacoin
         }
         #endregion
 
+        /// <summary>
+        /// Arrays equality checking helper method.
+        /// </summary>
+        /// <param name="a">Array 1</param>
+        /// <param name="b">Array 2</param>
+        /// <returns>Result.</returns>
         private static bool ArraysEqual(uint[] a, uint[] b)
         {
             Contract.Requires<ArgumentException>(a.Length == b.Length, "Array length mismatch.");
@@ -329,6 +335,10 @@ namespace Novacoin
         }
         #endregion
 
+        /// <summary>
+        /// Serialize to string.
+        /// </summary>
+        /// <returns></returns>
         public override string ToString()
         {
             return Interop.ToHex(Interop.ReverseBytes(this));