X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FScryptHash256.cs;h=4f959267eda5b801097a48e8232c250827128790;hb=1ad33d77c24d57f7ac25e0d6cf5f430464cdb058;hp=cd0caa35b342b57faf2b3f9a9665fa08196437d1;hpb=bcdf1637787154a7d20d06fefd235424fda1a1e4;p=NovacoinLibrary.git diff --git a/Novacoin/ScryptHash256.cs b/Novacoin/ScryptHash256.cs index cd0caa3..4f95926 100644 --- a/Novacoin/ScryptHash256.cs +++ b/Novacoin/ScryptHash256.cs @@ -1,16 +1,28 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +/** + * 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. -using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Security; -using Org.BouncyCastle.Asn1; + * 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 . + */ + +using System; namespace Novacoin { + /// + /// Representation of scrypt hash + /// public class ScryptHash256 : Hash { // 32 bytes @@ -20,49 +32,41 @@ namespace Novacoin } public ScryptHash256() : base() { } - public ScryptHash256(byte[] bytesArray) : base(bytesArray) { } - public ScryptHash256(IList bytesList) : base(bytesList) { } - - public static ScryptHash256 Compute256(IEnumerable inputBytes) + public ScryptHash256(byte[] bytes, int offset = 0) : base(bytes, offset) { } + public ScryptHash256(ScryptHash256 h) : base(h) { } + + /// + /// Calculate scrypt hash and return new instance of ScryptHash256 class + /// + /// Byte sequence to hash + /// Hashing result instance + public static ScryptHash256 Compute256(byte[] inputBytes) { - byte[] dataBytes = inputBytes.ToArray(); - - uint[] V = new uint[(131072 + 63) / sizeof(uint)]; - - // sha1 test: - // Rfc2898DeriveBytes key1 = new Rfc2898DeriveBytes(dataBytes, dataBytes, 1); - // byte[] keyBytes1 = key1.GetBytes(128); + var V = new uint[(131072 + 63) / sizeof(uint)]; + var keyBytes1 = CryptoUtils.PBKDF2_Sha256(128, (byte[])inputBytes, (byte[])inputBytes, 1); + var X = Interop.ToUInt32Array(keyBytes1); - // stub: - byte[] keyBytes1 = null; - - uint[] X = Interop.ToUInt32Array(keyBytes1); - - ushort 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 = (ushort)(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); - - // sha1 test: - // Rfc2898DeriveBytes key2 = new Rfc2898DeriveBytes(dataBytes, xBytes, 1); - // byte[] keyBytes2 = key2.GetBytes(32); - - byte[] keyBytes2 = null; + var xBytes = Interop.LEBytes(X); + var keyBytes2 = CryptoUtils.PBKDF2_Sha256(32, (byte[])inputBytes, xBytes, 1); return new ScryptHash256(keyBytes2); }