X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FHash.cs;h=5b1d3b74335030e8b2c9ceb251352410600ae35f;hb=1f6b6ffe24de703593f20f5f881a2e797473621c;hp=e013e8ba414dafc8d904577f8d18933aaafb477c;hpb=9f4f9d3f60f89dcf29fb7b24d60ffd40d7360a7d;p=NovacoinLibrary.git
diff --git a/Novacoin/Hash.cs b/Novacoin/Hash.cs
index e013e8b..5b1d3b7 100644
--- a/Novacoin/Hash.cs
+++ b/Novacoin/Hash.cs
@@ -1,20 +1,33 @@
-using System.Security.Cryptography;
-using System.Collections.Generic;
+/**
+ * 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.Diagnostics.Contracts;
using System.Linq;
namespace Novacoin
{
- public abstract class Hash
+ public abstract class Hash : IEquatable, IComparable
{
///
- /// Computes the SHA256 hash for the input data using the managed library.
- ///
- protected static SHA256Managed _hasher256 = new SHA256Managed();
-
- ///
/// Array of digest bytes.
///
- protected byte[] _hashBytes = null;
+ protected byte[] _hashBytes;
///
/// Hash size, must be overriden
@@ -24,33 +37,33 @@ namespace Novacoin
get;
}
- public byte[] hashBytes
- {
- get { return _hashBytes; }
- }
-
///
/// Initializes an empty instance of the Hash class.
///
public Hash()
{
- _hashBytes = Enumerable.Repeat(0, hashSize).ToArray();
+ _hashBytes = new byte[hashSize];
}
///
- /// Initializes a new instance of Hash class with first 20 bytes from supplied list
+ /// Initializes a new instance of Hash class
///
- /// List of bytes
- public Hash(IEnumerable bytes)
+ /// Array of bytes
+ public Hash(byte[] bytes, int offset = 0)
{
- _hashBytes = bytes.Take(hashSize).ToArray();
- }
+ if (bytes.Length - offset < hashSize)
+ {
+ throw new ArgumentException("You need to provide a sufficient amount of data to initialize new instance of hash object.");
+ }
- public Hash(byte[] bytes)
- {
- _hashBytes = bytes;
+ _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];
@@ -62,6 +75,150 @@ namespace Novacoin
get { return !_hashBytes.Any(b => b != 0); }
}
+ public static implicit operator byte[](Hash h)
+ {
+ return h._hashBytes;
+ }
+
+ public bool Equals(Hash item)
+ {
+ if ((object)item == null)
+ {
+ return false;
+ }
+ return _hashBytes.SequenceEqual((byte[])item);
+ }
+
+ public override bool Equals(object o)
+ {
+ if (o == null)
+ {
+ return false;
+ }
+
+ return _hashBytes.SequenceEqual(((Hash)o)._hashBytes);
+ }
+
+ public override int GetHashCode()
+ {
+ int hash = 17;
+ unchecked
+ {
+ foreach (var element in _hashBytes)
+ {
+ hash = hash * 31 + element.GetHashCode();
+ }
+ }
+ return hash;
+ }
+
+ public int CompareTo(Hash item)
+ {
+ if (this > item)
+ {
+ return 1;
+ }
+ else if (this < item)
+ {
+ return -1;
+ }
+
+ return 0;
+ }
+
+ public static bool operator <(Hash a, Hash b)
+ {
+ Contract.Requires((object)a != null && (object)b != null, "Null references are not allowed.");
+ Contract.Requires(a.hashSize == b.hashSize, "Hashes must have the same size.");
+
+ for (int i = a.hashSize - 1; i >= 0; i--)
+ {
+ if (a._hashBytes[i] < b._hashBytes[i])
+ {
+ return true;
+ }
+ else if (a._hashBytes[i] > b._hashBytes[i])
+ {
+ return false;
+ }
+ }
+
+ return false;
+ }
+
+ public static bool operator <=(Hash a, Hash b)
+ {
+ Contract.Requires((object)a != null && (object)b != null, "Null references are not allowed.");
+ Contract.Requires(a.hashSize == b.hashSize, "Hashes must have the same size.");
+
+ for (int i = a.hashSize - 1; i >= 0; i--)
+ {
+ if (a._hashBytes[i] < b._hashBytes[i])
+ {
+ return true;
+ }
+ else if (a._hashBytes[i] > b._hashBytes[i])
+ {
+ return false;
+ }
+ }
+
+ return false;
+ }
+
+ public static bool operator >(Hash a, Hash b)
+ {
+ Contract.Requires((object)a != null && (object)b != null, "Null references are not allowed.");
+ Contract.Requires(a.hashSize == b.hashSize, "Hashes must have the same size.");
+
+ for (int i = a.hashSize - 1; i >= 0; i--)
+ {
+ if (a._hashBytes[i] > b._hashBytes[i])
+ {
+ return true;
+ }
+ else if (a._hashBytes[i] < b._hashBytes[i])
+ {
+ return false;
+ }
+ }
+
+ return false;
+ }
+
+ public static bool operator >=(Hash a, Hash b)
+ {
+ Contract.Requires((object)a != null && (object)b != null, "Null references are not allowed.");
+ Contract.Requires(a.hashSize == b.hashSize, "Hashes must have the same size.");
+
+ for (int i = a.hashSize - 1; i >= 0; i--)
+ {
+ if (a._hashBytes[i] > b._hashBytes[i])
+ {
+ return true;
+ }
+ else if (a._hashBytes[i] < b._hashBytes[i])
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public static bool operator ==(Hash a, Hash b)
+ {
+ Contract.Requires((object)a != null && (object)b != null, "Null references are not allowed.");
+ Contract.Requires(a.hashSize == b.hashSize, "Hashes must have the same size.");
+
+ return a._hashBytes.SequenceEqual(b._hashBytes);
+ }
+
+ public static bool operator !=(Hash a, Hash b)
+ {
+ return !(a == b);
+ }
+
public override string ToString()
{
return Interop.ToHex(Interop.ReverseBytes(_hashBytes));