Complete implementation of uint256 and uint160 classes.
[NovacoinLibrary.git] / Novacoin / ScryptHash256.cs
index ae4f0dd..4f95926 100644 (file)
@@ -1,8 +1,22 @@
-\feffusing System;
-using System.Collections.Generic;
-using System.Linq;
+\feff/**
+ *  Novacoin classes library
+ *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
 
-using System.Security.Cryptography;
+ *  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;
 
 namespace Novacoin
 {
@@ -18,96 +32,45 @@ namespace Novacoin
         }
 
         public ScryptHash256() : base() { }
-        public ScryptHash256(byte[] bytesArray) : base(bytesArray) { }
-        public ScryptHash256(IList<byte> bytesList) : base(bytesList) { }
+        public ScryptHash256(byte[] bytes, int offset = 0) : base(bytes, offset) { }
+        public ScryptHash256(ScryptHash256 h) : base(h) { }
 
         /// <summary>
         /// Calculate scrypt hash and return new instance of ScryptHash256 class
         /// </summary>
         /// <param name="inputBytes">Byte sequence to hash</param>
         /// <returns>Hashing result instance</returns>
-        public static ScryptHash256 Compute256(IEnumerable<byte> inputBytes)
+        public static ScryptHash256 Compute256(byte[] inputBytes)
         {
-            uint[] V = new uint[(131072 + 63) / sizeof(uint)];
+            var V = new uint[(131072 + 63) / sizeof(uint)];
 
-            byte[] dataBytes = inputBytes.ToArray();
-            byte[] keyBytes1 = PBKDF2Sha256GetBytes(128, dataBytes, dataBytes, 1);
-            uint[] X = Interop.ToUInt32Array(keyBytes1);
+            var keyBytes1 = CryptoUtils.PBKDF2_Sha256(128, (byte[])inputBytes, (byte[])inputBytes, 1);
+            var X = Interop.ToUInt32Array(keyBytes1);
 
-            uint i, j, k;
-            for (i = 0; i < 1024; i++)
+            for (var i = 0; i < 1024; i++)
             {
                 Array.Copy(X, 0, V, i * 32, 32);
 
                 xor_salsa8(ref X, 0, ref X, 16);
                 xor_salsa8(ref X, 16, ref X, 0);
             }
-            for (i = 0; i < 1024; i++)
+            for (var i = 0; i < 1024; i++)
             {
-                j = 32 * (X[16] & 1023);
-                for (k = 0; k < 32; k++)
+                var j = 32 * (X[16] & 1023);
+                for (var k = 0; k < 32; k++)
+                {
                     X[k] ^= V[j + k];
+                }
                 xor_salsa8(ref X, 0, ref X, 16);
                 xor_salsa8(ref X, 16, ref X, 0);
             }
 
-            byte[] xBytes = Interop.LEBytes(X);
-            byte[] keyBytes2 = PBKDF2Sha256GetBytes(32, dataBytes, xBytes, 1);
+            var xBytes = Interop.LEBytes(X);
+            var keyBytes2 = CryptoUtils.PBKDF2_Sha256(32, (byte[])inputBytes, xBytes, 1);
 
             return new ScryptHash256(keyBytes2);
         }
 
-        private static byte[] PBKDF2Sha256GetBytes(int dklen, byte[] password, byte[] salt, int iterationCount)
-        {
-            using (var hmac = new HMACSHA256(password))
-            {
-                int hashLength = hmac.HashSize / 8;
-                if ((hmac.HashSize & 7) != 0)
-                    hashLength++;
-                int keyLength = dklen / hashLength;
-                if ((long)dklen > (0xFFFFFFFFL * hashLength) || dklen < 0)
-                    throw new ArgumentOutOfRangeException("dklen");
-                if (dklen % hashLength != 0)
-                    keyLength++;
-                byte[] extendedkey = new byte[salt.Length + 4];
-                Buffer.BlockCopy(salt, 0, extendedkey, 0, salt.Length);
-                using (var ms = new System.IO.MemoryStream())
-                {
-                    for (int i = 0; i < keyLength; i++)
-                    {
-                        extendedkey[salt.Length] = (byte)(((i + 1) >> 24) & 0xFF);
-                        extendedkey[salt.Length + 1] = (byte)(((i + 1) >> 16) & 0xFF);
-                        extendedkey[salt.Length + 2] = (byte)(((i + 1) >> 8) & 0xFF);
-                        extendedkey[salt.Length + 3] = (byte)(((i + 1)) & 0xFF);
-                        byte[] u = hmac.ComputeHash(extendedkey);
-                        Array.Clear(extendedkey, salt.Length, 4);
-                        byte[] f = u;
-                        for (int j = 1; j < iterationCount; j++)
-                        {
-                            u = hmac.ComputeHash(u);
-                            for (int k = 0; k < f.Length; k++)
-                            {
-                                f[k] ^= u[k];
-                            }
-                        }
-                        ms.Write(f, 0, f.Length);
-                        Array.Clear(u, 0, u.Length);
-                        Array.Clear(f, 0, f.Length);
-                    }
-                    byte[] dk = new byte[dklen];
-                    ms.Position = 0;
-                    ms.Read(dk, 0, dklen);
-                    ms.Position = 0;
-                    for (long i = 0; i < ms.Length; i++)
-                    {
-                        ms.WriteByte(0);
-                    }
-                    Array.Clear(extendedkey, 0, extendedkey.Length);
-                    return dk;
-                }
-            }
-        }
-
         private static void xor_salsa8(ref uint[] B, int indexB, ref uint[] Bx, int indexBx)
         {
             uint x00, x01, x02, x03, x04, x05, x06, x07, x08, x09, x10, x11, x12, x13, x14, x15;