Closed
Description
Background and motivation
The Base64
class provides efficient methods for encoding and decoding base64 data, but it doesn't provide any means for validating Base64-encoded data is properly encoded, at least not without having output memory into which to decode the resulting data. For scenarios that, for example, want to validate configuration data promptly and that might need the results of that configuration ever or until later, it's desirable to support an efficient means for validating Base64-encoded data without requiring the output memory, which then also enables the decoding/validation to be performed faster.
API Proposal
namespace System.Buffers.Text;
public static class Base64
{
+ public static bool IsValid(ReadOnlySpan<char> base64Text);
+ public static bool IsValid(ReadOnlySpan<byte> base64Text);
}
API Usage
string base64Text = ...;
if (!Base64.IsValid(base64Text))
throw new InvalidConfigurationException(...);
Alternative Designs
- We could expose such methods on the new Ascii class instead, since base64-encoded data is all ASCII. However, we already have two different places for Base64 functionality (Convert and Base64), and ideally we'd use the Base64 class as the central location moving forward for Base64-related functionality (we might even want to duplicate the Convert APIs for working with chars onto the Base64 type for discoverability, or potentially create streaming char-based versions to parallel the streaming byte-based versions already there).
- The existing Encode/Decode methods call the source text parameter "utf8"; we could do the same here, but only for the byte-based overload, which is then inconsistent between the overloads.
Risks
No response