This repository was archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathHashValue.cs
More file actions
214 lines (177 loc) · 6.97 KB
/
HashValue.cs
File metadata and controls
214 lines (177 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace System.Data.HashFunction.Core.Utilities
{
/// <summary>
/// Implementation of <see cref="IHashValue"/>
/// </summary>
public sealed class HashValue
: IHashValue
{
/// <summary>
/// Gets resulting byte array.
/// </summary>
/// <value>
/// The hash value.
/// </value>
/// <remarks>
/// Implementations should coerce the input hash value to be <see cref="BitLength"/> size in bits.
/// </remarks>
public byte[] Hash { get; }
/// <summary>
/// Gets the length of the hash value in bits.
/// </summary>
/// <value>
/// The length of the hash value bit.
/// </value>
public int BitLength { get; }
/// <summary>
/// Initializes a new instance of <see cref="HashValue"/>.
/// </summary>
/// <param name="hash">The hash.</param>
/// <param name="bitLength">Length of the hash, in bits.</param>
/// <exception cref="ArgumentNullException"><paramref name="hash"/></exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="bitLength"/>;bitLength must be greater than or equal to 1.</exception>
public HashValue(IEnumerable<byte> hash, int bitLength)
{
if (hash == null)
throw new ArgumentNullException(nameof(hash));
if (bitLength < 1)
throw new ArgumentOutOfRangeException(nameof(bitLength), $"{nameof(bitLength)} must be greater than or equal to 1.");
Hash = CoerceToArray(hash, bitLength);
BitLength = bitLength;
}
/// <summary>
/// Converts the hash value to a the base64 string.
/// </summary>
/// <returns>
/// A base64 string representing this hash value.
/// </returns>
public string AsBase64String()
{
return Convert.ToBase64String(Hash);
}
/// <summary>
/// Converts the hash value to a bit array.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.BitArray" /> instance to represent this hash value.
/// </returns>
public BitArray AsBitArray()
{
return new BitArray(Hash) {
Length = BitLength
};
}
/// <summary>
/// Converts the hash value to a hexadecimal string.
/// </summary>
/// <returns>
/// A hex string representing this hash value.
/// </returns>
public string AsHexString() => AsHexString(false);
/// <summary>
/// Converts the hash value to a hexadecimal string.
/// </summary>
/// <param name="uppercase"><c>true</c> if the result should use uppercase hex values; otherwise <c>false</c>.</param>
/// <returns>
/// A hex string representing this hash value.
/// </returns>
public string AsHexString(bool uppercase)
{
var stringBuilder = new StringBuilder(Hash.Length);
var formatString = uppercase ? "X2" : "x2";
foreach (var byteValue in Hash)
stringBuilder.Append(byteValue.ToString(formatString));
return stringBuilder.ToString();
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
unchecked
{
var hashCode = 17;
hashCode = (hashCode * 31) ^ BitLength.GetHashCode();
foreach (var value in Hash)
hashCode = (hashCode * 31) ^ value.GetHashCode();
return hashCode;
}
}
/// <summary>
/// Determines whether the specified <see cref="Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="Object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
return Equals(obj as IHashValue);
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// <c>true</c> if the current object is equal to the <paramref name="other" /> parameter; otherwise, <c>false</c>.
/// </returns>
public bool Equals(IHashValue other)
{
if (other == null || other.BitLength != BitLength)
return false;
return Hash.SequenceEqual(other.Hash);
}
/// <summary>
/// Coerces the given <paramref name="hash"/> to a byte array with <paramref name="bitLength"/> significant bits.
/// </summary>
/// <param name="hash">The hash.</param>
/// <param name="bitLength">Length of the hash, in bits.</param>
/// <returns>A byte array that has been coerced to the proper length.</returns>
private static byte[] CoerceToArray(IEnumerable<byte> hash, int bitLength)
{
var byteLength = (bitLength + 7) / 8;
if ((bitLength % 8) == 0)
{
if (hash is IReadOnlyCollection<byte> hashByteCollection)
{
if (hashByteCollection.Count == byteLength)
return hash.ToArray();
}
if (hash is byte[] hashByteArray)
{
var newHashArray = new byte[byteLength];
{
Array.Copy(hashByteArray, newHashArray, Math.Min(byteLength, hashByteArray.Length));
}
return newHashArray;
}
}
byte finalByteMask = (byte)((1 << (bitLength % 8)) - 1);
{
if (finalByteMask == 0)
finalByteMask = 255;
}
var coercedArray = new byte[byteLength];
var currentIndex = 0;
var hashEnumerator = hash.GetEnumerator();
while (currentIndex < byteLength && hashEnumerator.MoveNext())
{
if (currentIndex == (byteLength - 1))
coercedArray[currentIndex] = (byte) (hashEnumerator.Current & finalByteMask);
else
coercedArray[currentIndex] = hashEnumerator.Current;
currentIndex += 1;
}
return coercedArray;
}
}
}