/
Hasher.cs
127 lines (111 loc) · 4.03 KB
/
Hasher.cs
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
// BLAKE2 reference source code package - C# implementation
namespace Isopoh.Cryptography.Blake2b
{
// Written in 2012 by Christian Winnerlein <codesinchaos@gmail.com>
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
// You should have received a copy of the CC0 Public Domain Dedication along with
// this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
using System;
using System.Security.Cryptography;
/// <summary>
/// The base hasher class.
/// </summary>
public abstract class Hasher : IDisposable
{
/// <summary>
/// Initialize for hash generation.
/// </summary>
public abstract void Init();
/// <summary>
/// Generate the hash from the hash's state.
/// </summary>
/// <returns>
/// The generated hash.
/// </returns>
public abstract byte[] Finish();
/// <summary>
/// Update the hash's state with the given data.
/// </summary>
/// <param name="data">
/// The data to add to the hash's state.
/// </param>
/// <param name="start">
/// The index of the first byte of <paramref name="data"/> to add to the hash's state.
/// </param>
/// <param name="count">
/// The number of bytes of <paramref name="data"/> to add to the hash's state.
/// </param>
public abstract void Update(byte[] data, int start, int count);
/// <summary>
/// Update the hash's state with the given data.
/// </summary>
/// <param name="data">
/// The data to add to the hash's state.
/// </param>
public void Update(byte[] data)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
this.Update(data, 0, data.Length);
}
/// <summary>
/// Create a <see cref="HashAlgorithm"/> from this.
/// </summary>
/// <returns>
/// The <see cref="HashAlgorithm"/> based on this <see cref="Hasher"/>.
/// </returns>
// ReSharper disable once UnusedMember.Global
public HashAlgorithm AsHashAlgorithm() => new HashAlgorithmAdapter(this);
/// <summary>
/// Zero and release sensitive resources.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes resources if <paramref name="disposing"/> is true.
/// </summary>
/// <param name="disposing">
/// Set to true if disposing.
/// </param>
protected virtual void Dispose(bool disposing)
{
}
/// <summary>
/// <see cref="HashAlgorithm"/> implementation for a <see cref="Hasher"/>.
/// </summary>
internal class HashAlgorithmAdapter : HashAlgorithm
{
private readonly Hasher hasher;
/// <summary>
/// Initializes a new instance of the <see cref="HashAlgorithmAdapter"/> class.
/// </summary>
/// <param name="hasher">The <see cref="Hasher"/> the <see cref="HashAlgorithmAdapter"/> is for.</param>
public HashAlgorithmAdapter(Hasher hasher)
{
this.hasher = hasher;
}
/// <inheritdoc/>
public override void Initialize()
{
this.hasher.Init();
}
/// <inheritdoc/>
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
this.hasher.Update(array, ibStart, cbSize);
}
/// <inheritdoc/>
protected override byte[] HashFinal()
{
return this.hasher.Finish();
}
}
}
}