Пример #1
0
        private static Byte[] ParseToByteArray(String encoded, Base64GuidOptions options)
        {
            if (encoded is null)
            {
                throw new ArgumentNullException(nameof(encoded));
            }

            if (!options.Padding && (encoded.Length < 22 || encoded.Length > 22))
            {
                throw new FormatException($"{nameof( encoded )} is not 22 characters long");
            }

            if (options.Padding && (encoded.Length < 24 || encoded.Length > 24))
            {
                throw new FormatException($"{nameof( encoded )} is not 24 characters long");
            }

            Regex regex;

            if (!options.StandardBase64Encoding && !options.Padding)
            {
                regex = new Regex(@"^[a-zA-Z0-9-_]*$");
            }
            else if (options.StandardBase64Encoding && !options.Padding)
            {
                regex = new Regex(@"^[a-zA-Z0-9\+/]*$");
            }
            else if (!options.StandardBase64Encoding && options.Padding)
            {
                regex = new Regex(@"^[a-zA-Z0-9-_]*={0,2}$");
            }
            else
            {
                regex = new Regex(@"^[a-zA-Z0-9\+/]*={0,2}$");
            }

            if (!regex.IsMatch(encoded))
            {
                throw new FormatException($"{nameof( encoded )} is not encoded correctly");
            }

            if (!options.StandardBase64Encoding)
            {
                encoded = encoded.Replace("_", "/");
                encoded = encoded.Replace("-", "+");
            }

            if (options.Padding)
            {
                return(Convert.FromBase64String(encoded));
            }

            return(Convert.FromBase64String(encoded + "=="));
        }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Base64Guid"/> class with an existing
 /// <see cref="System.Guid"/> instance and a configurable <see cref="Base64GuidOptionsBuilder"/>.
 /// </summary>
 /// <param name="guid">The <see cref="System.Guid"/> instance to initialise the <see cref="Base64Guid"/> instance with.</param>
 /// <param name="optionsAction">The action to configure the <see cref="Base64GuidOptions"/> for the instance.</param>
 public Base64Guid(Guid guid, Action <Base64GuidOptionsBuilder> optionsAction)
 {
     _options = BuildOptions(optionsAction);
     Guid     = guid;
 }
Пример #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Base64Guid"/> class by using the value
 /// represented by the specified string with a configurable <see cref="Base64GuidOptionsBuilder"/>.
 /// </summary>
 /// <param name="encoded">
 /// <para>A string that contains a Guid which satisfies one of the following regular expressions:</para>
 /// <para>- "^[a-zA-Z0-9-_]{22}$" if <paramref name="optionsAction"/> is not configured.</para>
 /// <para>- "^[a-zA-Z0-9\+/]{22}$" if <paramref name="optionsAction"/> is configured with UseStandardBase64Encoding().</para>
 /// <para>- "^[a-zA-Z0-9-_]{22}==$" if <paramref name="optionsAction"/> is configured with UsePadding().</para>
 /// <para>- "^[a-zA-Z0-9\+/]{22}==$" if <paramref name="optionsAction"/> is configured with
 /// UseStandardBase64Encoding() and UsePadding().</para>
 /// </param>
 /// <param name="optionsAction">The action to configure the <see cref="Base64GuidOptions"/> for the instance.</param>
 public Base64Guid(string encoded, Action <Base64GuidOptionsBuilder> optionsAction)
 {
     _options = BuildOptions(optionsAction);
     Guid     = new Guid(ParseToByteArray(encoded, _options));
 }
Пример #4
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="Base64Guid" /> class by using the value
 ///     represented by the specified string with a configurable <see cref="Base64GuidOptionsBuilder" />.
 /// </summary>
 /// <param name="encoded">
 ///     <para>A string that contains a Guid which satisfies one of the following regular expressions:</para>
 ///     <para>- "^[a-zA-Z0-9-_]{22}$" if <paramref name="optionsAction" /> is not configured.</para>
 ///     <para>- "^[a-zA-Z0-9\+/]{22}$" if <paramref name="optionsAction" /> is configured with UseStandardBase64Encoding().</para>
 ///     <para>- "^[a-zA-Z0-9-_]{22}==$" if <paramref name="optionsAction" /> is configured with UsePadding().</para>
 ///     <para>
 ///         - "^[a-zA-Z0-9\+/]{22}==$" if <paramref name="optionsAction" /> is configured with
 ///         UseStandardBase64Encoding() and UsePadding().
 ///     </para>
 /// </param>
 /// <param name="optionsAction">The action to configure the <see cref="Base64GuidOptions" /> for the instance.</param>
 public Base64Guid(String encoded, [NotNull] Action <Base64GuidOptionsBuilder> optionsAction)
 {
     this._options = BuildOptions(optionsAction);
     this.Guid     = new Guid(ParseToByteArray(encoded, this._options));
 }
Пример #5
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="Base64Guid" /> class with an existing
 ///     <see cref="System.Guid" /> instance and a configurable <see cref="Base64GuidOptionsBuilder" />.
 /// </summary>
 /// <param name="guid">The <see cref="System.Guid" /> instance to initialise the <see cref="Base64Guid" /> instance with.</param>
 /// <param name="optionsAction">The action to configure the <see cref="Base64GuidOptions" /> for the instance.</param>
 public Base64Guid(Guid guid, [NotNull] Action <Base64GuidOptionsBuilder> optionsAction)
 {
     this._options = BuildOptions(optionsAction);
     this.Guid     = guid;
 }