< Summary

Class:Azure.Security.KeyVault.Keys.Cryptography.RemoteCryptographyClient
Assembly:Azure.Security.KeyVault.Keys
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\Cryptography\RemoteCryptographyClient.cs
Covered lines:170
Uncovered lines:40
Coverable lines:210
Total lines:418
Line coverage:80.9% (170 of 210)
Covered branches:1
Total branches:2
Branch coverage:50% (1 of 2)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor()-100%100%
.ctor(...)-100%50%
.ctor(...)-100%100%
get_Pipeline()-100%100%
SupportsOperation(...)-100%100%
EncryptAsync()-76.92%100%
Encrypt(...)-76.92%100%
DecryptAsync()-76.92%100%
Decrypt(...)-76.92%100%
WrapKeyAsync()-76.92%100%
WrapKey(...)-76.92%100%
UnwrapKeyAsync()-100%100%
UnwrapKey(...)-76.92%100%
SignAsync()-76.92%100%
Sign(...)-76.92%100%
VerifyAsync()-78.57%100%
Verify(...)-78.57%100%
GetKeyAsync()-62.5%100%
GetKey(...)-62.5%100%
Azure.Security.KeyVault.Keys.Cryptography.ICryptographyProvider.get_ShouldRemote()-0%100%
Azure-Security-KeyVault-Keys-Cryptography-ICryptographyProvider-EncryptAsync()-100%100%
Azure.Security.KeyVault.Keys.Cryptography.ICryptographyProvider.Encrypt(...)-100%100%
Azure-Security-KeyVault-Keys-Cryptography-ICryptographyProvider-DecryptAsync()-100%100%
Azure.Security.KeyVault.Keys.Cryptography.ICryptographyProvider.Decrypt(...)-100%100%
Azure-Security-KeyVault-Keys-Cryptography-ICryptographyProvider-WrapKeyAsync()-100%100%
Azure.Security.KeyVault.Keys.Cryptography.ICryptographyProvider.WrapKey(...)-100%100%
Azure-Security-KeyVault-Keys-Cryptography-ICryptographyProvider-UnwrapKeyAsync()-100%100%
Azure.Security.KeyVault.Keys.Cryptography.ICryptographyProvider.UnwrapKey(...)-100%100%
Azure-Security-KeyVault-Keys-Cryptography-ICryptographyProvider-SignAsync()-100%100%
Azure.Security.KeyVault.Keys.Cryptography.ICryptographyProvider.Sign(...)-100%100%
Azure-Security-KeyVault-Keys-Cryptography-ICryptographyProvider-VerifyAsync()-100%100%
Azure.Security.KeyVault.Keys.Cryptography.ICryptographyProvider.Verify(...)-100%100%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using Azure.Core;
 5using Azure.Core.Pipeline;
 6using System;
 7using System.Threading;
 8using System.Threading.Tasks;
 9
 10namespace Azure.Security.KeyVault.Keys.Cryptography
 11{
 12    internal class RemoteCryptographyClient : ICryptographyProvider
 13    {
 14        private readonly Uri _keyId;
 15
 3616        protected RemoteCryptographyClient()
 17        {
 3618        }
 19
 28420        internal RemoteCryptographyClient(Uri keyId, TokenCredential credential, CryptographyClientOptions options)
 21        {
 28422            Argument.AssertNotNull(keyId, nameof(keyId));
 28423            Argument.AssertNotNull(credential, nameof(credential));
 24
 28425            _keyId = keyId;
 28426            options ??= new CryptographyClientOptions();
 28427            string apiVersion = options.GetVersionString();
 28
 28429            HttpPipeline pipeline = HttpPipelineBuilder.Build(options,
 28430                    new ChallengeBasedAuthenticationPolicy(credential));
 31
 28432            Pipeline = new KeyVaultPipeline(keyId, apiVersion, pipeline, new ClientDiagnostics(options));
 28433        }
 34
 1635        internal RemoteCryptographyClient(KeyVaultPipeline pipeline)
 36        {
 1637            Pipeline = pipeline;
 1638        }
 39
 150840        internal KeyVaultPipeline Pipeline { get; }
 41
 39642        public bool SupportsOperation(KeyOperation operation) => true;
 43
 44        public virtual async Task<Response<EncryptResult>> EncryptAsync(EncryptionAlgorithm algorithm, byte[] plaintext,
 45        {
 1246            var parameters = new KeyEncryptParameters()
 1247            {
 1248                Algorithm = algorithm.ToString(),
 1249                Value = plaintext,
 1250            };
 51
 1252            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(Encrypt)}");
 1253            scope.AddAttribute("key", _keyId);
 1254            scope.Start();
 55
 56            try
 57            {
 2458                return await Pipeline.SendRequestAsync(RequestMethod.Post, parameters, () => new EncryptResult { Algorit
 59            }
 060            catch (Exception e)
 61            {
 062                scope.Failed(e);
 063                throw;
 64            }
 1265        }
 66
 67        public virtual Response<EncryptResult> Encrypt(EncryptionAlgorithm algorithm, byte[] plaintext, CancellationToke
 68        {
 1269            var parameters = new KeyEncryptParameters()
 1270            {
 1271                Algorithm = algorithm.ToString(),
 1272                Value = plaintext,
 1273            };
 74
 1275            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(Encrypt)}");
 1276            scope.AddAttribute("key", _keyId);
 1277            scope.Start();
 78
 79            try
 80            {
 2481                return Pipeline.SendRequest(RequestMethod.Post, parameters, () => new EncryptResult { Algorithm = algori
 82            }
 083            catch (Exception e)
 84            {
 085                scope.Failed(e);
 086                throw;
 87            }
 1288        }
 89
 90        public virtual async Task<Response<DecryptResult>> DecryptAsync(EncryptionAlgorithm algorithm, byte[] ciphertext
 91        {
 1492            var parameters = new KeyEncryptParameters()
 1493            {
 1494                Algorithm = algorithm.ToString(),
 1495                Value = ciphertext,
 1496            };
 97
 1498            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(Decrypt)}");
 1499            scope.AddAttribute("key", _keyId);
 14100            scope.Start();
 101
 102            try
 103            {
 28104                return await Pipeline.SendRequestAsync(RequestMethod.Post, parameters, () => new DecryptResult { Algorit
 105            }
 0106            catch (Exception e)
 107            {
 0108                scope.Failed(e);
 0109                throw;
 110            }
 14111        }
 112
 113        public virtual Response<DecryptResult> Decrypt(EncryptionAlgorithm algorithm, byte[] ciphertext, CancellationTok
 114        {
 14115            var parameters = new KeyEncryptParameters()
 14116            {
 14117                Algorithm = algorithm.ToString(),
 14118                Value = ciphertext,
 14119            };
 120
 14121            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(Decrypt)}");
 14122            scope.AddAttribute("key", _keyId);
 14123            scope.Start();
 124
 125            try
 126            {
 28127                return Pipeline.SendRequest(RequestMethod.Post, parameters, () => new DecryptResult { Algorithm = algori
 128            }
 0129            catch (Exception e)
 130            {
 0131                scope.Failed(e);
 0132                throw;
 133            }
 14134        }
 135
 136        public virtual async Task<Response<WrapResult>> WrapKeyAsync(KeyWrapAlgorithm algorithm, byte[] key, Cancellatio
 137        {
 14138            var parameters = new KeyWrapParameters()
 14139            {
 14140                Algorithm = algorithm.ToString(),
 14141                Key = key
 14142            };
 143
 14144            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(WrapKey)}");
 14145            scope.AddAttribute("key", _keyId);
 14146            scope.Start();
 147
 148            try
 149            {
 28150                return await Pipeline.SendRequestAsync(RequestMethod.Post, parameters, () => new WrapResult { Algorithm 
 151            }
 0152            catch (Exception e)
 153            {
 0154                scope.Failed(e);
 0155                throw;
 156            }
 14157        }
 158
 159        public virtual Response<WrapResult> WrapKey(KeyWrapAlgorithm algorithm, byte[] key, CancellationToken cancellati
 160        {
 14161            var parameters = new KeyWrapParameters()
 14162            {
 14163                Algorithm = algorithm.ToString(),
 14164                Key = key
 14165            };
 166
 14167            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(WrapKey)}");
 14168            scope.AddAttribute("key", _keyId);
 14169            scope.Start();
 170
 171            try
 172            {
 28173                return Pipeline.SendRequest(RequestMethod.Post, parameters, () => new WrapResult { Algorithm = algorithm
 174            }
 0175            catch (Exception e)
 176            {
 0177                scope.Failed(e);
 0178                throw;
 179            }
 14180        }
 181
 182        public virtual async Task<Response<UnwrapResult>> UnwrapKeyAsync(KeyWrapAlgorithm algorithm, byte[] encryptedKey
 183        {
 22184            var parameters = new KeyWrapParameters()
 22185            {
 22186                Algorithm = algorithm.ToString(),
 22187                Key = encryptedKey
 22188            };
 189
 22190            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(UnwrapKey)}"
 22191            scope.AddAttribute("key", _keyId);
 22192            scope.Start();
 193
 194            try
 195            {
 40196                return await Pipeline.SendRequestAsync(RequestMethod.Post, parameters, () => new UnwrapResult { Algorith
 197            }
 4198            catch (Exception e)
 199            {
 4200                scope.Failed(e);
 4201                throw;
 202            }
 18203        }
 204
 205        public virtual Response<UnwrapResult> UnwrapKey(KeyWrapAlgorithm algorithm, byte[] encryptedKey, CancellationTok
 206        {
 18207            var parameters = new KeyWrapParameters()
 18208            {
 18209                Algorithm = algorithm.ToString(),
 18210                Key = encryptedKey
 18211            };
 212
 18213            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(UnwrapKey)}"
 18214            scope.AddAttribute("key", _keyId);
 18215            scope.Start();
 216
 217            try
 218            {
 36219                return Pipeline.SendRequest(RequestMethod.Post, parameters, () => new UnwrapResult { Algorithm = algorit
 220            }
 0221            catch (Exception e)
 222            {
 0223                scope.Failed(e);
 0224                throw;
 225            }
 18226        }
 227
 228        public virtual async Task<Response<SignResult>> SignAsync(SignatureAlgorithm algorithm, byte[] digest, Cancellat
 229        {
 110230            var parameters = new KeySignParameters
 110231            {
 110232                Algorithm = algorithm.ToString(),
 110233                Digest = digest
 110234            };
 235
 110236            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(Sign)}");
 110237            scope.AddAttribute("key", _keyId);
 110238            scope.Start();
 239
 240            try
 241            {
 220242                return await Pipeline.SendRequestAsync(RequestMethod.Post, parameters, () => new SignResult { Algorithm 
 243            }
 0244            catch (Exception e)
 245            {
 0246                scope.Failed(e);
 0247                throw;
 248            }
 110249        }
 250
 251        public virtual Response<SignResult> Sign(SignatureAlgorithm algorithm, byte[] digest, CancellationToken cancella
 252        {
 110253            var parameters = new KeySignParameters
 110254            {
 110255                Algorithm = algorithm.ToString(),
 110256                Digest = digest
 110257            };
 258
 110259            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(Sign)}");
 110260            scope.AddAttribute("key", _keyId);
 110261            scope.Start();
 262
 263            try
 264            {
 220265                return Pipeline.SendRequest(RequestMethod.Post, parameters, () => new SignResult { Algorithm = algorithm
 266            }
 0267            catch (Exception e)
 268            {
 0269                scope.Failed(e);
 0270                throw;
 271            }
 110272        }
 273
 274        public virtual async Task<Response<VerifyResult>> VerifyAsync(SignatureAlgorithm algorithm, byte[] digest, byte[
 275        {
 106276            var parameters = new KeyVerifyParameters
 106277            {
 106278                Algorithm = algorithm.ToString(),
 106279                Digest = digest,
 106280                Signature = signature
 106281            };
 282
 106283            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(Verify)}");
 106284            scope.AddAttribute("key", _keyId);
 106285            scope.Start();
 286
 287            try
 288            {
 212289                return await Pipeline.SendRequestAsync(RequestMethod.Post, parameters, () => new VerifyResult { Algorith
 290            }
 0291            catch (Exception e)
 292            {
 0293                scope.Failed(e);
 0294                throw;
 295            }
 106296        }
 297
 298        public virtual Response<VerifyResult> Verify(SignatureAlgorithm algorithm, byte[] digest, byte[] signature, Canc
 299        {
 106300            var parameters = new KeyVerifyParameters
 106301            {
 106302                Algorithm = algorithm.ToString(),
 106303                Digest = digest,
 106304                Signature = signature
 106305            };
 306
 106307            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(Verify)}");
 106308            scope.AddAttribute("key", _keyId);
 106309            scope.Start();
 310
 311            try
 312            {
 212313                return Pipeline.SendRequest(RequestMethod.Post, parameters, () => new VerifyResult { Algorithm = algorit
 314            }
 0315            catch (Exception e)
 316            {
 0317                scope.Failed(e);
 0318                throw;
 319            }
 106320        }
 321
 322        internal virtual async Task<Response<KeyVaultKey>> GetKeyAsync(CancellationToken cancellationToken = default)
 323        {
 30324            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(GetKey)}");
 30325            scope.AddAttribute("key", _keyId);
 30326            scope.Start();
 327
 328            try
 329            {
 60330                return await Pipeline.SendRequestAsync(RequestMethod.Get, () => new KeyVaultKey(), cancellationToken).Co
 331            }
 0332            catch (Exception e)
 333            {
 0334                scope.Failed(e);
 0335                throw;
 336            }
 30337        }
 338
 339        internal virtual Response<KeyVaultKey> GetKey(CancellationToken cancellationToken = default)
 340        {
 30341            using DiagnosticScope scope = Pipeline.CreateScope($"{nameof(RemoteCryptographyClient)}.{nameof(GetKey)}");
 30342            scope.AddAttribute("key", _keyId);
 30343            scope.Start();
 344
 345            try
 346            {
 60347                return Pipeline.SendRequest(RequestMethod.Get, () => new KeyVaultKey(), cancellationToken);
 348            }
 0349            catch (Exception e)
 350            {
 0351                scope.Failed(e);
 0352                throw;
 353            }
 30354        }
 355
 0356        bool ICryptographyProvider.ShouldRemote => false;
 357
 358        async Task<EncryptResult> ICryptographyProvider.EncryptAsync(EncryptionAlgorithm algorithm, byte[] plaintext, Ca
 359        {
 8360            return await EncryptAsync(algorithm, plaintext, cancellationToken).ConfigureAwait(false);
 8361        }
 362
 363        EncryptResult ICryptographyProvider.Encrypt(EncryptionAlgorithm algorithm, byte[] plaintext, CancellationToken c
 364        {
 8365            return Encrypt(algorithm, plaintext, cancellationToken);
 366        }
 367
 368        async Task<DecryptResult> ICryptographyProvider.DecryptAsync(EncryptionAlgorithm algorithm, byte[] ciphertext, C
 369        {
 8370            return await DecryptAsync(algorithm, ciphertext, cancellationToken).ConfigureAwait(false);
 8371        }
 372
 373        DecryptResult ICryptographyProvider.Decrypt(EncryptionAlgorithm algorithm, byte[] ciphertext, CancellationToken 
 374        {
 8375            return Decrypt(algorithm, ciphertext, cancellationToken);
 376        }
 377
 378        async Task<WrapResult> ICryptographyProvider.WrapKeyAsync(KeyWrapAlgorithm algorithm, byte[] key, CancellationTo
 379        {
 8380            return await WrapKeyAsync(algorithm, key, cancellationToken).ConfigureAwait(false);
 8381        }
 382
 383        WrapResult ICryptographyProvider.WrapKey(KeyWrapAlgorithm algorithm, byte[] key, CancellationToken cancellationT
 384        {
 8385            return WrapKey(algorithm, key, cancellationToken);
 386        }
 387
 388        async Task<UnwrapResult> ICryptographyProvider.UnwrapKeyAsync(KeyWrapAlgorithm algorithm, byte[] encryptedKey, C
 389        {
 12390            return await UnwrapKeyAsync(algorithm, encryptedKey, cancellationToken).ConfigureAwait(false);
 8391        }
 392
 393        UnwrapResult ICryptographyProvider.UnwrapKey(KeyWrapAlgorithm algorithm, byte[] encryptedKey, CancellationToken 
 394        {
 8395            return UnwrapKey(algorithm, encryptedKey, cancellationToken);
 396        }
 397
 398        async Task<SignResult> ICryptographyProvider.SignAsync(SignatureAlgorithm algorithm, byte[] digest, Cancellation
 399        {
 82400            return await SignAsync(algorithm, digest, cancellationToken).ConfigureAwait(false);
 82401        }
 402
 403        SignResult ICryptographyProvider.Sign(SignatureAlgorithm algorithm, byte[] digest, CancellationToken cancellatio
 404        {
 82405            return Sign(algorithm, digest, cancellationToken);
 406        }
 407
 408        async Task<VerifyResult> ICryptographyProvider.VerifyAsync(SignatureAlgorithm algorithm, byte[] digest, byte[] s
 409        {
 118410            return await VerifyAsync(algorithm, digest, signature, cancellationToken).ConfigureAwait(false);
 118411        }
 412
 413        VerifyResult ICryptographyProvider.Verify(SignatureAlgorithm algorithm, byte[] digest, byte[] signature, Cancell
 414        {
 82415            return Verify(algorithm, digest, signature, cancellationToken);
 416        }
 417    }
 418}