-
Notifications
You must be signed in to change notification settings - Fork 425
/
Copy pathUtility.cs
256 lines (228 loc) · 8.09 KB
/
Utility.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using Microsoft.IdentityModel.Logging;
namespace Microsoft.IdentityModel.Tokens
{
/// <summary>
/// Contains some utility methods.
/// </summary>
public static class Utility
{
/// <summary>
/// A string with "empty" value.
/// </summary>
public const string Empty = "empty";
/// <summary>
/// A string with "null" value.
/// </summary>
public const string Null = "null";
/// <summary>
/// Creates a copy of the byte array.
/// </summary>
/// <param name="src">The resource array.</param>
/// <returns>A copy of the byte array.</returns>
public static byte[] CloneByteArray(this byte[] src)
{
if (src == null)
throw LogHelper.LogArgumentNullException(nameof(src));
return (byte[])src.Clone();
}
/// <summary>
/// Serializes the list of strings into string as follows:
/// 'str1','str2','str3' ...
/// </summary>
/// <param name="strings">
/// The strings used to build a comma delimited string.
/// </param>
/// <returns>
/// The single <see cref="string"/>.
/// </returns>
internal static string SerializeAsSingleCommaDelimitedString(IEnumerable<string> strings)
{
if (strings == null)
{
return Utility.Null;
}
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (string str in strings)
{
if (first)
{
sb.AppendFormat(CultureInfo.InvariantCulture, "{0}", str ?? Utility.Null);
first = false;
}
else
{
sb.AppendFormat(CultureInfo.InvariantCulture, ", {0}", str ?? Utility.Null);
}
}
if (first)
{
return Utility.Empty;
}
return sb.ToString();
}
/// <summary>
/// Returns whether the input string is https.
/// </summary>
/// <param name="address">The input string.</param>
/// <remarks>true if the input string is https; otherwise, false.</remarks>
public static bool IsHttps(string address)
{
if (string.IsNullOrEmpty(address))
{
return false;
}
try
{
Uri uri = new Uri(address);
return IsHttps(uri);
}
catch (UriFormatException)
{
return false;
}
}
/// <summary>
/// Returns whether the input uri is https.
/// </summary>
/// <param name="uri"><see cref="Uri"/>.</param>
/// <returns>true if the input uri is https; otherwise, false.</returns>
public static bool IsHttps(Uri uri)
{
if (uri == null)
{
return false;
}
#if NET462
return uri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase); //Uri.UriSchemeHttps is internal in dnxcore
#else
return uri.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase);
#endif
}
/// <summary>
/// Compares two byte arrays for equality. Hash size is fixed normally it is 32 bytes.
/// The attempt here is to take the same time if an attacker shortens the signature OR changes some of the signed contents.
/// </summary>
/// <param name="a">
/// One set of bytes to compare.
/// </param>
/// <param name="b">
/// The other set of bytes to compare with.
/// </param>
/// <returns>
/// true if the bytes are equal, false otherwise.
/// </returns>
[MethodImpl(MethodImplOptions.NoInlining)]
public static bool AreEqual(byte[] a, byte[] b)
{
ReadOnlySpan<byte> a1, a2;
if (((a == null) || (b == null))
|| (a.Length != b.Length))
{
// Non-allocating. The direct assignment into a ReadOnlySpan<byte> causes the C# compiler to emit these as pointers into the assembly's data section.
a1 = new byte[] { 0, 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 };
a2 = new byte[] { 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
}
else
{
a1 = a.AsSpan();
a2 = b.AsSpan();
}
int result = 0;
for (int i = 0; i < a1.Length; i++)
{
result |= a1[i] ^ a2[i];
}
return result == 0;
}
/// <summary>
/// Compares two byte spans for equality. Hash size is fixed normally it is 32 bytes.
/// The attempt here is to take the same time if an attacker shortens the signature OR changes some of the signed contents.
/// </summary>
/// <param name="a">
/// One set of bytes to compare.
/// </param>
/// <param name="b">
/// The other set of bytes to compare with.
/// </param>
/// <param name="length">length of spans to check</param>
/// <returns>
/// true if the bytes are equal, false otherwise.
/// </returns>
[MethodImpl(MethodImplOptions.NoInlining)]
internal static bool AreEqual(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b, int length)
{
if ((a.Length < length || b.Length < length))
{
// Non-allocating. The direct assignment into a ReadOnlySpan<byte> causes the C# compiler to emit these as pointers into the assembly's data section.
a = new byte[] { 0, 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 };
b = new byte[] { 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
}
else
{
a = a.Slice(0, length);
b = b.Slice(0, length);
}
int result = 0;
for (int i = 0; i < a.Length; i++)
{
result |= a[i] ^ b[i];
}
return result == 0;
}
internal static byte[] ConvertToBigEndian(long i)
{
byte[] temp = BitConverter.GetBytes(i);
if (BitConverter.IsLittleEndian)
Array.Reverse(temp);
return temp;
}
internal static byte[] Xor(byte[] a, byte[] b, int offset, bool inPlace)
{
if (inPlace)
{
for (var i = 0; i < a.Length; i++)
{
a[i] = (byte)(a[i] ^ b[offset + i]);
}
return a;
}
else
{
var result = new byte[a.Length];
for (var i = 0; i < a.Length; i++)
{
result[i] = (byte)(a[i] ^ b[offset + i]);
}
return result;
}
}
internal static void Zero(byte[] byteArray)
{
for (var i = 0; i < byteArray.Length; i++)
{
byteArray[i] = 0;
}
}
internal static byte[] GenerateSha256Hash(string input)
{
byte[] bytes = Encoding.UTF8.GetBytes(input);
#if NET6_0_OR_GREATER
return SHA256.HashData(bytes);
#else
using (var hash = SHA256.Create())
{
return hash.ComputeHash(bytes);
}
#endif
}
}
}