-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathByteUtilities.cs
More file actions
166 lines (147 loc) · 6.01 KB
/
ByteUtilities.cs
File metadata and controls
166 lines (147 loc) · 6.01 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
using System;
using System.Globalization;
using System.IO;
using System.Text;
namespace Moserware.TlsAnalyzer
{
/// <summary>
/// Utilities to help with <see cref="Byte"/>s.
/// </summary>
public static class ByteUtilities
{
/// <summary>
/// Performs <paramref name="a"/> xor <paramref name="b"/>.
/// </summary>
/// <param name="a">The first byte array.</param>
/// <param name="b">The second byte array.</param>
/// <returns><paramref name="a"/> xor <paramref name="b"/></returns>
public static byte[] Xor(this byte[] a, byte[] b)
{
if (a.Length > b.Length)
{
throw new ArgumentException("'a' must be smaller than or equal to 'b'");
}
byte[] result = new byte[b.Length];
for (int i = 0; i < a.Length; i++)
{
result[i] = (byte)(a[i] ^ b[i]);
}
for (int i = a.Length; i < b.Length; i++)
{
result[i] = b[i];
}
return result;
}
/// <summary>
/// Concatenates byte arrays into a single byte array.
/// </summary>
/// <param name="byteArrays">An array containing all the byte arrays to combine.</param>
/// <returns>A combined array of all of the individual arrays in <paramref name="byteArrays"/>.</returns>
public static byte[] ConcatBytes(params byte[][] byteArrays)
{
using(var ms = new MemoryStream())
{
foreach(byte[] currentArray in byteArrays)
{
ms.Write(currentArray, 0, currentArray.Length);
}
return ms.ToArray();
}
}
/// <summary>
/// Determines if two byte arrays are equivalent.
/// </summary>
/// <param name="a">The first byte array.</param>
/// <param name="b">The second byte array.</param>
/// <returns><see langword="true"/> if the byte arrays are equal; otherwise, <see langword="false"/>.</returns>
public static bool AreEqual(byte[] a, byte[] b)
{
if(a.Length != b.Length)
{
return false;
}
for(int i = 0; i < a.Length; i++)
{
if(a[i] != b[i])
{
return false;
}
}
return true;
}
/// <summary>
/// Formats the bytes for display into single byte segments.
/// </summary>
/// <param name="bytes">Array to convert to a display string.</param>
/// <returns>Formatted display byte string.</returns>
public static string ToDisplayByteString(this byte[] bytes)
{
return ToDisplayByteString(bytes, 1);
}
/// <summary>
/// Gets the ASCII byte represetnation of <paramref name="s"/>.
/// </summary>
/// <param name="s">The string to get the ASCII byte representation of.</param>
/// <returns>The ASCII byte representation of <paramref name="s"/>.</returns>
public static byte[] ToAsciiBytes(this string s)
{
return Encoding.ASCII.GetBytes(s);
}
/// <summary>
/// Formats the bytes for display into <param name="byteGroupSize"/> byte segments.
/// </summary>
/// <param name="bytes">Array to convert to a display string.</param>
/// <returns>Formatted display byte string.</returns>
public static string ToDisplayByteString(this byte[] bytes, int byteGroupSize)
{
int remainderBytes = (bytes.Length % byteGroupSize);
int extraBytesNeeded = (remainderBytes == 0) ? 0 : (byteGroupSize - remainderBytes);
StringBuilder sb = new StringBuilder();
int totalBytes = bytes.Length + extraBytesNeeded;
for (int i = 0; i < totalBytes; i++)
{
if (((i % byteGroupSize) == 0) && (i > 0))
{
// Add a space between the byte groups
sb.Append(" ");
}
if (i < extraBytesNeeded)
{
// Add pad bytes
sb.Append("00");
}
else
{
byte currentByte = bytes[i - extraBytesNeeded];
sb.Append(currentByte.ToString("X2", CultureInfo.InvariantCulture));
}
}
return sb.ToString();
}
/// <summary>
/// Return a <paramref name="length"/> length subset of <paramref name="bytes"/> starting at <paramref name="startIndex"/>.
/// </summary>
/// <param name="bytes">The byte array to take a subset of.</param>
/// <param name="startIndex">The zero-based starting index of subset.</param>
/// <param name="length">The total bytes to have in the subset.</param>
/// <returns>A <paramref name="length"/> length subset of <paramref name="bytes"/> starting at <paramref name="startIndex"/>.</returns>
public static byte[] SubBytes(this byte[] bytes, int startIndex, int length)
{
using (var ms = new MemoryStream())
{
ms.Write(bytes, startIndex, length);
return ms.ToArray();
}
}
/// <summary>
/// Return a subset of <paramref name="bytes"/> starting at <paramref name="startIndex"/>.
/// </summary>
/// <param name="bytes">The byte array to take a subset of.</param>
/// <param name="startIndex">The zero-based starting index of subset.</param>
/// <returns>A subset of <paramref name="bytes"/> starting at <paramref name="startIndex"/>.</returns>
public static byte[] SubBytes(this byte[] bytes, int startIndex)
{
return SubBytes(bytes, startIndex, bytes.Length - startIndex);
}
}
}