コード例 #1
0
        /// <summary>Parses the <see cref="ReadOnlySpan{T}"/> into its <see cref="EncryptionAlgorithm"/> representation.</summary>
        public static bool TryParse(ReadOnlySpan <byte> value, [NotNullWhen(true)] out EncryptionAlgorithm?algorithm)
        {
            if (value.Length == 13)
            {
                switch (IntegerMarshal.ReadUInt64(value))
                {
                case _A128CBC_ when IntegerMarshal.ReadUInt64(value, 5) == _BC_HS256:
                    algorithm = A128CbcHS256;

                    goto Found;

                case _A192CBC_ when IntegerMarshal.ReadUInt64(value, 5) == _BC_HS384:
                    algorithm = A192CbcHS384;

                    goto Found;

                case _A256CBC_ when IntegerMarshal.ReadUInt64(value, 5) == _BC_HS512:
                    algorithm = A256CbcHS512;

                    goto Found;
                }
            }
            else if (value.Length == 7)
            {
                switch (IntegerMarshal.ReadUInt56(value))
                {
                case _A128GCM:
                    algorithm = A128Gcm;
                    goto Found;

                case _A192GCM:
                    algorithm = A192Gcm;
                    goto Found;

                case _A256GCM:
                    algorithm = A256Gcm;
                    goto Found;
                }
            }

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

Found:
            return(true);
        }
コード例 #2
0
ファイル: EncryptionAlgorithm.cs プロジェクト: watfordgnf/Jwt
        /// <summary>Parses the <see cref="ReadOnlySpan{T}"/> into its <see cref="EncryptionAlgorithm"/> representation.</summary>
        /// <param name="value"></param>
        /// <param name="algorithm"></param>
        public static bool TryParse(ReadOnlySpan <byte> value, [NotNullWhen(true)] out EncryptionAlgorithm?algorithm)
        {
            if (value.Length == 13)
            {
                switch (IntegerMarshal.ReadUInt64(value))
                {
                case _A128CBC_ when IntegerMarshal.ReadUInt64(value, 5) == _BC_HS256:
                    algorithm = A128CbcHS256;

                    goto Found;

                case _A192CBC_ when IntegerMarshal.ReadUInt64(value, 5) == _BC_HS384:
                    algorithm = A192CbcHS384;

                    goto Found;

                case _A256CBC_ when IntegerMarshal.ReadUInt64(value, 5) == _BC_HS512:
                    algorithm = A256CbcHS512;

                    goto Found;
                }
            }
            else if (value.Length == 7)
            {
                switch (IntegerMarshal.ReadUInt56(value))
                {
                case _A128GCM:
                    algorithm = A128Gcm;
                    goto Found;

                case _A192GCM:
                    algorithm = A192Gcm;
                    goto Found;

                case _A256GCM:
                    algorithm = A256Gcm;
                    goto Found;
                }
            }

            algorithm = null;
            return(false);

Found:
            return(true);
        }
コード例 #3
0
        /// <summary>
        /// Cast the <see cref="ReadOnlySpan{T}"/> into its <see cref="EncryptionAlgorithm"/> representation.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="algorithm"></param>
        public static bool TryParse(ReadOnlySpan <byte> value, [NotNullWhen(true)] out EncryptionAlgorithm?algorithm)
        {
            if (value.Length == 13)
            {
                ref byte refValue = ref MemoryMarshal.GetReference(value);
                ulong    endValue = IntegerMarshal.ReadUInt64(ref refValue, 5);
                switch (IntegerMarshal.ReadUInt64(ref refValue))
                {
                case A128CBC_ when endValue == BC_HS256:
                    algorithm = Aes128CbcHmacSha256;
                    return(true);

                case A192CBC_ when endValue == BC_HS384:
                    algorithm = Aes192CbcHmacSha384;
                    return(true);

                case A256CBC_ when endValue == BC_HS512:
                    algorithm = Aes256CbcHmacSha512;
                    return(true);
                }
            }
コード例 #4
0
        /// <summary>Parses the <see cref="ReadOnlySpan{T}"/> into its <see cref="KeyManagementAlgorithm"/> representation.</summary>
        public static bool TryParse(ReadOnlySpan <byte> value, [NotNullWhen(true)] out KeyManagementAlgorithm?algorithm)
        {
            switch (value.Length)
            {
            case 3 when IntegerMarshal.ReadUInt24(value) == _dir:
                algorithm = Dir;

                goto Found;

            case 6 when IntegerMarshal.ReadUInt16(value, 4) == _KW:
                switch (IntegerMarshal.ReadUInt32(value))
                {
                case _A128:
                    algorithm = A128KW;
                    goto Found;

                case _A192:
                    algorithm = A192KW;
                    goto Found;

                case _A256:
                    algorithm = A256KW;
                    goto Found;
                }

                break;

            case 6 when IntegerMarshal.ReadUInt32(value) == _RSA1 && IntegerMarshal.ReadUInt16(value, 4) == __5:
                algorithm = Rsa1_5;

                goto Found;

            case 7 when IntegerMarshal.ReadUInt56(value) == _ECDH_ES:
                algorithm = EcdhEs;

                goto Found;

            case 8 when IntegerMarshal.ReadUInt64(value) == _RSA_OAEP:
                algorithm = RsaOaep;

                goto Found;

            case 9 when IntegerMarshal.ReadUInt8(value) == (byte) 'A':
                switch (IntegerMarshal.ReadUInt64(value, 1))
                {
                case __128GCMKW:
                    algorithm = A128GcmKW;
                    goto Found;

                case __192GCMKW:
                    algorithm = A192GcmKW;
                    goto Found;

                case __256GCMKW:
                    algorithm = A256GcmKW;
                    goto Found;
                }

                break;

            case 12 when IntegerMarshal.ReadUInt64(value) == _RSA_OAEP:
                switch (IntegerMarshal.ReadUInt32(value, 8))
                {
                case __256:
                    algorithm = RsaOaep256;
                    goto Found;

                case __384:
                    algorithm = RsaOaep384;
                    goto Found;

                case __512:
                    algorithm = RsaOaep512;
                    goto Found;
                }

                break;

            case 14 when IntegerMarshal.ReadUInt64(value) == _ECDH_ES_:
                switch (IntegerMarshal.ReadUInt64(value, 6))
                {
                case _S_A128KW:
                    algorithm = EcdhEsA128KW;
                    goto Found;

                case _S_A192KW:
                    algorithm = EcdhEsA192KW;
                    goto Found;

                case _S_A256KW:
                    algorithm = EcdhEsA256KW;
                    goto Found;
                }

                break;

            // 'PBES2-HS384+A192KW'
            case 18 when IntegerMarshal.ReadUInt64(value) == 6001096197639848528uL &&  /* PBES2-HS 384+A192KW*/
                IntegerMarshal.ReadUInt16(value, 16) == _KW:
                switch (IntegerMarshal.ReadUInt64(value, 8))
                {
                case 4049353170927105330uL:
                    algorithm = Pbes2HS256A128KW;
                    goto Found;

                case 3618977931536382003uL:
                    algorithm = Pbes2HS384A192KW;
                    goto Found;

                case 3906083507292746037:
                    algorithm = Pbes2HS512A256KW;
                    goto Found;
                }

                break;

            // Special case for escaped 'ECDH-ES\u002bAxxxKW'
            case 19 when IntegerMarshal.ReadUInt64(value) == _ECDH_ES_UTF8 /* ECDH-ES\ */:
                switch (IntegerMarshal.ReadUInt64(value, 8) | u002bUpperMask)
                {
                case _u002bA12 when IntegerMarshal.ReadUInt32(value, 15) == __28KW:
                    algorithm = EcdhEsA128KW;

                    goto Found;

                case _u002bA19 when IntegerMarshal.ReadUInt32(value, 15) == __92KW:
                    algorithm = EcdhEsA192KW;

                    goto Found;

                case _u002bA25 when IntegerMarshal.ReadUInt32(value, 15) == __56KW:
                    algorithm = EcdhEsA256KW;

                    goto Found;
                }

                break;
            }

            algorithm = null;
            return(false);

            Found:
            return(true);
        }
コード例 #5
0
        /// <summary>
        /// Cast the <see cref="ReadOnlySpan{T}"/> into its <see cref="KeyManagementAlgorithm"/> representation.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="algorithm"></param>
        public static bool TryParse(ReadOnlySpan <byte> value, [NotNullWhen(true)] out KeyManagementAlgorithm?algorithm)
        {
            switch (value.Length)
            {
            case 3 when IntegerMarshal.ReadUInt24(value) == dir:
                algorithm = Direct;

                return(true);

            case 6 when IntegerMarshal.ReadUInt16(value, 4) == KW:
                switch (IntegerMarshal.ReadUInt32(value))
                {
                case A128:
                    algorithm = Aes128KW;
                    return(true);

                case A192:
                    algorithm = Aes192KW;
                    return(true);

                case A256:
                    algorithm = Aes256KW;
                    return(true);
                }

                break;

            case 6 when IntegerMarshal.ReadUInt32(value) == RSA1 && IntegerMarshal.ReadUInt16(value, 4) == _5:
                algorithm = RsaPkcs1;

                return(true);

            case 7 when IntegerMarshal.ReadUInt56(value) == ECDH_ES:
                algorithm = EcdhEs;

                return(true);

            case 8 when IntegerMarshal.ReadUInt64(value) == RSA_OAEP:
                algorithm = RsaOaep;

                return(true);

            case 9 when IntegerMarshal.ReadUInt8(value) == (byte) 'A':
                switch (IntegerMarshal.ReadUInt64(value, 1))
                {
                case _128GCMKW:
                    algorithm = Aes128GcmKW;
                    return(true);

                case _192GCMKW:
                    algorithm = Aes192GcmKW;
                    return(true);

                case _256GCMKW:
                    algorithm = Aes256GcmKW;
                    return(true);
                }

                break;

            case 12 when IntegerMarshal.ReadUInt64(value) == RSA_OAEP:
                switch (IntegerMarshal.ReadUInt32(value, 8))
                {
                case _256:
                    algorithm = RsaOaep256;
                    return(true);

                case _384:
                    algorithm = RsaOaep384;
                    return(true);

                case _512:
                    algorithm = RsaOaep512;
                    return(true);
                }

                break;

            case 14 when IntegerMarshal.ReadUInt64(value) == ECDH_ES_:
                switch (IntegerMarshal.ReadUInt64(value, 6))
                {
                case S_A128KW:
                    algorithm = EcdhEsAes128KW;
                    return(true);

                case S_A192KW:
                    algorithm = EcdhEsAes192KW;
                    return(true);

                case S_A256KW:
                    algorithm = EcdhEsAes256KW;
                    return(true);
                }

                break;

            // Special case for escaped 'ECDH-ES\u002bAxxxKW'
            case 19 when IntegerMarshal.ReadUInt64(value) == ECDH_ES_UTF8 /* ECDH-ES\ */:
                switch (IntegerMarshal.ReadUInt64(value, 8))
                {
                case u002bA12 when IntegerMarshal.ReadUInt32(value, 15) == _28KW:
                    algorithm = EcdhEsAes128KW;

                    return(true);

                case u002bA19 when IntegerMarshal.ReadUInt32(value, 15) == _92KW:
                    algorithm = EcdhEsAes192KW;

                    return(true);

                case u002bA25 when IntegerMarshal.ReadUInt32(value, 15) == _56KW:
                    algorithm = EcdhEsAes256KW;

                    return(true);
                }

                break;
            }

            algorithm = null;
            return(false);
        }