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 System.Security.Cryptography;
21 using System.Collections.Generic;
24 using System.Numerics;
28 public abstract class Hash
31 /// Computes the SHA256 hash for the input data using the managed library.
33 protected static SHA256Managed _hasher256 = new SHA256Managed();
36 /// Array of digest bytes.
38 protected byte[] _hashBytes = null;
41 /// Hash size, must be overriden
43 public abstract int hashSize
48 public byte[] hashBytes
50 get { return _hashBytes; }
54 /// Initializes an empty instance of the Hash class.
58 _hashBytes = new byte[hashSize];
62 /// Initializes a new instance of Hash class
64 /// <param name="bytesList">List of bytes</param>
65 public Hash(IEnumerable<byte> bytes, int skip = 0)
67 _hashBytes = bytes.Skip(skip).Take(hashSize).ToArray();
71 /// Initializes a new instance of Hash class
73 /// <param name="bytesList">Array of bytes</param>
74 public Hash(byte[] bytes, int offset = 0)
76 _hashBytes = new byte[hashSize];
77 Array.Copy(bytes, offset, _hashBytes, 0, hashSize);
81 /// Initializes a new instance of Hash class as a copy of another one
83 /// <param name="bytesList">Instance of hash class</param>
86 _hashBytes = new byte[h.hashSize];
87 h._hashBytes.CopyTo(_hashBytes, 0);
92 get { return !_hashBytes.Any(b => b != 0); }
95 /*public static implicit operator BigInteger(Hash h)
97 return new BigInteger(h._hashBytes);
100 public override string ToString()
102 return Interop.ToHex(Interop.ReverseBytes(_hashBytes));