Closed wiesniak closed 2 years ago
Option with modulo division
private Snowflake EvaluateId(string token)
{
var index = token.IndexOf('.');
var partial = token[..index];
var lastSegmentLength = partial.Length % 4;
var totalWidth = partial.Length + (lastSegmentLength == 0 ? 0 : 4 - lastSegmentLength);
partial = partial.PadRight(totalWidth, '=');
var converted = Convert.FromBase64String(partial);
return new Snowflake(Encoding.ASCII.GetString(converted));
}
It is fixed in v1.0.0-alpha.107
.
Discord generates 3-part token, where first part is base64 encoded Id. This id is encoded without padding.
Token class fails when trying to obtain the Id and decode it if the token is missing padding (
=
at the end). This happens becauseConvert.FromBase64String
method is strict and does not handle strings with missing padding.Proposed change: