From b0845248147123a20d7876b1b114e25be90b036a Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Thu, 6 Aug 2020 11:35:39 +0000 Subject: [PATCH] Fixed base64url implementation, was missing a conversion --- SpotifyAPI.Web/Util/Base64Util.cs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/SpotifyAPI.Web/Util/Base64Util.cs b/SpotifyAPI.Web/Util/Base64Util.cs index 1c476e22..379a3148 100644 --- a/SpotifyAPI.Web/Util/Base64Util.cs +++ b/SpotifyAPI.Web/Util/Base64Util.cs @@ -25,6 +25,20 @@ namespace SpotifyAPI.Web var buffer = new char[GetArraySizeRequiredToEncode(input.Length)]; var numBase64Chars = Convert.ToBase64CharArray(input, 0, input.Length, buffer, 0); + // Fix up '+' -> '-' and '/' -> '_'. Drop padding characters. + for (var i = 0; i < numBase64Chars; i++) + { + var ch = buffer[i]; + if (ch == '+') + { + buffer[i] = '-'; + } + else if (ch == '/') + { + buffer[i] = '_'; + } + } + return new string(buffer, startIndex: 0, length: numBase64Chars); } @@ -36,19 +50,14 @@ namespace SpotifyAPI.Web throw new ArgumentNullException(nameof(input)); } - if (count == 0) - { - return Array.Empty(); - } - // Assumption: input is base64url encoded without padding and contains no whitespace. - var paddingCharsToAdd = GetNumBase64PaddingCharsToAddForDecode(count); - var arraySizeRequired = checked(count + paddingCharsToAdd); + var paddingCharsToAdd = GetNumBase64PaddingCharsToAddForDecode(input.Length); + var arraySizeRequired = checked(input.Length + paddingCharsToAdd); // Copy input into buffer, fixing up '-' -> '+' and '_' -> '/'. var i = 0; - for (var j = 0; i < count; i++, j++) + for (var j = 0; i < input.Length; i++, j++) { var ch = input[j]; if (ch == '-')