/// <inheritdoc /> public override bool Execute() { if (string.IsNullOrWhiteSpace(OutputDirectory)) { Log?.LogError(KeyGenerationTaskStrings.OutputDirectoryInvalid); return(false); } if (!Directory.Exists(OutputDirectory)) { try { Directory.CreateDirectory(OutputDirectory); } catch (Exception exception) { Log?.LogError(KeyGenerationTaskStrings.ErrorCreatingOutputDirectory, OutputDirectory); Log?.LogErrorFromException(exception); } } var power = Math.Log(KeySize, 0); if (KeySize % 2 != 0 || power < 10 || power - Math.Floor(power) > 0.001) { Log?.LogError(KeyGenerationTaskStrings.KeySizeInvalid, KeySize); return(false); } var pathPrivateKey = Path.Combine(OutputDirectory, "network.handshake.bkey"); var pathPublicKey = Path.Combine(OutputDirectory, "network.handshake.bkey.pub"); using (var rsa = new RSACryptoServiceProvider(KeySize)) { var writePrivateKey = true; if (!GenerateEachBuild) { if (File.Exists(pathPrivateKey)) { if (File.Exists(pathPublicKey)) { return(true); } Log?.LogWarning(KeyGenerationTaskStrings.PublicKeyMissing); } try { using (var stream = File.OpenRead(pathPrivateKey)) { var privateKey = new RsaKey(); privateKey.Read(stream); if (privateKey.IsPublic || privateKey.Format != KeyFormat.Rsa) { Log?.LogWarning(KeyGenerationTaskStrings.PrivateKeyInvalid); } else { rsa.ImportParameters(privateKey.Parameters); writePrivateKey = false; } } } catch (Exception exception) { Log?.LogWarning(KeyGenerationTaskStrings.ErrorReadingPrivateKey); Log?.LogWarningFromException(exception); } } if (writePrivateKey) { var privateKey = new RsaKey(rsa.ExportParameters(true)); try { using (var stream = File.OpenWrite(pathPrivateKey)) { privateKey.Write(stream); } } catch (Exception exception) { Log?.LogError(KeyGenerationTaskStrings.ErrorWritingPrivateKey); Log?.LogErrorFromException(exception); return(false); } } var publicKey = new RsaKey(rsa.ExportParameters(false)); try { using (var stream = File.OpenWrite(pathPublicKey)) { publicKey.Write(stream); } } catch (Exception exception) { Log?.LogError(KeyGenerationTaskStrings.ErrorWritingPublicKey); Log?.LogErrorFromException(exception); return(false); } return(true); } }
public override Result Run(string[] environmentArgs, string[] commandArgs) { var bits = 2048; var filename = "intersect.bek"; if (commandArgs?.Length > 0) { if (!int.TryParse(commandArgs[0], out bits)) { return(new Result { Code = 1, Message = "Failed to parse the bit size." }); } if (bits % 2 != 0) { return(new Result { Code = 101, Message = "Bit size must be a multiple of 2." }); } if (bits < 1) { return(new Result { Code = 102, Message = "Bit size must be non-zero." }); } } if (commandArgs?.Length > 1) { if (string.IsNullOrWhiteSpace(commandArgs[1])) { return(new Result { Code = 2, Message = "Empty filename." }); } } using (var rsa = new RSACryptoServiceProvider(bits)) { var rsaPrivateKey = new RsaKey(rsa.ExportParameters(true)); using (var privateStream = new FileStream($"private-{filename}", FileMode.Create)) { rsaPrivateKey.Write(privateStream); } var rsaPublicKey = new RsaKey(rsa.ExportParameters(false)); using (var publicStream = new FileStream($"public-{filename}", FileMode.Create)) { rsaPublicKey.Write(publicStream); } } return(new Result()); }