< Summary

Information
Class: Renci.SshNet.Security.Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\crypto\generators\ECKeyPairGenerator.cs
Line coverage
75%
Covered lines: 34
Uncovered lines: 11
Coverable lines: 45
Total lines: 91
Line coverage: 75.5%
Branch coverage
75%
Covered branches: 9
Total branches: 12
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor()100%1100%
.ctor(...)50%285.71%
Init(...)75%475%
GenerateKeyPair()83.33%686.66%
CreateBasePointMultiplier()100%1100%
GetCorrespondingPublicKey(...)100%10%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\crypto\generators\ECKeyPairGenerator.cs

#LineLine coverage
 1using System;
 2
 3using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Parameters;
 4using Renci.SshNet.Security.Org.BouncyCastle.Math;
 5using Renci.SshNet.Security.Org.BouncyCastle.Math.EC;
 6using Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Multiplier;
 7using Renci.SshNet.Security.Org.BouncyCastle.Security;
 8
 9namespace Renci.SshNet.Security.Org.BouncyCastle.Crypto.Generators
 10{
 11    internal class ECKeyPairGenerator
 12        : IAsymmetricCipherKeyPairGenerator
 13    {
 14        private readonly string algorithm;
 15
 16        private ECDomainParameters parameters;
 17        private SecureRandom random;
 18
 19        public ECKeyPairGenerator()
 920            : this("EC")
 921        {
 922        }
 23
 924        public ECKeyPairGenerator(
 925            string algorithm)
 926        {
 927            if (algorithm == null)
 028                throw new ArgumentNullException("algorithm");
 29
 930            this.algorithm = ECKeyParameters.VerifyAlgorithmName(algorithm);
 931        }
 32
 33        public void Init(
 34            KeyGenerationParameters parameters)
 935        {
 936            if (parameters is ECKeyGenerationParameters)
 937            {
 938                ECKeyGenerationParameters ecP = (ECKeyGenerationParameters) parameters;
 39
 940                this.parameters = ecP.DomainParameters;
 941            }
 42
 943            this.random = parameters.Random;
 44
 945            if (this.random == null)
 046            {
 047                this.random = new SecureRandom();
 048            }
 949        }
 50
 51        public AsymmetricCipherKeyPair GenerateKeyPair()
 952        {
 953            BigInteger n = parameters.N;
 54            BigInteger d;
 955            int minWeight = n.BitLength >> 2;
 56
 57            for (;;)
 958            {
 959                d = new BigInteger(n.BitLength, random);
 60
 961                if (d.CompareTo(BigInteger.Two) < 0 || d.CompareTo(n) >= 0)
 062                    continue;
 63
 964                if (WNafUtilities.GetNafWeight(d) < minWeight)
 065                    continue;
 66
 967                break;
 68            }
 69
 970            ECPoint q = CreateBasePointMultiplier().Multiply(parameters.G, d);
 71
 972            return new AsymmetricCipherKeyPair(
 973                new ECPublicKeyParameters(algorithm, q, parameters),
 974                new ECPrivateKeyParameters(algorithm, d, parameters));
 975        }
 76
 77        protected virtual ECMultiplier CreateBasePointMultiplier()
 978        {
 979            return new FixedPointCombMultiplier();
 980        }
 81
 82        internal static ECPublicKeyParameters GetCorrespondingPublicKey(
 83            ECPrivateKeyParameters privKey)
 084        {
 085            ECDomainParameters ec = privKey.Parameters;
 086            ECPoint q = new FixedPointCombMultiplier().Multiply(ec.G, privKey.D);
 87
 088            return new ECPublicKeyParameters(privKey.AlgorithmName, q, ec);
 089        }
 90    }
 91}