Using WebEncoders' Base64Url encode/decode functionality
This commit is contained in:
parent
6657f4cf36
commit
ce68ec23c0
|
|
@ -1,10 +1,6 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
|
||||
namespace Microsoft.AspNetCore.WebUtilities
|
||||
{
|
||||
public static class Base64UrlTextEncoder
|
||||
|
|
@ -17,8 +13,7 @@ namespace Microsoft.AspNetCore.WebUtilities
|
|||
/// <returns>Base64 encoded string modified with non-URL encodable characters</returns>
|
||||
public static string Encode(byte[] data)
|
||||
{
|
||||
var encodedValue = Convert.ToBase64String(data);
|
||||
return EncodeInternal(encodedValue);
|
||||
return WebEncoders.Base64UrlEncode(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -29,76 +24,7 @@ namespace Microsoft.AspNetCore.WebUtilities
|
|||
/// <returns>The decoded data.</returns>
|
||||
public static byte[] Decode(string text)
|
||||
{
|
||||
return Convert.FromBase64String(DecodeToBase64String(text));
|
||||
}
|
||||
|
||||
// To enable unit testing
|
||||
internal static string EncodeInternal(string base64EncodedString)
|
||||
{
|
||||
var length = base64EncodedString.Length;
|
||||
while (length > 0 && base64EncodedString[length - 1] == '=')
|
||||
{
|
||||
length--;
|
||||
}
|
||||
|
||||
if (length == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var inplaceStringBuilder = new InplaceStringBuilder(length);
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
if (base64EncodedString[i] == '+')
|
||||
{
|
||||
inplaceStringBuilder.Append('-');
|
||||
}
|
||||
else if (base64EncodedString[i] == '/')
|
||||
{
|
||||
inplaceStringBuilder.Append('_');
|
||||
}
|
||||
else
|
||||
{
|
||||
inplaceStringBuilder.Append(base64EncodedString[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return inplaceStringBuilder.ToString();
|
||||
}
|
||||
|
||||
// To enable unit testing
|
||||
internal static string DecodeToBase64String(string text)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text))
|
||||
{
|
||||
return text;
|
||||
}
|
||||
|
||||
var padLength = 3 - ((text.Length + 3) % 4);
|
||||
var inplaceStringBuilder = new InplaceStringBuilder(capacity: text.Length + padLength);
|
||||
|
||||
for (var i = 0; i < text.Length; i++)
|
||||
{
|
||||
if (text[i] == '-')
|
||||
{
|
||||
inplaceStringBuilder.Append('+');
|
||||
}
|
||||
else if (text[i] == '_')
|
||||
{
|
||||
inplaceStringBuilder.Append('/');
|
||||
}
|
||||
else
|
||||
{
|
||||
inplaceStringBuilder.Append(text[i]);
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < padLength; i++)
|
||||
{
|
||||
inplaceStringBuilder.Append('=');
|
||||
}
|
||||
|
||||
return inplaceStringBuilder.ToString();
|
||||
return WebEncoders.Base64UrlDecode(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,68 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.WebUtilities
|
||||
{
|
||||
public class Base64UrlTextEncoderTests
|
||||
{
|
||||
[Fact]
|
||||
public void DataOfVariousLengthRoundTripCorrectly()
|
||||
{
|
||||
for (int length = 0; length != 256; ++length)
|
||||
{
|
||||
var data = new byte[length];
|
||||
for (int index = 0; index != length; ++index)
|
||||
{
|
||||
data[index] = (byte)(5 + length + (index * 23));
|
||||
}
|
||||
string text = Base64UrlTextEncoder.Encode(data);
|
||||
byte[] result = Base64UrlTextEncoder.Decode(text);
|
||||
|
||||
for (int index = 0; index != length; ++index)
|
||||
{
|
||||
Assert.Equal(data[index], result[index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("", "")]
|
||||
[InlineData("+", "-")]
|
||||
[InlineData("/", "_")]
|
||||
[InlineData("=", "")]
|
||||
[InlineData("==", "")]
|
||||
[InlineData("a+b+c+==", "a-b-c-")]
|
||||
[InlineData("a/b/c==", "a_b_c")]
|
||||
[InlineData("a+b/c==", "a-b_c")]
|
||||
[InlineData("a+b/c", "a-b_c")]
|
||||
[InlineData("abcd", "abcd")]
|
||||
public void EncodeInternal_Replaces_UrlEncodableCharacters(string base64EncodedValue, string expectedValue)
|
||||
{
|
||||
// Arrange & Act
|
||||
var result = Base64UrlTextEncoder.EncodeInternal(base64EncodedValue);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedValue, result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("_", "/===")]
|
||||
[InlineData("-", "+===")]
|
||||
[InlineData("a-b-c", "a+b+c===")]
|
||||
[InlineData("a_b_c_d", "a/b/c/d=")]
|
||||
[InlineData("a-b_c", "a+b/c===")]
|
||||
[InlineData("a-b_c-d", "a+b/c+d=")]
|
||||
[InlineData("abcd", "abcd")]
|
||||
public void DecodeToBase64String_ReturnsValid_Base64String(string text, string expectedValue)
|
||||
{
|
||||
// Arrange & Act
|
||||
var actual = Base64UrlTextEncoder.DecodeToBase64String(text);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedValue, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,13 +2,13 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.WebUtilities
|
||||
{
|
||||
public class WebEncodersTests
|
||||
{
|
||||
|
||||
[Theory]
|
||||
[InlineData("", 1, 0)]
|
||||
[InlineData("", 0, 1)]
|
||||
|
|
@ -41,5 +41,25 @@ namespace Microsoft.AspNetCore.WebUtilities
|
|||
var retVal = WebEncoders.Base64UrlEncode(input, offset, count);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DataOfVariousLengthRoundTripCorrectly()
|
||||
{
|
||||
for (int length = 0; length != 256; ++length)
|
||||
{
|
||||
var data = new byte[length];
|
||||
for (int index = 0; index != length; ++index)
|
||||
{
|
||||
data[index] = (byte)(5 + length + (index * 23));
|
||||
}
|
||||
string text = WebEncoders.Base64UrlEncode(data);
|
||||
byte[] result = WebEncoders.Base64UrlDecode(text);
|
||||
|
||||
for (int index = 0; index != length; ++index)
|
||||
{
|
||||
Assert.Equal(data[index], result[index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue