public void Given_ValidKey_When_WriteToFile_Then_PublicAndPrivateKeyEncodedAndWrittenToFile() { // Arrange var privateKey = new RsaKeyGenerator().GetNewKey(new RsaKeyGenerationOptions() { KeySize = 384, UseOaepPadding = true, NewKeyName = "test" }); var pocoMapper = new Mock <IKeyToExternalRepresentationMapper <RsaKey, EnvCryptKey> >(); pocoMapper.Setup(m => m.Map(It.IsAny <RsaKey>(), It.IsAny <EnvCryptKey>())).Callback <RsaKey, EnvCryptKey>((key, keyXml) => keyXml.Type = key.Key.D == null ? KeyTypeEnum.Public.ToString() : keyXml.Type = KeyTypeEnum.Private.ToString()); const string privateKeyXmlContents = @"<EnvCryptKey Type=""Private"" ... > ..."; const string publicKeyXmlContents = @"<EnvCryptKey Type=""Public"" ... > ..."; var usedEncoding = Encoding.UTF32; var serialisationUtil = new Mock <IXmlSerializationUtils <EnvCryptKey> >(); serialisationUtil.Setup(u => u.Serialize(It.Is <EnvCryptKey>(p => p.Type == KeyTypeEnum.Private.ToString()))).Returns(privateKeyXmlContents); serialisationUtil.Setup(u => u.Serialize(It.Is <EnvCryptKey>(p => p.Type == KeyTypeEnum.Public.ToString()))).Returns(publicKeyXmlContents); serialisationUtil.Setup(u => u.GetUsedEncoding()).Returns(usedEncoding); var writer = new Mock <IStringWriter <StringToFileWriterOptions> >(); const string privateKeyFile = @"C:\some\path\privatekey.xml"; const string publicKeyFile = @"C:\some\path\publickey.xml"; var opts = new AsymmetricKeyFilePersisterOptions() { NewPrivateKeyFullFilePath = privateKeyFile, NewPublicKeyFullFilePath = publicKeyFile, OverwriteFileIfExists = true }; // Act var persister = new RsaKeyPersister(pocoMapper.Object, serialisationUtil.Object, writer.Object); persister.Persist(privateKey, opts); // Assert serialisationUtil.Verify(u => u.Serialize(It.Is <EnvCryptKey>(p => p.Type == KeyTypeEnum.Private.ToString())), Times.Once); writer.Verify(w => w.Write(new StringToFileWriterOptions() { Path = privateKeyFile, Contents = privateKeyXmlContents, OverwriteIfFileExists = true, Encoding = usedEncoding }), Times.Once); serialisationUtil.Verify(u => u.Serialize(It.Is <EnvCryptKey>(p => p.Type == KeyTypeEnum.Public.ToString())), Times.Once); writer.Verify(w => w.Write(new StringToFileWriterOptions() { Path = publicKeyFile, Contents = publicKeyXmlContents, OverwriteIfFileExists = true, Encoding = usedEncoding }), Times.Once); }
public void Run(GenerateKeyVerbOptions options) { var encryptionType = options.GetAlgorithm(); if (encryptionType == EnvCryptAlgoEnum.Rsa) { var rsaKeyPersisterOpts = new AsymmetricKeyFilePersisterOptions() { NewKeyName = options.KeyName, NewPrivateKeyFullFilePath = Path.Combine( options.OutputDirectory, string.Concat(options.KeyName, PrivateKeyPostfix)), NewPublicKeyFullFilePath = Path.Combine( options.OutputDirectory, string.Concat(options.KeyName, PublicKeyPostfix)), OverwriteFileIfExists = false }; if (options.OutputKeyToConsole) { new GenerateRsaKeyBuilder() .WithKeyPersister(AsymmetricKeyFilePersisterFactory.GetRsaKeyPersister(new ToConsoleTextWriter())) .Build().Run(rsaKeyPersisterOpts); } else { new GenerateRsaKeyBuilder().Build().Run(rsaKeyPersisterOpts); } } else if (encryptionType == EnvCryptAlgoEnum.Aes) { var aesKeyPersisterOpts = new SymmetricKeyFilePersisterOptions() { NewKeyName = options.KeyName, NewKeyFileFullPath = Path.Combine( options.OutputDirectory, string.Concat(options.KeyName, CommonPostFix)), OverwriteFileIfExists = false }; if (options.OutputKeyToConsole) { new GenerateAesKeyBuilder() .WithKeyPersister(SymmetricKeyFilePersisterFactory.GetAesKeyPersister(new ToConsoleTextWriter())) .Build().Run(aesKeyPersisterOpts); } else { new GenerateAesKeyBuilder().Build().Run(aesKeyPersisterOpts); } } else { System.Console.Error.WriteLine("Unsupported encryption type: {0}", encryptionType); } }
public void Run(AsymmetricKeyFilePersisterOptions options) { Contract.Requires <ArgumentNullException>(options != null, "fileOptions"); // ThrowIfNotBuilt(); var keyGenerationOptions = new RsaKeyGenerationOptions() { KeySize = DefaultRsaKeySize, UseOaepPadding = DefaultUseOaepPadding, NewKeyName = options.NewKeyName }; _workflow.Run(keyGenerationOptions, options); }