< Summary

Class:Microsoft.Azure.KeyVault.Cryptography.Algorithms.Rs256
Assembly:Microsoft.Azure.KeyVault.Cryptography
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault.Cryptography\src\Algorithms\Rs256.cs
Covered lines:15
Uncovered lines:7
Coverable lines:22
Total lines:97
Line coverage:68.1% (15 of 22)
Covered branches:10
Total branches:20
Branch coverage:50% (10 of 20)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor()-100%100%
CreateSignatureTransform(...)-100%100%
.ctor(...)-71.43%50%
Sign(...)-60%50%
Verify(...)-57.14%50%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault.Cryptography\src\Algorithms\Rs256.cs

#LineLine coverage
 1//
 2// Copyright (c) Microsoft Corporation. All rights reserved.
 3// Licensed under the MIT License. See License.txt in the project root for
 4// license information.
 5//
 6
 7using System;
 8using System.Security.Cryptography;
 9
 10namespace Microsoft.Azure.KeyVault.Cryptography.Algorithms
 11{
 12    /// <summary>
 13    /// RSA SHA-256 Signature algorithim.
 14    /// </summary>
 15    public class Rs256 : RsaSignature
 16    {
 17        public const string AlgorithmName = "RS256";
 18
 19        internal const string OID_OIWSEC_SHA256 = "2.16.840.1.101.3.4.2.1";
 20        internal const string OID_OIWSEC_SHA384 = "2.16.840.1.101.3.4.2.2";
 21        internal const string OID_OIWSEC_SHA512 = "2.16.840.1.101.3.4.2.3";
 22
 23        public Rs256()
 224            : base( AlgorithmName )
 25        {
 226        }
 27
 28        public override ISignatureTransform CreateSignatureTransform( AsymmetricAlgorithm key )
 29        {
 630            return new Rs256SignatureTransform( key );
 31        }
 32
 33        class Rs256SignatureTransform : ISignatureTransform
 34        {
 35            private RSA _key;
 36
 637            public Rs256SignatureTransform( AsymmetricAlgorithm key )
 38            {
 639                if ( key == null )
 040                    throw new ArgumentNullException( "key" );
 41
 642                if ( !( key is RSA ) )
 043                    throw new ArgumentException( string.Format( "key must be of type {0}", typeof( RSA ).AssemblyQualifi
 44
 645                _key = key as RSA;
 646            }
 47
 48            public byte[] Sign( byte[] digest )
 49            {
 250                if ( digest == null || digest.Length == 0 )
 051                    throw new ArgumentNullException( "digest" );
 52
 253                if ( digest.Length != 32 )
 054                    throw new ArgumentOutOfRangeException( "digest", "The digest must be 32 bytes for SHA-256" );
 55
 56#if FullNetFx
 57                if ( _key is RSACryptoServiceProvider )
 58                {
 59                    return ((RSACryptoServiceProvider)_key).SignHash( digest, OID_OIWSEC_SHA256 );
 60                }
 61
 62                throw new CryptographicException( string.Format( "{0} is not supported", _key.GetType().FullName ) );
 63#elif NETSTANDARD
 264                return _key.SignHash( digest, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1 );
 65#else
 66                #error Unknown Framework
 67#endif
 68            }
 69
 70            public bool Verify( byte[] digest, byte[] signature )
 71            {
 472                if ( digest == null || digest.Length == 0 )
 073                    throw new ArgumentNullException( "digest" );
 74
 475                if ( digest.Length != 32 )
 076                    throw new ArgumentOutOfRangeException( "digest", "The digest must be 32 bytes for SHA-256" );
 77
 478                if ( signature == null || signature.Length == 0 )
 079                    throw new ArgumentNullException( "signature" );
 80
 81
 82#if FullNetFx
 83                if ( _key is RSACryptoServiceProvider )
 84                {
 85                    return ((RSACryptoServiceProvider)_key).VerifyHash( digest, OID_OIWSEC_SHA256, signature );
 86                }
 87
 88                throw new CryptographicException( string.Format( "{0} is not supported", _key.GetType().FullName ) );
 89#elif NETSTANDARD
 490                return _key.VerifyHash( digest, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1 );
 91#else
 92                #error Unknown Framework
 93#endif
 94            }
 95        }
 96    }
 97}