Пример #1
0
        /// <summary>Parses the <see cref="ReadOnlySpan{T}"/> into its <see cref="SignatureAlgorithm"/> representation.</summary>
        public static bool TryParse(ReadOnlySpan <byte> value, [NotNullWhen(true)] out SignatureAlgorithm?algorithm)
        {
            if (value.Length == 5)
            {
                var first = IntegerMarshal.ReadUInt8(value);
                switch (IntegerMarshal.ReadUInt32(value, 1))
                {
                case _S256 when first == (byte)'H':
                    algorithm = HS256;
                    goto Found;

                case _S256 when first == (byte)'R':
                    algorithm = RS256;
                    goto Found;

                case _S256 when first == (byte)'E':
                    algorithm = ES256;
                    goto Found;

                case _S256 when first == (byte)'P':
                    algorithm = PS256;
                    goto Found;

                case _S384 when first == (byte)'H':
                    algorithm = HS384;
                    goto Found;

                case _S384 when first == (byte)'R':
                    algorithm = RS384;
                    goto Found;

                case _S384 when first == (byte)'E':
                    algorithm = ES384;
                    goto Found;

                case _S384 when first == (byte)'P':
                    algorithm = PS384;
                    goto Found;

                case _S512 when first == (byte)'H':
                    algorithm = HS512;
                    goto Found;

                case _S512 when first == (byte)'R':
                    algorithm = RS512;
                    goto Found;

                case _S512 when first == (byte)'E':
                    algorithm = ES512;
                    goto Found;

                case _S512 when first == (byte)'P':
                    algorithm = PS512;
                    goto Found;
                }
            }
            else if (value.Length == 4 && IntegerMarshal.ReadUInt32(value) == _none)
            {
                algorithm = None;
                goto Found;
            }
            else if (value.Length == 6 && IntegerMarshal.ReadUInt48(value) == _ES256K)
            {
                algorithm = ES256K;
                goto Found;
            }

#if NET5_0_OR_GREATER
            Unsafe.SkipInit(out algorithm);
#else
            algorithm = default;
#endif
            return(false);

            Found:
            return(true);
        }
Пример #2
0
        /// <summary>Parses the <see cref="ReadOnlySpan{T}"/> into its <see cref="SignatureAlgorithm"/> representation.</summary>
        /// <param name="value"></param>
        /// <param name="algorithm"></param>
        public static bool TryParse(ReadOnlySpan <byte> value, [NotNullWhen(true)] out SignatureAlgorithm?algorithm)
        {
            if (value.Length == 5)
            {
                var first = IntegerMarshal.ReadUInt8(value);
                switch (IntegerMarshal.ReadUInt32(value, 1))
                {
                case _S256 when first == (byte)'H':
                    algorithm = HS256;
                    goto Found;

                case _S256 when first == (byte)'R':
                    algorithm = RS256;
                    goto Found;

                case _S256 when first == (byte)'E':
                    algorithm = ES256;
                    goto Found;

                case _S256 when first == (byte)'P':
                    algorithm = PS256;
                    goto Found;

                case _S384 when first == (byte)'H':
                    algorithm = HS384;
                    goto Found;

                case _S384 when first == (byte)'R':
                    algorithm = RS384;
                    goto Found;

                case _S384 when first == (byte)'E':
                    algorithm = ES384;
                    goto Found;

                case _S384 when first == (byte)'P':
                    algorithm = PS384;
                    goto Found;

                case _S512 when first == (byte)'H':
                    algorithm = HS512;
                    goto Found;

                case _S512 when first == (byte)'R':
                    algorithm = RS512;
                    goto Found;

                case _S512 when first == (byte)'E':
                    algorithm = ES512;
                    goto Found;

                case _S512 when first == (byte)'P':
                    algorithm = PS512;
                    goto Found;
                }
            }
            else if (value.Length == 4 && IntegerMarshal.ReadUInt32(value) == _none)
            {
                algorithm = None;
                goto Found;
            }
            else if (value.Length == 6 && IntegerMarshal.ReadUInt48(value) == _ES256X)
            {
                algorithm = ES256X;
                goto Found;
            }

            algorithm = null;
            return(false);

            Found:
            return(true);
        }