2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 using Org.BouncyCastle.Crypto.Digests;
25 /// Representation of Double SHA-256 hash
27 public class Hash256 : Hash
29 private static Sha256Digest _hasher256 = new Sha256Digest();
32 public override int hashSize
34 get { return _hasher256.GetDigestSize(); }
37 public Hash256() : base() { }
38 public Hash256(byte[] bytes, int offset=0) : base(bytes, offset) { }
39 public Hash256(Hash256 h) : base(h) { }
41 public static Hash256 Compute256(byte[] dataBytes)
43 var digest1 = new byte[32];
44 var digest2 = new byte[32];
46 _hasher256.BlockUpdate(dataBytes, 0, dataBytes.Length);
47 _hasher256.DoFinal(digest1, 0);
48 _hasher256.BlockUpdate(digest1, 0, digest1.Length);
49 _hasher256.DoFinal(digest2, 0);
51 return new Hash256(digest2);
54 public static Hash256 Compute256(ref byte[] input1, ref byte[] input2)
56 var digest1 = new byte[_hasher256.GetDigestSize()];
57 var digest2 = new byte[_hasher256.GetDigestSize()];
59 _hasher256.BlockUpdate(input1, 0, input1.Length);
60 _hasher256.BlockUpdate(input2, 0, input2.Length);
61 _hasher256.DoFinal(digest1, 0);
63 _hasher256.BlockUpdate(digest1, 0, digest1.Length);
64 _hasher256.DoFinal(digest2, 0);
66 return new Hash256(digest2);
69 public static byte[] ComputeRaw256(byte[] dataBytes)
71 var digest1 = new byte[32];
72 var digest2 = new byte[32];
74 _hasher256.BlockUpdate(dataBytes, 0, dataBytes.Length);
75 _hasher256.DoFinal(digest1, 0);
76 _hasher256.BlockUpdate(digest1, 0, digest1.Length);
77 _hasher256.DoFinal(digest2, 0);
82 public static byte[] ComputeRaw256(ref byte[] input1, ref byte[] input2)
84 var digest1 = new byte[_hasher256.GetDigestSize()];
85 var digest2 = new byte[_hasher256.GetDigestSize()];
87 _hasher256.BlockUpdate(input1, 0, input1.Length);
88 _hasher256.BlockUpdate(input2, 0, input2.Length);
89 _hasher256.DoFinal(digest1, 0);
91 _hasher256.BlockUpdate(digest1, 0, digest1.Length);
92 _hasher256.DoFinal(digest2, 0);