Remove RIPEMD160, SHA1 and SHA256 classes.
[NovacoinLibrary.git] / Novacoin / Hash256.cs
1 \feff/**
2  *  Novacoin classes library
3  *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
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.
9
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.
14
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/>.
17  */
18
19
20 using Org.BouncyCastle.Crypto.Digests;
21
22 namespace Novacoin
23 {
24     /// <summary>
25     /// Representation of Double SHA-256 hash
26     /// </summary>
27     public class Hash256 : Hash
28     {
29         private static Sha256Digest _hasher256 = new Sha256Digest();
30
31         // 32 bytes
32         public override int hashSize
33         {
34             get { return _hasher256.GetDigestSize(); }
35         }
36
37         public Hash256() : base() { }
38         public Hash256(byte[] bytes, int offset=0) : base(bytes, offset) { }
39         public Hash256(Hash256 h) : base(h) { }
40
41         public static Hash256 Compute256(byte[] dataBytes)
42         {
43             var digest1 = new byte[32];
44             var digest2 = new byte[32];
45
46             _hasher256.BlockUpdate(dataBytes, 0, dataBytes.Length);            
47             _hasher256.DoFinal(digest1, 0);
48             _hasher256.BlockUpdate(digest1, 0, digest1.Length);            
49             _hasher256.DoFinal(digest2, 0);
50
51             return new Hash256(digest2);
52         }
53
54         public static Hash256 Compute256(ref byte[] input1, ref byte[] input2)
55         {
56             var digest1 = new byte[_hasher256.GetDigestSize()];
57             var digest2 = new byte[_hasher256.GetDigestSize()];
58
59             _hasher256.BlockUpdate(input1, 0, input1.Length);
60             _hasher256.BlockUpdate(input2, 0, input2.Length);
61             _hasher256.DoFinal(digest1, 0);
62
63             _hasher256.BlockUpdate(digest1, 0, digest1.Length);
64             _hasher256.DoFinal(digest2, 0);
65
66             return new Hash256(digest2);
67         }
68
69         public static byte[] ComputeRaw256(byte[] dataBytes)
70         {
71             var digest1 = new byte[32];
72             var digest2 = new byte[32];
73
74             _hasher256.BlockUpdate(dataBytes, 0, dataBytes.Length);
75             _hasher256.DoFinal(digest1, 0);
76             _hasher256.BlockUpdate(digest1, 0, digest1.Length);
77             _hasher256.DoFinal(digest2, 0);
78
79             return digest2;
80         }
81
82         public static byte[] ComputeRaw256(ref byte[] input1, ref byte[] input2)
83         {
84             var digest1 = new byte[_hasher256.GetDigestSize()];
85             var digest2 = new byte[_hasher256.GetDigestSize()];
86
87             _hasher256.BlockUpdate(input1, 0, input1.Length);
88             _hasher256.BlockUpdate(input2, 0, input2.Length);
89             _hasher256.DoFinal(digest1, 0);
90
91             _hasher256.BlockUpdate(digest1, 0, digest1.Length);
92             _hasher256.DoFinal(digest2, 0);
93
94             return digest2;
95         }
96     }
97 }