Skip to content

Commit 7e6ff25

Browse files
committed
Use Base64Url in WebEncoders
1 parent ef09c06 commit 7e6ff25

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/Shared/WebEncoders/WebEncoders.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
#if NETCOREAPP
77
using System.Buffers;
8+
using System.Buffers.Text;
89
#endif
910
using System.Diagnostics;
1011
using System.Globalization;
@@ -60,6 +61,9 @@ public static byte[] Base64UrlDecode(string input, int offset, int count)
6061

6162
ValidateParameters(input.Length, nameof(input), offset, count);
6263

64+
#if NET9_0_OR_GREATER
65+
return Base64Url.DecodeFromChars(input.AsSpan(offset, count));
66+
#else
6367
// Special-case empty input
6468
if (count == 0)
6569
{
@@ -70,6 +74,7 @@ public static byte[] Base64UrlDecode(string input, int offset, int count)
7074
var buffer = new char[GetArraySizeRequiredToDecode(count)];
7175

7276
return Base64UrlDecode(input, offset, buffer, bufferOffset: 0, count: count);
77+
#endif
7378
}
7479

7580
/// <summary>
@@ -99,6 +104,9 @@ public static byte[] Base64UrlDecode(string input, int offset, char[] buffer, in
99104
ValidateParameters(input.Length, nameof(input), offset, count);
100105
ArgumentOutOfRangeThrowHelper.ThrowIfNegative(bufferOffset);
101106

107+
#if NET9_0_OR_GREATER
108+
return Base64Url.DecodeFromChars(input.AsSpan(offset, count));
109+
#else
102110
if (count == 0)
103111
{
104112
return Array.Empty<byte>();
@@ -150,6 +158,7 @@ public static byte[] Base64UrlDecode(string input, int offset, char[] buffer, in
150158
// Decode.
151159
// If the caller provided invalid base64 chars, they'll be caught here.
152160
return Convert.FromBase64CharArray(buffer, bufferOffset, arraySizeRequired);
161+
#endif
153162
}
154163

155164
/// <summary>
@@ -314,6 +323,9 @@ public static int GetArraySizeRequiredToEncode(int count)
314323
[SkipLocalsInit]
315324
public static string Base64UrlEncode(ReadOnlySpan<byte> input)
316325
{
326+
#if NET9_0_OR_GREATER
327+
return Base64Url.EncodeToString(input);
328+
#else
317329
const int StackAllocThreshold = 128;
318330

319331
if (input.IsEmpty)
@@ -337,6 +349,7 @@ public static string Base64UrlEncode(ReadOnlySpan<byte> input)
337349
}
338350

339351
return base64Url;
352+
#endif
340353
}
341354

342355
#if NET9_0_OR_GREATER
@@ -347,9 +360,11 @@ public static string Base64UrlEncode(ReadOnlySpan<byte> input)
347360
/// <param name="output">The buffer to place the result in.</param>
348361
/// <returns></returns>
349362
public static int Base64UrlEncode(ReadOnlySpan<byte> input, Span<char> output)
363+
{
364+
return Base64Url.EncodeToChars(input, output);
365+
}
350366
#else
351367
private static int Base64UrlEncode(ReadOnlySpan<byte> input, Span<char> output)
352-
#endif
353368
{
354369
Debug.Assert(output.Length >= GetArraySizeRequiredToEncode(input.Length));
355370

@@ -383,9 +398,10 @@ private static int Base64UrlEncode(ReadOnlySpan<byte> input, Span<char> output)
383398

384399
return charsWritten;
385400
}
401+
#endif
386402
#endif
387403

388-
private static int GetNumBase64PaddingCharsToAddForDecode(int inputLength)
404+
private static int GetNumBase64PaddingCharsToAddForDecode(int inputLength)
389405
{
390406
switch (inputLength % 4)
391407
{

0 commit comments

Comments
 (0)