-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathHashes.cs
More file actions
150 lines (142 loc) · 8.35 KB
/
Copy pathHashes.cs
File metadata and controls
150 lines (142 loc) · 8.35 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
using BytecodeApi.Cryptography.HashAlgorithms;
using BytecodeApi.Extensions;
using System.Security.Cryptography;
using System.Text;
namespace BytecodeApi.Cryptography;
/// <summary>
/// Class to compute hashes of a specific <see cref="HashType" />.
/// </summary>
public static class Hashes
{
/// <summary>
/// Computes the hash value for the specified <see cref="string" /> using the specified <see cref="HashType" />. The <see cref="string" /> is converted using the <see cref="Encoding.UTF8" /> encoding.
/// </summary>
/// <param name="data">The <see cref="string" /> to be used in the hash computation. The <see cref="string" /> is converted using the <see cref="Encoding.UTF8" /> encoding.</param>
/// <param name="type">The <see cref="HashType" /> specifying the algorithm that is used.</param>
/// <returns>
/// A <see cref="byte" />[] representing the fixed-length binary of the hash of the <paramref name="data" /> parameter.
/// </returns>
public static byte[] ComputeBytes(string data, HashType type)
{
return ComputeBytes(data, type, 1);
}
/// <summary>
/// Computes the hash value for the specified <see cref="string" /> using the specified <see cref="HashType" /> and repeats computation a specified number of times. The <see cref="string" /> is converted using the <see cref="Encoding.UTF8" /> encoding.
/// </summary>
/// <param name="data">The <see cref="string" /> to be used in the hash computation. The <see cref="string" /> is converted using the <see cref="Encoding.UTF8" /> encoding.</param>
/// <param name="type">The <see cref="HashType" /> specifying the algorithm that is used.</param>
/// <param name="passes">A <see cref="int" /> value indicating the number of times <paramref name="data" /> should be processed. For successive passes, the binary result of the previous pass is used as input value for the next pass.</param>
/// <returns>
/// A <see cref="byte" />[] representing the fixed-length binary of the hash of the <paramref name="data" /> parameter.
/// </returns>
public static byte[] ComputeBytes(string data, HashType type, int passes)
{
Check.ArgumentNull(data);
return ComputeBytes(data.ToUTF8Bytes(), type, passes);
}
/// <summary>
/// Computes the hash value for the specified <see cref="byte" />[] using the specified <see cref="HashType" />.
/// </summary>
/// <param name="data">The <see cref="byte" />[] to be used in the hash computation.</param>
/// <param name="type">The <see cref="HashType" /> specifying the algorithm that is used.</param>
/// <returns>
/// A <see cref="byte" />[] representing the fixed-length binary of the hash of the <paramref name="data" /> parameter.
/// </returns>
public static byte[] ComputeBytes(byte[] data, HashType type)
{
return ComputeBytes(data, type, 1);
}
/// <summary>
/// Computes the hash value for the specified <see cref="byte" />[] using the specified <see cref="HashType" /> and repeats computation a specified number of times.
/// </summary>
/// <param name="data">The <see cref="byte" />[] to be used in the hash computation.</param>
/// <param name="type">The <see cref="HashType" /> specifying the algorithm that is used.</param>
/// <param name="passes">A <see cref="int" /> value indicating the number of times <paramref name="data" /> should be processed. For successive passes, the binary result of the previous pass is used as input value for the next pass.</param>
/// <returns>
/// A <see cref="byte" />[] representing the fixed-length binary of the hash of the <paramref name="data" /> parameter.
/// </returns>
public static byte[] ComputeBytes(byte[] data, HashType type, int passes)
{
Check.ArgumentNull(data);
Check.ArgumentOutOfRangeEx.Greater0(passes);
using HashAlgorithm hashAlgorithm = type switch
{
HashType.Adler32 => Adler32.Create(),
HashType.CRC32 => CRC32.Create(),
HashType.CRC64 => CRC64.Create(),
HashType.MD2 => MD2.Create(),
HashType.MD4 => MD4.Create(),
HashType.MD5 => MD5.Create(),
HashType.RIPEMD128 => RIPEMD128.Create(),
HashType.RIPEMD160 => RIPEMD160.Create(),
HashType.SHA1 => SHA1.Create(),
HashType.SHA224 => SHA224.Create(),
HashType.SHA256 => SHA256.Create(),
HashType.SHA384 => SHA384.Create(),
HashType.SHA512 => SHA512.Create(),
HashType.Tiger => Tiger.Create(),
HashType.Tiger2 => Tiger2.Create(),
HashType.Whirlpool => Whirlpool.Create(),
_ => throw Throw.InvalidEnumArgument(nameof(type), type)
};
for (int i = 0; i < passes; i++)
{
data = hashAlgorithm.ComputeHash(data);
}
return data;
}
/// <summary>
/// Computes the hash value for the specified <see cref="string" /> using the specified <see cref="HashType" />. The <see cref="string" /> is converted using the <see cref="Encoding.UTF8" /> encoding. The result is the lowercase hexadecimal hash representation of the <paramref name="data" /> parameter.
/// </summary>
/// <param name="data">The <see cref="string" /> to be used in the hash computation. The <see cref="string" /> is converted using the <see cref="Encoding.UTF8" /> encoding.</param>
/// <param name="type">The <see cref="HashType" /> specifying the algorithm that is used.</param>
/// <returns>
/// The lowercase hexadecimal hash representation of the <paramref name="data" /> parameter.
/// </returns>
public static string Compute(string data, HashType type)
{
return Compute(data, type, 1);
}
/// <summary>
/// Computes the hash value for the specified <see cref="string" /> using the specified <see cref="HashType" /> and repeats computation a specified number of times. The <see cref="string" /> is converted using the <see cref="Encoding.UTF8" /> encoding. The result is the lowercase hexadecimal hash representation of the <paramref name="data" /> parameter.
/// </summary>
/// <param name="data">The <see cref="string" /> to be used in the hash computation. The <see cref="string" /> is converted using the <see cref="Encoding.UTF8" /> encoding.</param>
/// <param name="type">The <see cref="HashType" /> specifying the algorithm that is used.</param>
/// <param name="passes">A <see cref="int" /> value indicating the number of times <paramref name="data" /> should be processed. For successive passes, the binary result of the previous pass is used as input value for the next pass.</param>
/// <returns>
/// The lowercase hexadecimal hash representation of the <paramref name="data" /> parameter.
/// </returns>
public static string Compute(string data, HashType type, int passes)
{
Check.ArgumentNull(data);
Check.ArgumentOutOfRangeEx.Greater0(passes);
return ComputeBytes(data.ToUTF8Bytes(), type, passes).ToHexString();
}
/// <summary>
/// Computes the hash value for the specified <see cref="byte" />[] using the specified <see cref="HashType" />. The result is the lowercase hexadecimal hash representation of the <paramref name="data" /> parameter.
/// </summary>
/// <param name="data">The <see cref="byte" />[] to be used in the hash computation.</param>
/// <param name="type">The <see cref="HashType" /> specifying the algorithm that is used.</param>
/// <returns>
/// The lowercase hexadecimal hash representation of the <paramref name="data" /> parameter.
/// </returns>
public static string Compute(byte[] data, HashType type)
{
return Compute(data, type, 1);
}
/// <summary>
/// Computes the hash value for the specified <see cref="byte" />[] using the specified <see cref="HashType" /> and repeats computation a specified number of times. The result is the lowercase hexadecimal hash representation of the <paramref name="data" /> parameter.
/// </summary>
/// <param name="data">The <see cref="byte" />[] to be used in the hash computation.</param>
/// <param name="type">The <see cref="HashType" /> specifying the algorithm that is used.</param>
/// <param name="passes">A <see cref="int" /> value indicating the number of times <paramref name="data" /> should be processed. For successive passes, the binary result of the previous pass is used as input value for the next pass.</param>
/// <returns>
/// The lowercase hexadecimal hash representation of the <paramref name="data" /> parameter.
/// </returns>
public static string Compute(byte[] data, HashType type, int passes)
{
Check.ArgumentNull(data);
Check.ArgumentOutOfRangeEx.Greater0(passes);
return ComputeBytes(data, type, passes).ToHexString();
}
}