public void Encrypt_When_GivenSettingsAndKey_Then_EncryptedJson() { var settings = new AppSettings { ConnectionString = "pgsqlconnectionstring", EmailApiKey = "api1.538c9073e4eb4461a87f1947bd47adb2bdd3a53bb26d4daf81d1e21b2039aab1" }; var key = AesEncryptionHelpers.GenerateKey(); var encrypted = AesEncryptionHelpers.Encrypt <AppSettings>(settings, key); Assert.NotNull(encrypted); }
public void AddEncryptedJsonFile_When_FileNotFound_Then_ThrowsCryptographicException() { var key = AesEncryptionHelpers.GenerateKey(); Assert.Throws <FileNotFoundException>(() => { var configuration = new ConfigurationBuilder() .AddEncryptedJsonFile("file_not_found.ejson", key) .Build(); }); }
public void Decrypt_When_GivenNoSettings_Then_ThrowsArgumentNullException() { var key = AesEncryptionHelpers.GenerateKey(); Assert.Throws <ArgumentNullException>(() => { var encrypted = AesEncryptionHelpers.Encrypt(Array.Empty <byte>(), key); }); Assert.Throws <ArgumentNullException>(() => { var encrypted = AesEncryptionHelpers.Encrypt(null, key); }); }
public void Decrypt_When_IncorrectKey_Then_ThrowsCryptographicException() { var settings = new AppSettings { ConnectionString = "pgsqlconnectionstring", EmailApiKey = "api1.538c9073e4eb4461a87f1947bd47adb2bdd3a53bb26d4daf81d1e21b2039aab1" }; var key = AesEncryptionHelpers.GenerateBase64EncodedKey(); var incorrectKey = AesEncryptionHelpers.GenerateBase64EncodedKey(); var encrypted = AesEncryptionHelpers.Encrypt <AppSettings>(settings, key); Assert.Throws <CryptographicException>(() => { var decrypted = AesEncryptionHelpers.Decrypt(encrypted, incorrectKey); }); }
public void Decrypt_When_GivenSettingsAndKey_Then_DecryptedJson() { var apiKey = "api1.538c9073e4eb4461a87f1947bd47adb2bdd3a53bb26d4daf81d1e21b2039aab1"; var settings = new AppSettings { ConnectionString = "pgsqlconnectionstring", EmailApiKey = apiKey }; var key = AesEncryptionHelpers.GenerateBase64EncodedKey(); var encrypted = AesEncryptionHelpers.Encrypt <AppSettings>(settings, key); var decrypted = AesEncryptionHelpers.Decrypt(encrypted, key); var result = JsonSerializer.Deserialize <AppSettings>(decrypted); Assert.Equal(apiKey, result.EmailApiKey); }
public void AddEncryptedJsonStream_When_GivenSettingsStreamAndKey_Then_BuildsConfiguration() { var apiKey = "api1.538c9073e4eb4461a87f1947bd47adb2bdd3a53bb26d4daf81d1e21b2039aab1"; var settings = new AppSettings { ConnectionString = "pgsqlconnectionstring", EmailApiKey = apiKey }; var key = AesEncryptionHelpers.GenerateKey(); var cipher = Convert.FromBase64String(AesEncryptionHelpers.Encrypt <AppSettings>(settings, key)); using var stream = new MemoryStream(cipher); var configuration = new ConfigurationBuilder() .AddEncryptedJsonStream(stream, key) .Build(); Assert.Equal(apiKey, configuration["EmailApiKey"]); }
public void AddEncryptedJsonStream_When_IncorrectKey_Then_ThrowsCryptographicException() { var apiKey = "api1.538c9073e4eb4461a87f1947bd47adb2bdd3a53bb26d4daf81d1e21b2039aab1"; var settings = new AppSettings { ConnectionString = "pgsqlconnectionstring", EmailApiKey = apiKey }; var encryptionKey = AesEncryptionHelpers.GenerateKey(); var incorrectKey = AesEncryptionHelpers.GenerateKey(); var cipher = Convert.FromBase64String(AesEncryptionHelpers.Encrypt <AppSettings>(settings, encryptionKey)); using var stream = new MemoryStream(cipher); Assert.Throws <CryptographicException>(() => { var configuration = new ConfigurationBuilder() .AddEncryptedJsonStream(stream, incorrectKey) .Build(); }); }