< Summary

Class:Microsoft.Azure.KeyVault.KeyVaultKey
Assembly:Microsoft.Azure.KeyVault.Extensions
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault.Extensions\src\KeyVaultKey.cs
Covered lines:18
Uncovered lines:40
Coverable lines:58
Total lines:170
Line coverage:31% (18 of 58)
Covered branches:9
Total branches:38
Branch coverage:23.6% (9 of 38)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-77.78%50%
get_DefaultEncryptionAlgorithm()-0%0%
get_DefaultKeyWrapAlgorithm()-0%0%
get_DefaultSignatureAlgorithm()-0%0%
get_Kid()-66.67%50%
DecryptAsync(...)-0%0%
EncryptAsync(...)-0%0%
WrapKeyAsync(...)-0%0%
UnwrapKeyAsync(...)-0%0%
SignAsync(...)-0%0%
VerifyAsync(...)-0%0%
Dispose()-80%50%
Dispose(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault.Extensions\src\KeyVaultKey.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for
 3// license information.
 4
 5using System;
 6using System.Globalization;
 7using System.Threading;
 8using System.Threading.Tasks;
 9using Microsoft.Azure.KeyVault.Core;
 10using Microsoft.Azure.KeyVault.WebKey;
 11using Microsoft.Azure.KeyVault.Models;
 12
 13namespace Microsoft.Azure.KeyVault
 14{
 15    /// <summary>
 16    /// Key Vault key that performs cryptography operations at REST
 17    /// </summary>
 18    internal class KeyVaultKey : IKey
 19    {
 20        private readonly IKeyVaultClient _client;
 21        private          IKey           _implementation;
 22
 823        internal KeyVaultKey( IKeyVaultClient client, KeyBundle keyBundle )
 24        {
 825            switch ( keyBundle.Key.Kty )
 26            {
 27                case JsonWebKeyType.Rsa:
 828                    _implementation = new RsaKey( keyBundle.Key.Kid, keyBundle.Key.ToRSA() );
 829                    break;
 30
 31                case JsonWebKeyType.RsaHsm:
 032                    _implementation = new RsaKey( keyBundle.Key.Kid, keyBundle.Key.ToRSA() );
 33                    break;
 34            }
 35
 836            if ( _implementation == null )
 037                throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, "The key type \"{0}\" is not s
 38
 839            _client = client;
 840        }
 41
 42        public string DefaultEncryptionAlgorithm
 43        {
 44            get
 45            {
 046                if ( _implementation == null )
 047                    throw new ObjectDisposedException( "KeyVaultKey" );
 48
 049                return _implementation.DefaultEncryptionAlgorithm;
 50            }
 51        }
 52
 53        public string DefaultKeyWrapAlgorithm
 54        {
 55            get
 56            {
 057                if ( _implementation == null )
 058                    throw new ObjectDisposedException( "KeyVaultKey" );
 59
 060                return _implementation.DefaultKeyWrapAlgorithm;
 61            }
 62        }
 63
 64        public string DefaultSignatureAlgorithm
 65        {
 66            get
 67            {
 068                if ( _implementation == null )
 069                    throw new ObjectDisposedException( "KeyVaultKey" );
 70
 071                return _implementation.DefaultSignatureAlgorithm;
 72            }
 73        }
 74
 75        public string Kid
 76        {
 77            get
 78            {
 879                if ( _implementation == null )
 080                    throw new ObjectDisposedException( "KeyVaultKey" );
 81
 882                return _implementation.Kid;
 83            }
 84        }
 85
 86        public Task<byte[]> DecryptAsync( byte[] ciphertext, byte[] iv, byte[] authenticationData, byte[] authentication
 87        {
 088            if ( _implementation == null )
 089                throw new ObjectDisposedException( "KeyVaultKey" );
 90
 091            if ( string.IsNullOrWhiteSpace( algorithm ) )
 092                algorithm = DefaultEncryptionAlgorithm;
 93
 94            // Never local
 095            return _client.DecryptAsync( _implementation.Kid, algorithm, ciphertext, token )
 096                    .ContinueWith( result => result.Result.Result, token );
 97        }
 98
 99        public Task<Tuple<byte[], byte[], string>> EncryptAsync( byte[] plaintext, byte[] iv, byte[] authenticationData,
 100        {
 0101            if ( _implementation == null )
 0102                throw new ObjectDisposedException( "KeyVaultKey" );
 103
 0104            return _implementation.EncryptAsync( plaintext, iv, authenticationData, algorithm, token );
 105        }
 106
 107        public Task<Tuple<byte[], string>> WrapKeyAsync( byte[] plaintext, string algorithm = null, CancellationToken to
 108        {
 0109            if ( _implementation == null )
 0110                throw new ObjectDisposedException( "KeyVaultKey" );
 111
 0112            return _implementation.WrapKeyAsync( plaintext, algorithm, token );
 113        }
 114
 115        public Task<byte[]> UnwrapKeyAsync( byte[] ciphertext, string algorithm = null, CancellationToken token = defaul
 116        {
 0117            if ( _implementation == null )
 0118                throw new ObjectDisposedException( "KeyVaultKey" );
 119
 0120            if ( string.IsNullOrWhiteSpace( algorithm ) )
 0121                algorithm = DefaultKeyWrapAlgorithm;
 122
 123            // Never local
 0124            return _client.UnwrapKeyAsync( _implementation.Kid, algorithm, ciphertext, token )
 0125                    .ContinueWith( result => result.Result.Result, token );
 126        }
 127
 128        public Task<Tuple<byte[], string>> SignAsync( byte[] digest, string algorithm = null, CancellationToken token = 
 129        {
 0130            if ( _implementation == null )
 0131                throw new ObjectDisposedException( "KeyVaultKey" );
 132
 0133            if ( string.IsNullOrWhiteSpace( algorithm ) )
 0134                algorithm = DefaultSignatureAlgorithm;
 135
 136            // Never local
 0137            return _client.SignAsync( _implementation.Kid, algorithm, digest, token )
 0138                .ContinueWith( result => new Tuple<byte[], string>( result.Result.Result, algorithm ), token );
 139        }
 140
 141        public Task<bool> VerifyAsync( byte[] digest, byte[] signature, string algorithm = null, CancellationToken token
 142        {
 0143            if ( _implementation == null )
 0144                throw new ObjectDisposedException( "KeyVaultKey" );
 145
 0146            return _implementation.VerifyAsync( digest, signature, algorithm, token );
 147        }
 148
 149        public void Dispose()
 150        {
 4151            if ( _implementation == null )
 0152                throw new ObjectDisposedException( "KeyVaultKey" );
 153
 4154            Dispose( true );
 4155            GC.SuppressFinalize( this );
 4156        }
 157
 158        private void Dispose( bool disposing )
 159        {
 4160            if ( disposing )
 161            {
 4162                if ( _implementation != null )
 163                {
 4164                    _implementation.Dispose();
 4165                    _implementation = null;
 166                }
 167            }
 4168        }
 169    }
 170}