< Summary

Class:Azure.Security.KeyVault.Keys.KeyCurveName
Assembly:Azure.Security.KeyVault.Keys
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\KeyCurveName.cs
Covered lines:65
Uncovered lines:2
Coverable lines:67
Total lines:195
Line coverage:97% (65 of 67)
Covered branches:73
Total branches:80
Branch coverage:91.2% (73 of 80)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%50%
get_P256()-100%100%
get_P256K()-100%100%
get_P384()-100%100%
get_P521()-100%100%
.cctor()-100%100%
op_Equality(...)-100%100%
op_Inequality(...)-100%100%
op_Implicit(...)-100%100%
Equals(...)-0%0%
Equals(...)-100%100%
GetHashCode()-0%0%
ToString()-100%100%
FromOid(...)-100%95.24%
get_IsSupported()-100%100%
get_KeyParameterSize()-100%100%
get_KeySize()-100%100%
get_Oid()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\KeyCurveName.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.ComponentModel;
 6using System.Security.Cryptography;
 7
 8namespace Azure.Security.KeyVault.Keys
 9{
 10    /// <summary>
 11    /// Elliptic Curve Cryptography (ECC) curve names.
 12    /// </summary>
 13    public readonly struct KeyCurveName : IEquatable<KeyCurveName>
 14    {
 15        private const string P256Value = "P-256";
 16        private const string P256KValue = "P-256K";
 17        private const string P384Value = "P-384";
 18        private const string P521Value = "P-521";
 19
 20        private const string P256OidValue = "1.2.840.10045.3.1.7";
 21        private const string P256KOidValue = "1.3.132.0.10";
 22        private const string P384OidValue = "1.3.132.0.34";
 23        private const string P521OidValue = "1.3.132.0.35";
 24
 25        private readonly string _value;
 26
 27        /// <summary>
 28        /// Initializes a new instance of the <see cref="KeyCurveName"/> structure.
 29        /// </summary>
 30        /// <param name="value">The string value of the instance.</param>
 31        public KeyCurveName(string value)
 32        {
 52633            _value = value ?? throw new ArgumentNullException(nameof(value));
 52634        }
 35
 36        /// <summary>
 37        /// Gets the NIST P-256 elliptic curve, AKA SECG curve SECP256R1
 38        /// For more information, see <see href="https://docs.microsoft.com/azure/key-vault/keys/about-keys#curve-types"
 39        /// </summary>
 11040        public static KeyCurveName P256 { get; } = new KeyCurveName(P256Value);
 41
 42        /// <summary>
 43        /// Gets the SECG SECP256K1 elliptic curve.
 44        /// For more information, see <see href="https://docs.microsoft.com/azure/key-vault/keys/about-keys#curve-types"
 45        /// </summary>
 5846        public static KeyCurveName P256K { get; } = new KeyCurveName(P256KValue);
 47
 48        /// <summary>
 49        /// Gets the NIST P-384 elliptic curve, AKA SECG curve SECP384R1.
 50        /// For more information, see <see href="https://docs.microsoft.com/azure/key-vault/keys/about-keys#curve-types"
 51        /// </summary>
 8252        public static KeyCurveName P384 { get; } = new KeyCurveName(P384Value);
 53
 54        /// <summary>
 55        /// Gets the NIST P-521 elliptic curve, AKA SECG curve SECP521R1.
 56        /// For more information, see <see href="https://docs.microsoft.com/azure/key-vault/keys/about-keys#curve-types"
 57        /// </summary>
 10858        public static KeyCurveName P521 { get; } = new KeyCurveName(P521Value);
 59
 260        internal static readonly KeyCurveName s_default = default;
 61
 62        /// <summary>
 63        /// Determines if two <see cref="KeyCurveName"/> values are the same.
 64        /// </summary>
 65        /// <param name="left">The first <see cref="KeyCurveName"/> to compare.</param>
 66        /// <param name="right">The second <see cref="KeyCurveName"/> to compare.</param>
 67        /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are the same; otherwise, false.</retur
 5268        public static bool operator ==(KeyCurveName left, KeyCurveName right) => left.Equals(right);
 69
 70        /// <summary>
 71        /// Determines if two <see cref="KeyCurveName"/> values are different.
 72        /// </summary>
 73        /// <param name="left">The first <see cref="KeyCurveName"/> to compare.</param>
 74        /// <param name="right">The second <see cref="KeyCurveName"/> to compare.</param>
 75        /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are different; otherwise, false.</retu
 3876        public static bool operator !=(KeyCurveName left, KeyCurveName right) => !left.Equals(right);
 77
 78        /// <summary>
 79        /// Converts a string to a <see cref="KeyCurveName"/>.
 80        /// </summary>
 81        /// <param name="value">The string value to convert.</param>
 51882        public static implicit operator KeyCurveName(string value) => new KeyCurveName(value);
 83
 84        /// <inheritdoc/>
 85        [EditorBrowsable(EditorBrowsableState.Never)]
 086        public override bool Equals(object obj) => obj is KeyCurveName other && Equals(other);
 87
 88        /// <inheritdoc/>
 19889        public bool Equals(KeyCurveName other) => string.Equals(_value, other._value, StringComparison.Ordinal);
 90
 91        /// <inheritdoc/>
 92        [EditorBrowsable(EditorBrowsableState.Never)]
 093        public override int GetHashCode() => _value?.GetHashCode() ?? 0;
 94
 95        /// <inheritdoc/>
 30896        public override string ToString() => _value;
 97
 98        internal static KeyCurveName FromOid(Oid oid, int keySize = 0)
 99        {
 128100            if (!string.IsNullOrEmpty(oid?.Value))
 101            {
 10102                if (string.Equals(oid.Value, P521OidValue, StringComparison.Ordinal))
 103                {
 2104                    return P521;
 105                }
 106
 8107                if (string.Equals(oid.Value, P384OidValue, StringComparison.Ordinal))
 108                {
 2109                    return P384;
 110                }
 111
 6112                if (string.Equals(oid.Value, P256KOidValue, StringComparison.Ordinal))
 113                {
 2114                    return P256K;
 115                }
 116
 4117                if (string.Equals(oid.Value, P256OidValue, StringComparison.Ordinal))
 118                {
 2119                    return P256;
 120                }
 121            }
 122
 120123            if (!string.IsNullOrEmpty(oid?.FriendlyName))
 124            {
 114125                switch (keySize)
 126                {
 48127                    case 521 when string.Equals(oid.FriendlyName, "nistP521", StringComparison.OrdinalIgnoreCase)
 48128                               || string.Equals(oid.FriendlyName, "secp521r1", StringComparison.OrdinalIgnoreCase)
 48129                               || string.Equals(oid.FriendlyName, "ECDSA_P521", StringComparison.OrdinalIgnoreCase):
 48130                        return P521;
 131
 22132                    case 384 when string.Equals(oid.FriendlyName, "nistP384", StringComparison.OrdinalIgnoreCase)
 22133                               || string.Equals(oid.FriendlyName, "secp384r1", StringComparison.OrdinalIgnoreCase)
 22134                               || string.Equals(oid.FriendlyName, "ECDSA_P384", StringComparison.OrdinalIgnoreCase):
 22135                        return P384;
 136
 42137                    case 256 when string.Equals(oid.FriendlyName, "secp256k1", StringComparison.OrdinalIgnoreCase):
 14138                        return P256K;
 139
 28140                    case 256 when string.Equals(oid.FriendlyName, "nistP256", StringComparison.OrdinalIgnoreCase)
 28141                               || string.Equals(oid.FriendlyName, "secp256r1", StringComparison.OrdinalIgnoreCase)
 28142                               || string.Equals(oid.FriendlyName, "ECDSA_P256", StringComparison.OrdinalIgnoreCase):
 28143                        return P256;
 144                }
 145            }
 146
 8147            return s_default;
 148        }
 149
 150        internal bool IsSupported
 151        {
 152            get
 153            {
 124154                switch (_value)
 155                {
 156                    case P256Value:
 157                    case P256KValue:
 158                    case P384Value:
 159                    case P521Value:
 108160                        return true;
 161
 162                    default:
 16163                        return false;
 164                }
 165            }
 166        }
 167
 82168        internal int KeyParameterSize => _value switch
 82169        {
 106170            P256Value => 32,
 88171            P256KValue => 32,
 96172            P384Value => 48,
 98173            P521Value => 66,
 104174            _ => 0,
 82175        };
 262176        internal int KeySize => _value switch
 262177        {
 332178            P256Value => 256,
 304179            P256KValue => 256,
 328180            P384Value => 384,
 328181            P521Value => 521,
 280182            _ => 0,
 262183        };
 184
 90185        internal Oid Oid => _value switch
 90186        {
 118187            P256Value => new Oid(P256OidValue),
 96188            P256KValue => new Oid(P256KOidValue),
 108189            P384Value => new Oid(P384OidValue),
 110190            P521Value => new Oid(P521OidValue),
 108191            _ => null,
 90192        };
 193
 194    }
 195}