/** * Novacoin classes library * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com) * 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 System; using System.Linq; namespace Novacoin { public abstract class Hash { /// /// Array of digest bytes. /// protected byte[] _hashBytes = null; /// /// Hash size, must be overriden /// public abstract int hashSize { get; } public byte[] hashBytes { get { return _hashBytes; } } /// /// Initializes an empty instance of the Hash class. /// public Hash() { _hashBytes = new byte[hashSize]; } /// /// Initializes a new instance of Hash class /// /// Array of bytes public Hash(byte[] bytes, int offset = 0) { _hashBytes = new byte[hashSize]; Array.Copy(bytes, offset, _hashBytes, 0, hashSize); } /// /// Initializes a new instance of Hash class as a copy of another one /// /// Instance of hash class public Hash(Hash h) { _hashBytes = new byte[h.hashSize]; h._hashBytes.CopyTo(_hashBytes, 0); } public bool IsZero { get { return !_hashBytes.Any(b => b != 0); } } /*public static implicit operator BigInteger(Hash h) { return new BigInteger(h._hashBytes); }*/ public override string ToString() { return Interop.ToHex(Interop.ReverseBytes(_hashBytes)); } } }