< Summary

Information
Class: Renci.SshNet.Security.RsaKey
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Cryptography\RsaKey.cs
Line coverage
87%
Covered lines: 96
Uncovered lines: 14
Coverable lines: 110
Total lines: 313
Line coverage: 87.2%
Branch coverage
66%
Covered branches: 16
Total branches: 24
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
ToString()100%10%
get_Modulus()100%1100%
get_Exponent()100%1100%
get_D()100%2100%
get_P()50%283.33%
get_Q()50%283.33%
get_DP()50%283.33%
get_DQ()50%283.33%
get_InverseQ()50%283.33%
get_KeyLength()100%1100%
get_DigitalSignature()100%2100%
get_Public()100%1100%
set_Public(...)50%266.66%
.ctor()100%1100%
.ctor(...)50%266.66%
.ctor(...)100%1100%
PrimeExponent(...)100%1100%
Dispose()100%1100%
Dispose(...)83.33%686.66%
Finalize()100%1100%

File(s)

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

#LineLine coverage
 1using System;
 2using Renci.SshNet.Common;
 3using Renci.SshNet.Security.Cryptography;
 4
 5namespace Renci.SshNet.Security
 6{
 7    /// <summary>
 8    /// Contains the RSA private and public key.
 9    /// </summary>
 10    public class RsaKey : Key, IDisposable
 11    {
 12        private bool _isDisposed;
 13        private RsaDigitalSignature _digitalSignature;
 14
 15        /// <summary>
 16        /// Gets the name of the key.
 17        /// </summary>
 18        /// <returns>
 19        /// The name of the key.
 20        /// </returns>
 21        public override string ToString()
 022        {
 023            return "ssh-rsa";
 024        }
 25
 26        /// <summary>
 27        /// Gets the modulus.
 28        /// </summary>
 29        /// <value>
 30        /// The modulus.
 31        /// </value>
 32        public BigInteger Modulus
 33        {
 34            get
 988435            {
 988436                return _privateKey[0];
 988437            }
 38        }
 39
 40        /// <summary>
 41        /// Gets the exponent.
 42        /// </summary>
 43        /// <value>
 44        /// The exponent.
 45        /// </value>
 46        public BigInteger Exponent
 47        {
 48            get
 449449            {
 449450                return _privateKey[1];
 449451            }
 52        }
 53
 54        /// <summary>
 55        /// Gets the D.
 56        /// </summary>
 57        /// <value>
 58        /// The D.
 59        /// </value>
 60        public BigInteger D
 61        {
 62            get
 190663            {
 190664                if (_privateKey.Length > 2)
 69665                {
 69666                    return _privateKey[2];
 67                }
 68
 121069                return BigInteger.Zero;
 190670            }
 71        }
 72
 73        /// <summary>
 74        /// Gets the P.
 75        /// </summary>
 76        /// <value>
 77        /// The P.
 78        /// </value>
 79        public BigInteger P
 80        {
 81            get
 208882            {
 208883                if (_privateKey.Length > 3)
 208884                {
 208885                    return _privateKey[3];
 86                }
 87
 088                return BigInteger.Zero;
 208889            }
 90        }
 91
 92        /// <summary>
 93        /// Gets the Q.
 94        /// </summary>
 95        /// <value>
 96        /// The Q.
 97        /// </value>
 98        public BigInteger Q
 99        {
 100            get
 2088101            {
 2088102                if (_privateKey.Length > 4)
 2088103                {
 2088104                    return _privateKey[4];
 105                }
 106
 0107                return BigInteger.Zero;
 2088108            }
 109        }
 110
 111        /// <summary>
 112        /// Gets the DP.
 113        /// </summary>
 114        /// <value>
 115        /// The DP.
 116        /// </value>
 117        public BigInteger DP
 118        {
 119            get
 696120            {
 696121                if (_privateKey.Length > 5)
 696122                {
 696123                    return _privateKey[5];
 124                }
 125
 0126                return BigInteger.Zero;
 696127            }
 128        }
 129
 130        /// <summary>
 131        /// Gets the DQ.
 132        /// </summary>
 133        /// <value>
 134        /// The DQ.
 135        /// </value>
 136        public BigInteger DQ
 137        {
 138            get
 696139            {
 696140                if (_privateKey.Length > 6)
 696141                {
 696142                    return _privateKey[6];
 143                }
 144
 0145                return BigInteger.Zero;
 696146            }
 147        }
 148
 149        /// <summary>
 150        /// Gets the inverse Q.
 151        /// </summary>
 152        /// <value>
 153        /// The inverse Q.
 154        /// </value>
 155        public BigInteger InverseQ
 156        {
 157            get
 696158            {
 696159                if (_privateKey.Length > 7)
 696160                {
 696161                    return _privateKey[7];
 162                }
 163
 0164                return BigInteger.Zero;
 696165            }
 166        }
 167
 168        /// <summary>
 169        /// Gets the length of the key.
 170        /// </summary>
 171        /// <value>
 172        /// The length of the key.
 173        /// </value>
 174        public override int KeyLength
 175        {
 176            get
 1214177            {
 1214178                return Modulus.BitLength;
 1214179            }
 180        }
 181
 182        /// <summary>
 183        /// Gets the digital signature implementation for this key.
 184        /// </summary>
 185        /// <returns>
 186        /// An implementation of an RSA digital signature using the SHA-1 hash algorithm.
 187        /// </returns>
 188        protected internal override DigitalSignature DigitalSignature
 189        {
 190            get
 529191            {
 529192                _digitalSignature ??= new RsaDigitalSignature(this);
 193
 529194                return _digitalSignature;
 529195            }
 196        }
 197
 198        /// <summary>
 199        /// Gets or sets the public.
 200        /// </summary>
 201        /// <value>
 202        /// The public.
 203        /// </value>
 204        public override BigInteger[] Public
 205        {
 206            get
 2588207            {
 2588208                return new[] { Exponent, Modulus };
 2588209            }
 210            set
 1211211            {
 1211212                if (value.Length != 2)
 0213                {
 0214                    throw new InvalidOperationException("Invalid private key.");
 215                }
 216
 1211217                _privateKey = new[] { value[1], value[0] };
 1211218            }
 219        }
 220
 221        /// <summary>
 222        /// Initializes a new instance of the <see cref="RsaKey"/> class.
 223        /// </summary>
 1214224        public RsaKey()
 1214225        {
 1214226        }
 227
 228        /// <summary>
 229        /// Initializes a new instance of the <see cref="RsaKey"/> class.
 230        /// </summary>
 231        /// <param name="data">DER encoded private key data.</param>
 232        public RsaKey(byte[] data)
 499233            : base(data)
 499234        {
 499235            if (_privateKey.Length != 8)
 0236            {
 0237                throw new InvalidOperationException("Invalid private key.");
 238            }
 499239        }
 240
 241        /// <summary>
 242        /// Initializes a new instance of the <see cref="RsaKey"/> class.
 243        /// </summary>
 244        /// <param name="modulus">The modulus.</param>
 245        /// <param name="exponent">The exponent.</param>
 246        /// <param name="d">The d.</param>
 247        /// <param name="p">The p.</param>
 248        /// <param name="q">The q.</param>
 249        /// <param name="inverseQ">The inverse Q.</param>
 12250        public RsaKey(BigInteger modulus, BigInteger exponent, BigInteger d, BigInteger p, BigInteger q, BigInteger inve
 12251        {
 12252            _privateKey = new BigInteger[8]
 12253                {
 12254                    modulus,
 12255                    exponent,
 12256                    d,
 12257                    p,
 12258                    q,
 12259                    PrimeExponent(d, p),
 12260                    PrimeExponent(d, q),
 12261                    inverseQ
 12262                };
 12263        }
 264
 265        private static BigInteger PrimeExponent(BigInteger privateExponent, BigInteger prime)
 24266        {
 24267            var pe = prime - new BigInteger(1);
 24268            return privateExponent % pe;
 24269        }
 270
 271        /// <summary>
 272        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 273        /// </summary>
 274        public void Dispose()
 3275        {
 3276            Dispose(disposing: true);
 3277            GC.SuppressFinalize(this);
 3278        }
 279
 280        /// <summary>
 281        /// Releases unmanaged and - optionally - managed resources.
 282        /// </summary>
 283        /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor
 284        protected virtual void Dispose(bool disposing)
 1724285        {
 1724286            if (_isDisposed)
 0287            {
 0288                return;
 289            }
 290
 1724291            if (disposing)
 3292            {
 3293                var digitalSignature = _digitalSignature;
 3294                if (digitalSignature != null)
 3295                {
 3296                    digitalSignature.Dispose();
 3297                    _digitalSignature = null;
 3298                }
 299
 3300                _isDisposed = true;
 3301            }
 1724302        }
 303
 304        /// <summary>
 305        /// Releases unmanaged resources and performs other cleanup operations before the
 306        /// <see cref="RsaKey"/> is reclaimed by garbage collection.
 307        /// </summary>
 308        ~RsaKey()
 3442309        {
 1721310            Dispose(disposing: false);
 3442311        }
 312    }
 313}