public static void OutputKey(RSAWrapper rsa) { ProgramParameters parms = ProgramParameters.Instance; Console.WriteLine(); Console.WriteLine("Ding!! Delicious scallions for you!!"); Console.WriteLine(); if (parms.KeyOutputPath != null) { System.IO.File.AppendAllText(parms.KeyOutputPath, "Generated at: " + System.DateTime.Now.ToString("G") + "\n"); System.IO.File.AppendAllText(parms.KeyOutputPath, "Address/Hash: " + rsa.OnionHash + ".onion\n"); System.IO.File.AppendAllText(parms.KeyOutputPath, "Public Modulus: " + rsa.Rsa.PublicModulus.ToDecimalString() + "\n"); System.IO.File.AppendAllText(parms.KeyOutputPath, "Public Exponent: " + rsa.Rsa.PublicExponent.ToDecimalString() + "\n"); if (rsa.HasPrivateKey) { System.IO.File.AppendAllText(parms.KeyOutputPath, "RSA key: \n" + rsa.Rsa.PrivateKeyAsPEM + "\n"); } System.IO.File.AppendAllText(parms.KeyOutputPath, "\n\n"); } Console.WriteLine("Public Modulus: {0}", rsa.Rsa.PublicModulus.ToDecimalString()); Console.WriteLine("Public Exponent: {0}", rsa.Rsa.PublicExponent.ToDecimalString()); Console.WriteLine("Address/Hash: " + rsa.OnionHash + ".onion"); Console.WriteLine(); if (rsa.HasPrivateKey) { Console.WriteLine(rsa.Rsa.PrivateKeyAsPEM); Console.WriteLine(); } }
public static void WriteModuli() { ProgramParameters parms = ProgramParameters.Instance; var rp = new RegexPattern(parms.Regex); ulong hashes_per_win = (ulong)(0.5 / rp.GenerateAllOnionPatternsForRegex().Select(t => Math.Pow(2, -5 * t.Count(q => q != '.'))).Sum()); ulong hashes_per_key = (CLRuntime.EXP_MAX - CLRuntime.EXP_MIN) / 2; ulong keys_needed = hashes_per_win / hashes_per_key; uint SF = 5; Console.WriteLine("Generating that pattern will require approximately {0:0.000} gigahashes.", hashes_per_win / 1e9); Console.WriteLine("That will require on average {0} public keys.", keys_needed); Console.WriteLine("Generating {0} keys (for safety's sake).", keys_needed * SF); RSAWrapper rsa = new RSAWrapper(); StreamWriter priv_sw = new StreamWriter(parms.RSAModuliPath + ".priv"); StreamWriter pub_sw = new StreamWriter(parms.RSAModuliPath); for (ulong i = 0; i < keys_needed * SF; i++) { if (i % 100 == 0) { Console.WriteLine("Generating key {0} of {1}...", i, keys_needed * SF); } rsa.GenerateKey((int)parms.KeySize); pub_sw.WriteLine(rsa.Rsa.PublicModulus.ToDecimalString()); priv_sw.WriteLine("Public Modulus: " + rsa.Rsa.PublicModulus.ToDecimalString()); priv_sw.WriteLine(rsa.Rsa.PrivateKeyAsPEM); priv_sw.WriteLine(""); } pub_sw.Close(); priv_sw.Close(); }
public KernelInput(int num_exps) { Rsa = new RSAWrapper(); LastWs = new uint[num_exps * 16]; Midstates = new uint[num_exps * 5]; ExpIndexes = new int[num_exps]; Results = new uint[128]; BaseExp = EXP_MIN; }
public static void ReadResults() { ProgramParameters parms = ProgramParameters.Instance; string rsaPrivFn = parms.RSAModuliPath + ".priv"; if (!File.Exists(rsaPrivFn)) { Console.WriteLine("Error: expecting private key file at {0}.", rsaPrivFn); return; } if (!File.Exists(parms.InputResultsPath)) { Console.WriteLine("Error: expecting results from miner at {0}.", parms.InputResultsPath); return; } // Create a map to hold (public modulus as decimal string) -> (private key as pem) IDictionary<string, string> modulusKeyMap = new Dictionary<string, string>(); // Read the priv key list file string currentModulus = null, currentPEM = null; foreach (string l in File.ReadAllLines(rsaPrivFn)) { string line = l.Trim(); if (line.StartsWith("Public Modulus: ")) { currentModulus = line.Replace("Public Modulus: ",""); } if (line.StartsWith("-----BEGIN RSA PRIVATE KEY-----")) { currentPEM = ""; } currentPEM += line + "\n"; if (line.StartsWith("-----END RSA PRIVATE KEY-----")) { modulusKeyMap.Add(currentModulus, currentPEM); } } // Read the results file string modulus = null, exponent = null, address = null; foreach (string l in File.ReadAllLines(parms.InputResultsPath)) { //string[] split = l.Trim().Split(":".ToCharArray(), 2); string line = l.Trim(); if (line.StartsWith("Public Modulus: ")) { modulus = line.Replace("Public Modulus: ",""); } if (line.StartsWith("Public Exponent: ")) { exponent = line.Replace("Public Exponent: ",""); } if (line.StartsWith("Address/Hash: ")) { address = line.Replace("Address/Hash: ",""); } if (modulus != null && exponent != null && address != null) { // Find the modulus in the private key map string pem; if (!modulusKeyMap.TryGetValue(modulus, out pem)) { throw new InvalidDataException(String.Format("Modulus {0} is missing from the private key data file.", modulus)); } // Load the PEM into the RSA RSAWrapper rsa = new RSAWrapper(); rsa.FromPrivateKeyPEM(pem); // Verify that modulus matches if (rsa.Rsa.PublicModulus != BigNumber.FromDecimalString(modulus)) { throw new InvalidDataException("Modulus of PEM does not match declared value."); } // Change the public exponent rsa.ChangePublicExponent(BigNumber.FromDecimalString(exponent)); // Check the key's sanity rsa.CheckSanity(); // Verify the hash if (rsa.OnionHash + ".onion" != address) { throw new InvalidDataException("Onion hash of key does not match declared value."); } // Yay the key is good! Output it as required CLRuntime.OutputKey(rsa); modulus = null; exponent = null; address = null; } } }
public static void WriteModuli() { ProgramParameters parms = ProgramParameters.Instance; var rp = new RegexPattern(parms.Regex); ulong hashes_per_win = (ulong)(0.5 / rp.GenerateAllOnionPatternsForRegex().Select(t=>Math.Pow(2,-5*t.Count(q=>q!='.'))).Sum()); ulong hashes_per_key = (CLRuntime.EXP_MAX - CLRuntime.EXP_MIN) / 2; ulong keys_needed = hashes_per_win / hashes_per_key; uint SF = 5; Console.WriteLine("Generating that pattern will require approximately {0:0.000} gigahashes.", hashes_per_win / 1e9); Console.WriteLine("That will require on average {0} public keys.", keys_needed); Console.WriteLine("Generating {0} keys (for safety's sake).", keys_needed * SF); RSAWrapper rsa = new RSAWrapper(); StreamWriter priv_sw = new StreamWriter(parms.RSAModuliPath + ".priv"); StreamWriter pub_sw = new StreamWriter(parms.RSAModuliPath); for (ulong i = 0; i < keys_needed * SF; i++) { if (i % 100 == 0) { Console.WriteLine("Generating key {0} of {1}...", i, keys_needed*SF); } rsa.GenerateKey((int)parms.KeySize); pub_sw.WriteLine(rsa.Rsa.PublicModulus.ToDecimalString()); priv_sw.WriteLine("Public Modulus: " + rsa.Rsa.PublicModulus.ToDecimalString()); priv_sw.WriteLine(rsa.Rsa.PrivateKeyAsPEM); priv_sw.WriteLine(""); } pub_sw.Close(); priv_sw.Close(); }
public static void ReadResults() { ProgramParameters parms = ProgramParameters.Instance; string rsaPrivFn = parms.RSAModuliPath + ".priv"; if (!File.Exists(rsaPrivFn)) { Console.WriteLine("Error: expecting private key file at {0}.", rsaPrivFn); return; } if (!File.Exists(parms.InputResultsPath)) { Console.WriteLine("Error: expecting results from miner at {0}.", parms.InputResultsPath); return; } // Create a map to hold (public modulus as decimal string) -> (private key as pem) IDictionary <string, string> modulusKeyMap = new Dictionary <string, string>(); // Read the priv key list file string currentModulus = null, currentPEM = null; foreach (string l in File.ReadAllLines(rsaPrivFn)) { string line = l.Trim(); if (line.StartsWith("Public Modulus: ")) { currentModulus = line.Replace("Public Modulus: ", ""); } if (line.StartsWith("-----BEGIN RSA PRIVATE KEY-----")) { currentPEM = ""; } currentPEM += line + "\n"; if (line.StartsWith("-----END RSA PRIVATE KEY-----")) { modulusKeyMap.Add(currentModulus, currentPEM); } } // Read the results file string modulus = null, exponent = null, address = null; foreach (string l in File.ReadAllLines(parms.InputResultsPath)) { //string[] split = l.Trim().Split(":".ToCharArray(), 2); string line = l.Trim(); if (line.StartsWith("Public Modulus: ")) { modulus = line.Replace("Public Modulus: ", ""); } if (line.StartsWith("Public Exponent: ")) { exponent = line.Replace("Public Exponent: ", ""); } if (line.StartsWith("Address/Hash: ")) { address = line.Replace("Address/Hash: ", ""); } if (modulus != null && exponent != null && address != null) { // Find the modulus in the private key map string pem; if (!modulusKeyMap.TryGetValue(modulus, out pem)) { throw new InvalidDataException(String.Format("Modulus {0} is missing from the private key data file.", modulus)); } // Load the PEM into the RSA RSAWrapper rsa = new RSAWrapper(); rsa.FromPrivateKeyPEM(pem); // Verify that modulus matches if (rsa.Rsa.PublicModulus != BigNumber.FromDecimalString(modulus)) { throw new InvalidDataException("Modulus of PEM does not match declared value."); } // Change the public exponent rsa.ChangePublicExponent(BigNumber.FromDecimalString(exponent)); // Check the key's sanity rsa.CheckSanity(); // Verify the hash if (rsa.OnionHash + ".onion" != address) { throw new InvalidDataException("Onion hash of key does not match declared value."); } // Yay the key is good! Output it as required CLRuntime.OutputKey(rsa); modulus = null; exponent = null; address = null; } } }