public static string TrimEndOfTrailingEquals(this string base64UrlString, PaddingPolicy paddingPolicy = PaddingPolicy.Discard)
        {
            var output = paddingPolicy == PaddingPolicy.Discard
                ? base64UrlString.TrimEnd(Characters.Equals)
                : base64UrlString;

            return(output);
        }
Ejemplo n.º 2
0
        public static string Encode(byte[] bytes, PaddingPolicy padding = PaddingPolicy.Discard)
        {
            var encoded = Convert.ToBase64String(bytes).Replace('+', '-').Replace('/', '_');

            if (padding == PaddingPolicy.Discard)
            {
                encoded = encoded.TrimEnd('=');
            }

            return(encoded);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// The following functions perform base64url encoding which differs from regular base64 encoding as follows
        /// * padding is skipped so the pad character '=' doesn't have to be percent encoded
        /// * the 62nd and 63rd regular base64 encoding characters ('+' and '/') are replace with ('-' and '_')
        /// The changes make the encoding alphabet file and URL safe.
        /// </summary>
        /// <param name="input">bytes to encode.</param>
        /// <param name="policy">The padding policy.</param>
        /// <returns>Base64Url encoding of the UTF8 bytes.</returns>
        public string Encode(byte[] input, PaddingPolicy policy = PaddingPolicy.Discard)
        {
            var encoded = Convert.ToBase64String(input).Replace(Char62, UrlChar62).Replace(Char63, UrlChar63);

            if (policy == PaddingPolicy.Discard)
            {
                encoded = encoded.TrimEnd(OnePads);
            }

            return(encoded);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <remarks>
        /// Source: https://github.com/neosmart/UrlBase64/blob/master/UrlBase64/UrlBase64.cs
        /// </remarks>
        public static string EncodeBase64Url(string @string, PaddingPolicy paddingPolicy = PaddingPolicy.Discard)
        {
            var valueBytes = Encoding.UTF8.GetBytes(@string);

            var base64String = Convert.ToBase64String(valueBytes);

            var base64UrlString = base64String.PerformBase64ToBase64UrlCharacterReplacements();

            base64UrlString = base64UrlString.TrimEndOfTrailingEquals(paddingPolicy);

            return(base64UrlString);
        }