Esempio n. 1
0
        public static void BenchmarkKeyGeneration(int num_iterations, int key_size, string output_file = "")
        {
            StreamWriter output = null;

            if (output_file != "")
            {
                output = File.CreateText(output_file);
            }
            // Testing some key generation features
            Logging.info("Preparing entropy to benchmark key generation speed...");
            byte[] entropy = getNewRandomSeed(1024 * 1024);
            IXICore.CryptoKey.KeyDerivation kd = new IXICore.CryptoKey.KeyDerivation(entropy);
            CryptoManager.initLib();
            Logging.info(String.Format("Starting key generation. Iterations: {0}", num_iterations));
            List <TimeSpan> generationTimes = new List <TimeSpan>();

            for (int i = 0; i < num_iterations; i++)
            {
                DateTime start = DateTime.Now;
                Logging.info(String.Format("Generating key {0}...", i));
                IxianKeyPair kp             = kd.deriveKey(i, key_size, 65537);
                TimeSpan     generationTime = DateTime.Now - start;
                bool         success        = CryptoManager.lib.testKeys(Encoding.Unicode.GetBytes("TEST TEST"), kp);
                double       key_entropy    = calculateBytestreamEntropy(kp.privateKeyBytes);
                if (success && output != null)
                {
                    RSACryptoServiceProvider rsaCSP = rsaKeyFromBytes(kp.privateKeyBytes);
                    RSAParameters            rsaP   = rsaCSP.ExportParameters(true);
                    BigInteger n = new BigInteger(rsaP.Modulus);
                    output.WriteLine(String.Format("{0}|{1}", n.ToString(), key_entropy));
                }
                Logging.info(String.Format("Key generated. ({0:0.00} ms)",
                                           generationTime.TotalMilliseconds));
                generationTimes.Add(generationTime);
                Logging.info(String.Format("Key test: {0}", success ? "success" : "failure"));
                Logging.info(String.Format("Key entropy: {0}", key_entropy));
            }
            if (output != null)
            {
                output.Flush();
                output.Close();
            }
            Logging.info(String.Format("Average time to generate a key: {0:0.00} ms", generationTimes.Average(x => x.TotalMilliseconds)));
            Logging.info(String.Format("Maximum time to generate a key: {0:0.00} ms", generationTimes.Max().TotalMilliseconds));
            return;
        }
Esempio n. 2
0
        public IxianKeyPair generateNewKeyPair(bool writeToFile = true)
        {
            if (walletVersion < 3)
            {
                lock (myKeys)
                {
                    return(myKeys.First().Value);
                }
            }

            IXICore.CryptoKey.KeyDerivation kd = new IXICore.CryptoKey.KeyDerivation(masterSeed);

            int key_count = 0;

            lock (myKeys)
            {
                key_count = myKeys.Count();
            }

            IxianKeyPair kp = kd.deriveKey(key_count, ConsensusConfig.defaultRsaKeySize, 65537);

            if (kp == null)
            {
                Logging.error("An error occured generating new key pair, unable to derive key.");
                return(null);
            }

            if (!IXICore.CryptoManager.lib.testKeys(Encoding.Unicode.GetBytes("TEST TEST"), kp))
            {
                Logging.error("An error occured while testing the newly generated keypair, unable to produce a valid address.");
                return(null);
            }
            Address addr = new Address(kp.publicKeyBytes);

            if (addr.address == null)
            {
                Logging.error("An error occured generating new key pair, unable to produce a valid address.");
                return(null);
            }
            lock (myKeys)
            {
                lock (myAddresses)
                {
                    if (!writeToFile)
                    {
                        myKeys.Add(addr.address, kp);
                        AddressData ad = new AddressData()
                        {
                            nonce = kp.lastNonceBytes, keyPair = kp
                        };
                        myAddresses.Add(addr.address, ad);
                    }
                    else
                    {
                        if (writeWallet(walletPassword))
                        {
                            myKeys.Add(addr.address, kp);
                            AddressData ad = new AddressData()
                            {
                                nonce = kp.lastNonceBytes, keyPair = kp
                            };
                            myAddresses.Add(addr.address, ad);
                        }
                        else
                        {
                            Logging.error("An error occured while writing wallet file.");
                            return(null);
                        }
                    }
                }
            }

            return(kp);
        }