Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit ce68ec2

Browse files
committed
Using WebEncoders' Base64Url encode/decode functionality
1 parent 6657f4c commit ce68ec2

File tree

3 files changed

+23
-145
lines changed

3 files changed

+23
-145
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Text;
6-
using Microsoft.Extensions.Primitives;
7-
84
namespace Microsoft.AspNetCore.WebUtilities
95
{
106
public static class Base64UrlTextEncoder
@@ -17,8 +13,7 @@ public static class Base64UrlTextEncoder
1713
/// <returns>Base64 encoded string modified with non-URL encodable characters</returns>
1814
public static string Encode(byte[] data)
1915
{
20-
var encodedValue = Convert.ToBase64String(data);
21-
return EncodeInternal(encodedValue);
16+
return WebEncoders.Base64UrlEncode(data);
2217
}
2318

2419
/// <summary>
@@ -29,76 +24,7 @@ public static string Encode(byte[] data)
2924
/// <returns>The decoded data.</returns>
3025
public static byte[] Decode(string text)
3126
{
32-
return Convert.FromBase64String(DecodeToBase64String(text));
33-
}
34-
35-
// To enable unit testing
36-
internal static string EncodeInternal(string base64EncodedString)
37-
{
38-
var length = base64EncodedString.Length;
39-
while (length > 0 && base64EncodedString[length - 1] == '=')
40-
{
41-
length--;
42-
}
43-
44-
if (length == 0)
45-
{
46-
return string.Empty;
47-
}
48-
49-
var inplaceStringBuilder = new InplaceStringBuilder(length);
50-
for (var i = 0; i < length; i++)
51-
{
52-
if (base64EncodedString[i] == '+')
53-
{
54-
inplaceStringBuilder.Append('-');
55-
}
56-
else if (base64EncodedString[i] == '/')
57-
{
58-
inplaceStringBuilder.Append('_');
59-
}
60-
else
61-
{
62-
inplaceStringBuilder.Append(base64EncodedString[i]);
63-
}
64-
}
65-
66-
return inplaceStringBuilder.ToString();
67-
}
68-
69-
// To enable unit testing
70-
internal static string DecodeToBase64String(string text)
71-
{
72-
if (string.IsNullOrEmpty(text))
73-
{
74-
return text;
75-
}
76-
77-
var padLength = 3 - ((text.Length + 3) % 4);
78-
var inplaceStringBuilder = new InplaceStringBuilder(capacity: text.Length + padLength);
79-
80-
for (var i = 0; i < text.Length; i++)
81-
{
82-
if (text[i] == '-')
83-
{
84-
inplaceStringBuilder.Append('+');
85-
}
86-
else if (text[i] == '_')
87-
{
88-
inplaceStringBuilder.Append('/');
89-
}
90-
else
91-
{
92-
inplaceStringBuilder.Append(text[i]);
93-
}
94-
}
95-
96-
for (var i = 0; i < padLength; i++)
97-
{
98-
inplaceStringBuilder.Append('=');
99-
}
100-
101-
return inplaceStringBuilder.ToString();
27+
return WebEncoders.Base64UrlDecode(text);
10228
}
10329
}
10430
}

test/Microsoft.AspNetCore.WebUtilities.Tests/Base64UrlTextEncoderTests.cs

-68
This file was deleted.

test/Microsoft.AspNetCore.WebUtilities.Tests/WebEncodersTests.cs

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Linq;
65
using Xunit;
76

87
namespace Microsoft.AspNetCore.WebUtilities
98
{
109
public class WebEncodersTests
1110
{
11+
1212
[Theory]
1313
[InlineData("", 1, 0)]
1414
[InlineData("", 0, 1)]
@@ -41,5 +41,25 @@ public void Base64UrlEncode_BadOffsets(int inputLength, int offset, int count)
4141
var retVal = WebEncoders.Base64UrlEncode(input, offset, count);
4242
});
4343
}
44+
45+
[Fact]
46+
public void DataOfVariousLengthRoundTripCorrectly()
47+
{
48+
for (int length = 0; length != 256; ++length)
49+
{
50+
var data = new byte[length];
51+
for (int index = 0; index != length; ++index)
52+
{
53+
data[index] = (byte)(5 + length + (index * 23));
54+
}
55+
string text = WebEncoders.Base64UrlEncode(data);
56+
byte[] result = WebEncoders.Base64UrlDecode(text);
57+
58+
for (int index = 0; index != length; ++index)
59+
{
60+
Assert.Equal(data[index], result[index]);
61+
}
62+
}
63+
}
4464
}
4565
}

0 commit comments

Comments
 (0)