< Summary

Class:Azure.Security.KeyVault.Keys.Cryptography.SignatureAlgorithm
Assembly:Azure.Security.KeyVault.Keys
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\Cryptography\SignatureAlgorithm.cs
Covered lines:32
Uncovered lines:7
Coverable lines:39
Total lines:213
Line coverage:82% (32 of 39)
Covered branches:93
Total branches:118
Branch coverage:78.8% (93 of 118)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%50%
get_RS256()-100%100%
get_RS384()-100%100%
get_RS512()-100%100%
get_PS256()-100%100%
get_PS384()-100%100%
get_PS512()-100%100%
get_ES256()-100%100%
get_ES384()-100%100%
get_ES512()-100%100%
get_ES256K()-100%100%
op_Equality(...)-0%100%
op_Inequality(...)-0%100%
op_Implicit(...)-100%100%
Equals(...)-0%0%
Equals(...)-100%100%
GetHashCode()-0%0%
ToString()-100%100%
GetHashAlgorithm()-80%69.57%
GetHashAlgorithmName()-100%91.3%
GetEcKeyCurveName()-83.33%87.5%
GetRsaSignaturePadding()-75%91.67%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\Cryptography\SignatureAlgorithm.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.Cryptography
 9{
 10    /// <summary>
 11    /// An algorithm used for signing and verification.
 12    /// </summary>
 13    public readonly struct SignatureAlgorithm : IEquatable<SignatureAlgorithm>
 14    {
 15        internal const string RS256Value = "RS256";
 16        internal const string RS384Value = "RS384";
 17        internal const string RS512Value = "RS512";
 18        internal const string PS256Value = "PS256";
 19        internal const string PS384Value = "PS384";
 20        internal const string PS512Value = "PS512";
 21        internal const string ES256Value = "ES256";
 22        internal const string ES384Value = "ES384";
 23        internal const string ES512Value = "ES512";
 24        internal const string ES256KValue = "ES256K";
 25
 26        private readonly string _value;
 27
 28        /// <summary>
 29        /// Initializes a new instance of the <see cref="SignatureAlgorithm"/> structure.
 30        /// </summary>
 31        /// <param name="value">The string value of the instance.</param>
 32        public SignatureAlgorithm(string value)
 33        {
 8434            _value = value ?? throw new ArgumentNullException(nameof(value));
 8435        }
 36
 37        /// <summary>
 38        /// Gets an RSA SHA-256 <see cref="SignatureAlgorithm"/>.
 39        /// </summary>
 4240        public static SignatureAlgorithm RS256 { get; } = new SignatureAlgorithm(RS256Value);
 41
 42        /// <summary>
 43        /// Gets an RSA SHA-384  <see cref="SignatureAlgorithm"/>.
 44        /// </summary>
 3445        public static SignatureAlgorithm RS384 { get; } = new SignatureAlgorithm(RS384Value);
 46
 47        /// <summary>
 48        /// Gets an RSA SHA-512  <see cref="SignatureAlgorithm"/>.
 49        /// </summary>
 3450        public static SignatureAlgorithm RS512 { get; } = new SignatureAlgorithm(RS512Value);
 51
 52        /// <summary>
 53        /// Gets an RSASSA-PSS using SHA-256 and MGF1 with SHA-256 <see cref="SignatureAlgorithm"/>.
 54        /// </summary>
 5455        public static SignatureAlgorithm PS256 { get; } = new SignatureAlgorithm(PS256Value);
 56
 57        /// <summary>
 58        /// Gets an RSASSA-PSS using SHA-384 and MGF1 with SHA-384 <see cref="SignatureAlgorithm"/>.
 59        /// </summary>
 5060        public static SignatureAlgorithm PS384 { get; } = new SignatureAlgorithm(PS384Value);
 61
 62        /// <summary>
 63        /// Gets an RSASSA-PSS using SHA-512 and MGF1 with SHA-512 <see cref="SignatureAlgorithm"/>.
 64        /// </summary>
 5065        public static SignatureAlgorithm PS512 { get; } = new SignatureAlgorithm(PS512Value);
 66
 67        /// <summary>
 68        /// Gets an ECDSA with a P-256 curve <see cref="SignatureAlgorithm"/>.
 69        /// </summary>
 4670        public static SignatureAlgorithm ES256 { get; } = new SignatureAlgorithm(ES256Value);
 71
 72        /// <summary>
 73        /// Gets an ECDSA with a P-384 curve <see cref="SignatureAlgorithm"/>.
 74        /// </summary>
 4675        public static SignatureAlgorithm ES384 { get; } = new SignatureAlgorithm(ES384Value);
 76
 77        /// <summary>
 78        /// Gets an ECDSA with a P-521 curve <see cref="SignatureAlgorithm"/>.
 79        /// </summary>
 4680        public static SignatureAlgorithm ES512 { get; } = new SignatureAlgorithm(ES512Value);
 81
 82        /// <summary>
 83        /// Gets an ECDSA with a secp256k1 curve <see cref="SignatureAlgorithm"/>.
 84        /// </summary>
 3085        public static SignatureAlgorithm ES256K { get; } = new SignatureAlgorithm(ES256KValue);
 86
 87        /// <summary>
 88        /// Determines if two <see cref="SignatureAlgorithm"/> values are the same.
 89        /// </summary>
 90        /// <param name="left">The first <see cref="SignatureAlgorithm"/> to compare.</param>
 91        /// <param name="right">The second <see cref="SignatureAlgorithm"/> to compare.</param>
 92        /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are the same; otherwise, false.</retur
 093        public static bool operator ==(SignatureAlgorithm left, SignatureAlgorithm right) => left.Equals(right);
 94
 95        /// <summary>
 96        /// Determines if two <see cref="SignatureAlgorithm"/> values are different.
 97        /// </summary>
 98        /// <param name="left">The first <see cref="SignatureAlgorithm"/> to compare.</param>
 99        /// <param name="right">The second <see cref="SignatureAlgorithm"/> to compare.</param>
 100        /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are different; otherwise, false.</retu
 0101        public static bool operator !=(SignatureAlgorithm left, SignatureAlgorithm right) => !left.Equals(right);
 102
 103        /// <summary>
 104        /// Converts a string to a <see cref="SignatureAlgorithm"/>.
 105        /// </summary>
 106        /// <param name="value">The string value to convert.</param>
 64107        public static implicit operator SignatureAlgorithm(string value) => new SignatureAlgorithm(value);
 108
 109        /// <inheritdoc/>
 110        [EditorBrowsable(EditorBrowsableState.Never)]
 0111        public override bool Equals(object obj) => obj is SignatureAlgorithm other && Equals(other);
 112
 113        /// <inheritdoc/>
 468114        public bool Equals(SignatureAlgorithm other) => string.Equals(_value, other._value, StringComparison.Ordinal);
 115
 116        /// <inheritdoc/>
 117        [EditorBrowsable(EditorBrowsableState.Never)]
 0118        public override int GetHashCode() => _value?.GetHashCode() ?? 0;
 119
 120        /// <inheritdoc/>
 1040121        public override string ToString() => _value;
 122
 123        internal HashAlgorithm GetHashAlgorithm()
 124        {
 312125            switch (_value)
 126            {
 127                case RS256Value:
 128                case PS256Value:
 129                case ES256Value:
 130                case ES256KValue:
 120131                    return SHA256.Create();
 132
 133                case RS384Value:
 134                case PS384Value:
 135                case ES384Value:
 96136                    return SHA384.Create();
 137
 138                case RS512Value:
 139                case PS512Value:
 140                case ES512Value:
 96141                    return SHA512.Create();
 142
 143                default:
 0144                    throw new InvalidOperationException("Invalid Algorithm");
 145            }
 146        }
 147
 148        internal HashAlgorithmName GetHashAlgorithmName()
 149        {
 56150            switch (_value)
 151            {
 152                case RS256Value:
 153                case PS256Value:
 154                case ES256Value:
 155                case ES256KValue:
 16156                    return HashAlgorithmName.SHA256;
 157
 158                case RS384Value:
 159                case PS384Value:
 160                case ES384Value:
 16161                    return HashAlgorithmName.SHA384;
 162
 163                case RS512Value:
 164                case PS512Value:
 165                case ES512Value:
 16166                    return HashAlgorithmName.SHA512;
 167                default:
 168
 8169                    return default;
 170            }
 171        }
 172
 173        internal KeyCurveName GetEcKeyCurveName()
 174        {
 90175            switch (_value)
 176            {
 177                case ES256Value:
 30178                    return KeyCurveName.P256;
 179
 180                case ES256KValue:
 12181                    return KeyCurveName.P256K;
 182
 183                case ES384Value:
 24184                    return KeyCurveName.P384;
 185
 186                case ES512Value:
 24187                    return KeyCurveName.P521;
 188
 189                default:
 0190                    return KeyCurveName.s_default;
 191            }
 192        }
 193
 194        internal RSASignaturePadding GetRsaSignaturePadding()
 195        {
 48196            switch (_value)
 197            {
 198                case RS256Value:
 199                case RS384Value:
 200                case RS512Value:
 24201                    return RSASignaturePadding.Pkcs1;
 202
 203                case PS256Value:
 204                case PS384Value:
 205                case PS512Value:
 24206                    return RSASignaturePadding.Pss;
 207
 208                default:
 0209                    return null;
 210            }
 211        }
 212    }
 213}