< Summary

Information
Class: Renci.SshNet.Security.Key
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Cryptography\Key.cs
Line coverage
79%
Covered lines: 19
Uncovered lines: 5
Coverable lines: 24
Total lines: 100
Line coverage: 79.1%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Comment()100%1100%
.ctor(...)75%485.71%
.ctor()100%1100%
Sign(...)100%1100%
VerifySignature(...)100%10%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Cryptography\Key.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3
 4using Renci.SshNet.Common;
 5using Renci.SshNet.Security.Cryptography;
 6
 7namespace Renci.SshNet.Security
 8{
 9    /// <summary>
 10    /// Base class for asymmetric cipher algorithms.
 11    /// </summary>
 12    public abstract class Key
 13    {
 14        /// <summary>
 15        /// Specifies array of big integers that represent private key.
 16        /// </summary>
 17#pragma warning disable SA1401 // Fields should be private
 18        protected BigInteger[] _privateKey;
 19#pragma warning restore SA1401 // Fields should be private
 20
 21        /// <summary>
 22        /// Gets the default digital signature implementation for this key.
 23        /// </summary>
 24        protected internal abstract DigitalSignature DigitalSignature { get; }
 25
 26        /// <summary>
 27        /// Gets or sets the public key.
 28        /// </summary>
 29        /// <value>
 30        /// The public.
 31        /// </value>
 32        public abstract BigInteger[] Public { get; set; }
 33
 34        /// <summary>
 35        /// Gets the length of the key.
 36        /// </summary>
 37        /// <value>
 38        /// The length of the key.
 39        /// </value>
 40        public abstract int KeyLength { get; }
 41
 42        /// <summary>
 43        /// Gets or sets the key comment.
 44        /// </summary>
 3345        public string Comment { get; set; }
 46
 47        /// <summary>
 48        /// Initializes a new instance of the <see cref="Key"/> class.
 49        /// </summary>
 50        /// <param name="data">DER encoded private key data.</param>
 49951        protected Key(byte[] data)
 49952        {
 49953            if (data is null)
 054            {
 055                throw new ArgumentNullException(nameof(data));
 56            }
 57
 49958            var der = new DerData(data);
 49959            _ = der.ReadBigInteger(); // skip version
 60
 49961            var keys = new List<BigInteger>();
 449162            while (!der.IsEndOfData)
 399263            {
 399264                keys.Add(der.ReadBigInteger());
 399265            }
 66
 49967            _privateKey = keys.ToArray();
 49968        }
 69
 70        /// <summary>
 71        /// Initializes a new instance of the <see cref="Key"/> class.
 72        /// </summary>
 129173        protected Key()
 129174        {
 129175        }
 76
 77        /// <summary>
 78        /// Signs the specified data with the key.
 79        /// </summary>
 80        /// <param name="data">The data to sign.</param>
 81        /// <returns>
 82        /// Signed data.
 83        /// </returns>
 84        public byte[] Sign(byte[] data)
 385        {
 386            return DigitalSignature.Sign(data);
 387        }
 88
 89        /// <summary>
 90        /// Verifies the signature.
 91        /// </summary>
 92        /// <param name="data">The data to verify.</param>
 93        /// <param name="signature">The signature to verify against.</param>
 94        /// <returns><see langword="true"/> is signature was successfully verifies; otherwise <see langword="false"/>.</
 95        public bool VerifySignature(byte[] data, byte[] signature)
 096        {
 097            return DigitalSignature.Verify(data, signature);
 098        }
 99    }
 100}