diff --git a/src/Microsoft.AspNetCore.Sockets.Common/Internal/Formatters/MessageFormatUtils.cs b/src/Microsoft.AspNetCore.Sockets.Common/Internal/Formatters/MessageFormatUtils.cs index df070f8621..2d2c25327e 100644 --- a/src/Microsoft.AspNetCore.Sockets.Common/Internal/Formatters/MessageFormatUtils.cs +++ b/src/Microsoft.AspNetCore.Sockets.Common/Internal/Formatters/MessageFormatUtils.cs @@ -13,22 +13,11 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters if (inputPayload.Length > 0) { // Determine the output size - // Every 4 Base64 characters represents 3 bytes - var decodedLength = (inputPayload.Length / 4) * 3; - - // Subtract padding bytes - if (inputPayload[inputPayload.Length - 1] == '=') - { - decodedLength -= 1; - } - if (inputPayload.Length > 1 && inputPayload[inputPayload.Length - 2] == '=') - { - decodedLength -= 1; - } + var decodedLength = Base64Encoder.ComputeDecodedLength(inputPayload); // Allocate a new buffer to decode to var decodeBuffer = new byte[decodedLength]; - if (Base64.Decode(inputPayload, decodeBuffer) != decodedLength) + if (!Base64Encoder.TryDecode(inputPayload, decodeBuffer, out _, out var _)) { throw new FormatException("Invalid Base64 payload"); } diff --git a/src/Microsoft.AspNetCore.Sockets.Common/Internal/Formatters/ServerSentEventsMessageFormatter.cs b/src/Microsoft.AspNetCore.Sockets.Common/Internal/Formatters/ServerSentEventsMessageFormatter.cs index f3c3e8f757..29fdf3d669 100644 --- a/src/Microsoft.AspNetCore.Sockets.Common/Internal/Formatters/ServerSentEventsMessageFormatter.cs +++ b/src/Microsoft.AspNetCore.Sockets.Common/Internal/Formatters/ServerSentEventsMessageFormatter.cs @@ -72,8 +72,8 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters if (type == MessageType.Binary) { // TODO: Base64 writer that works with IOutput would be amazing! - var arr = new byte[Base64.ComputeEncodedLength(payload.Length)]; - Base64.Encode(payload, arr); + var arr = new byte[Base64Encoder.ComputeEncodedLength(payload.Length)]; + Base64Encoder.TryEncode(payload, arr, out _, out _); return TryWriteLine(arr, output); } else diff --git a/src/Microsoft.AspNetCore.Sockets.Common/Internal/Formatters/TextMessageFormatter.cs b/src/Microsoft.AspNetCore.Sockets.Common/Internal/Formatters/TextMessageFormatter.cs index a9c6da8b81..56d9897dc9 100644 --- a/src/Microsoft.AspNetCore.Sockets.Common/Internal/Formatters/TextMessageFormatter.cs +++ b/src/Microsoft.AspNetCore.Sockets.Common/Internal/Formatters/TextMessageFormatter.cs @@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters var length = message.Payload.Length; if (message.Type == MessageType.Binary) { - length = Base64.ComputeEncodedLength(length); + length = Base64Encoder.ComputeEncodedLength(length); } // Get the type indicator @@ -61,8 +61,8 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters if (message.Type == MessageType.Binary) { // TODO: Base64 writer that works with IOutput would be amazing! - var arr = new byte[Base64.ComputeEncodedLength(message.Payload.Length)]; - Base64.Encode(message.Payload, arr); + var arr = new byte[Base64Encoder.ComputeEncodedLength(message.Payload.Length)]; + Base64Encoder.TryEncode(message.Payload, arr, out _, out _); return output.TryWrite(arr); } else