Path: redb-route/redb.Route/Serialization/Base64Text.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Serialization/Base64Text.cs ---- namespace redb.Route.Serialization; /// /// Base64 decoding that reads a value the same way on every runtime. .NET 11 refuses a value whose last character /// carries non-zero unused bits (YR== where an encoder writes YQ==); .NET 10 ignores those bits and /// decodes both to the same bytes. RFC 4648 ยง3.5 allows a decoder either behaviour, so each place picks one: /// accepts such a value on every runtime (data and digests from a partner, as .NET 10 read /// them), refuses it on every runtime (credentials and tokens: one value, one /// spelling). Everything else (alphabet, padding, whitespace, errors) is that of /// . /// public static class Base64Text { private const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /// with the unused bits of the last character read as zero. /// is not base64. public static byte[] Decode(string value) => Convert.FromBase64String(ClearUnusedBits(value)); /// with the unused bits of the last character read as zero. public static bool TryDecode(string value, Span bytes, out int bytesWritten) => Convert.TryFromBase64String(ClearUnusedBits(value), bytes, out bytesWritten); /// /// that refuses a value whose last character has non-zero unused bits on /// every runtime, as .NET 11 does. /// public static bool TryDecodeCanonical(string value, Span bytes, out int bytesWritten) { if (HasUnusedBitsSet(value)) { bytesWritten = 0; return false; } return Convert.TryFromBase64String(value, bytes, out bytesWritten); } /// /// Whether the last character of carries non-zero unused bits: the spelling XML Schema /// base64Binary and .NET 11 refuse. It says nothing else about whether the value is base64. /// public static bool HasUnusedBitsSet(string value) => UnusedBitsOfLast(value, out _, out _, out _); private static string ClearUnusedBits(string value) { if (!UnusedBitsOfLast(value, out var last, out var index, out var unused)) return value; var chars = value.ToCharArray(); chars[last] = Alphabet[index & ~unused]; return new string(chars); } /// True when the last character before the padding has some of its unused bits set. private static bool UnusedBitsOfLast(string value, out int last, out int index, out int unused) { ArgumentNullException.ThrowIfNull(value); index = -1; unused = 0; var pads = 0; last = value.Length - 1; for (; last >= 0; last--) { var c = value[last]; if (c == '=') pads++; else if (c is not (' ' or '\t' or '\r' or '\n')) break; } if (last < 0 || pads is < 1 or > 2) return false; index = Alphabet.IndexOf(value[last], StringComparison.Ordinal); // One '=' leaves 2 unused bits in the last character, two leave 4. unused = pads == 1 ? 0b11 : 0b1111; return index >= 0 && (index & unused) != 0; } }