/
ByteUtil.cs
106 lines (97 loc) · 3.89 KB
/
ByteUtil.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
using System;
using System.Diagnostics.Contracts;
using System.Linq;
namespace Libplanet
{
/// <summary>
/// Utility methods to deal with <see cref="byte"/> arrays.
/// </summary>
public static class ByteUtil
{
/// <summary>
/// Converts a hexadecimal string to a <see cref="byte"/> array.
/// </summary>
/// <param name="hex">A <see cref="string"/> which encodes
/// <see cref="byte"/>s in hexadecimal. Its length must be zero or
/// an even number. It must not be <c>null</c>.</param>
/// <returns>A <see cref="byte"/> array that the given
/// <paramref name="hex"/> string represented in hexadecimal.
/// It lengthens the half of the given <paramref name="hex"/> string.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when the given
/// <paramref name="hex"/> string is <c>null</c>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the length
/// of the given <paramref name="hex"/> string is an odd number.
/// </exception>
/// <exception cref="FormatException">Thrown when the given
/// <paramref name="hex"/> string is not a valid hexadecimal string.
/// </exception>
[Pure]
public static byte[] ParseHex(string hex)
{
if (hex == null)
{
throw new ArgumentNullException(nameof(hex));
}
if (hex.Length % 2 > 0)
{
throw new ArgumentOutOfRangeException(
nameof(hex),
"A length of a hexadecimal string must be an even number."
);
}
var bytes = new byte[hex.Length / 2];
for (var i = 0; i < hex.Length / 2; i++)
{
bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return bytes;
}
/// <summary>
/// Renders a hexadecimal string from a <see cref="byte"/> array.
/// </summary>
/// <param name="bytes">A <see cref="byte"/> array to renders
/// the corresponding hexadecimal string. It must not be <c>null</c>.
/// </param>
/// <returns>A hexadecimal string which encodes the given
/// <paramref name="bytes"/>.</returns>
/// <exception cref="ArgumentNullException">Thrown when the given
/// <paramref name="bytes"/> is <c>null</c>.</exception>
[Pure]
public static string Hex(byte[] bytes)
{
if (bytes == null)
{
throw new ArgumentNullException(nameof(bytes));
}
string s = BitConverter.ToString(bytes);
return s.Replace("-", string.Empty).ToLower();
}
/// <summary>
/// Calculates a deterministic hash code from a given
/// <paramref name="bytes"/>. It is mostly used to implement
/// <see cref="object.GetHashCode()"/> method.
/// </summary>
/// <param name="bytes">A <see cref="byte"/> array, which completely
/// determines (or encodes) an object, to calculate a hash code.</param>
/// <returns>A hash code determined from the given
/// <paramref name="bytes"/>. For equivalent <paramref name="bytes"/>,
/// the same hash code is returned. It must not be <c>null</c>.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when the given
/// <paramref name="bytes"/> is <c>null</c>.</exception>
[Pure]
public static int CalculateHashCode(byte[] bytes)
{
if (bytes == null)
{
throw new ArgumentNullException(nameof(bytes));
}
return bytes.Aggregate(
0,
(current, t) => unchecked(current * (bytes.Length + 1) + t)
);
}
}
}