X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FHash256.cs;h=3d3a360ac051d4ad5000b550ca2736e73954b8ea;hb=1f6b6ffe24de703593f20f5f881a2e797473621c;hp=132dc4088aa4260078b738762dd4aacdef668f96;hpb=9f4f9d3f60f89dcf29fb7b24d60ffd40d7360a7d;p=NovacoinLibrary.git diff --git a/Novacoin/Hash256.cs b/Novacoin/Hash256.cs index 132dc40..3d3a360 100644 --- a/Novacoin/Hash256.cs +++ b/Novacoin/Hash256.cs @@ -1,37 +1,97 @@ -using System; -using System.Text; -using System.Linq; -using System.Collections.Generic; +/** + * 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 . + */ + + +using Org.BouncyCastle.Crypto.Digests; namespace Novacoin { - /// - /// Representation of SHA-256 hash - /// + /// + /// Representation of Double SHA-256 hash + /// public class Hash256 : Hash { + private static Sha256Digest _hasher256 = new Sha256Digest(); + // 32 bytes public override int hashSize { - get { return 32; } + get { return _hasher256.GetDigestSize(); } } public Hash256() : base() { } - public Hash256(byte[] bytes) : base(bytes) { } - public Hash256(IEnumerable bytes) : base(bytes) { } + public Hash256(byte[] bytes, int offset=0) : base(bytes, offset) { } public Hash256(Hash256 h) : base(h) { } + public static Hash256 Compute256(byte[] dataBytes) + { + 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); + } - public static Hash256 Compute256(IEnumerable inputBytes) + public static Hash256 Compute256(ref byte[] input1, ref byte[] input2) { - byte[] dataBytes = inputBytes.ToArray(); - byte[] digest1 = _hasher256.ComputeHash(dataBytes, 0, dataBytes.Length); - byte[] digest2 = _hasher256.ComputeHash(digest1, 0, digest1.Length); + 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 static byte[] ComputeRaw256(byte[] dataBytes) + { + 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 static byte[] ComputeRaw256(ref byte[] input1, ref byte[] input2) + { + 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; + } } } -