From 4b65086622e4cd2183f00803af4d40765966a727 Mon Sep 17 00:00:00 2001 From: David Sparer Date: Mon, 11 Jul 2016 15:42:48 -0600 Subject: [PATCH] Some cleanup --- .../Security/AeadCryptographyProviderTests.cs | 18 +++++++++--------- mRemoteV1/Security/AeadCryptographyProvider.cs | 13 ++++++------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/mRemoteNGTests/Security/AeadCryptographyProviderTests.cs b/mRemoteNGTests/Security/AeadCryptographyProviderTests.cs index 95dea987..53fdc28e 100644 --- a/mRemoteNGTests/Security/AeadCryptographyProviderTests.cs +++ b/mRemoteNGTests/Security/AeadCryptographyProviderTests.cs @@ -7,14 +7,14 @@ namespace mRemoteNGTests.Security { public class AeadCryptographyProviderTests { - private ICryptographyProvider _aesgcm; + private ICryptographyProvider _cryptographyProvider; private SecureString _encryptionKey; private string _plainText; [SetUp] public void Setup() { - _aesgcm = new AeadCryptographyProvider(); + _cryptographyProvider = new AeadCryptographyProvider(); _encryptionKey = "mypassword111111".ConvertToSecureString(); _plainText = "MySecret!"; } @@ -22,35 +22,35 @@ namespace mRemoteNGTests.Security [TearDown] public void TearDown() { - _aesgcm = null; + _cryptographyProvider = null; } [Test] public void GetBlockSizeReturnsProperValueForAes() { - Assert.That(_aesgcm.BlockSizeInBytes, Is.EqualTo(16)); + Assert.That(_cryptographyProvider.BlockSizeInBytes, Is.EqualTo(16)); } [Test] public void EncryptionOutputsBase64String() { - var cipherText = _aesgcm.Encrypt(_plainText, _encryptionKey); + var cipherText = _cryptographyProvider.Encrypt(_plainText, _encryptionKey); Assert.That(cipherText.IsBase64String, Is.True); } [Test] public void DecryptedTextIsEqualToOriginalPlainText() { - var cipherText = _aesgcm.Encrypt(_plainText, _encryptionKey); - var decryptedCipherText = _aesgcm.Decrypt(cipherText, _encryptionKey); + var cipherText = _cryptographyProvider.Encrypt(_plainText, _encryptionKey); + var decryptedCipherText = _cryptographyProvider.Decrypt(cipherText, _encryptionKey); Assert.That(decryptedCipherText, Is.EqualTo(_plainText)); } [Test] public void EncryptingTheSameValueReturnsNewCipherTextEachTime() { - var cipherText1 = _aesgcm.Encrypt(_plainText, _encryptionKey); - var cipherText2 = _aesgcm.Encrypt(_plainText, _encryptionKey); + var cipherText1 = _cryptographyProvider.Encrypt(_plainText, _encryptionKey); + var cipherText2 = _cryptographyProvider.Encrypt(_plainText, _encryptionKey); Assert.That(cipherText1, Is.Not.EqualTo(cipherText2)); } } diff --git a/mRemoteV1/Security/AeadCryptographyProvider.cs b/mRemoteV1/Security/AeadCryptographyProvider.cs index 4985ac62..544320b8 100644 --- a/mRemoteV1/Security/AeadCryptographyProvider.cs +++ b/mRemoteV1/Security/AeadCryptographyProvider.cs @@ -21,7 +21,6 @@ namespace mRemoteNG.Security { public class AeadCryptographyProvider : ICryptographyProvider { - private readonly IBlockCipher _blockCipher; private readonly IAeadBlockCipher _aeadBlockCipher; private readonly Encoding _encoding; private readonly SecureRandom _random = new SecureRandom(); @@ -72,7 +71,7 @@ namespace mRemoteNG.Security private string SimpleEncryptWithPassword(string secretMessage, string password, byte[] nonSecretPayload = null) { if (string.IsNullOrEmpty(secretMessage)) - throw new ArgumentException("Secret Message Required!", "secretMessage"); + throw new ArgumentException("Secret Message Required!", nameof(secretMessage)); var plainText = _encoding.GetBytes(secretMessage); var cipherText = SimpleEncryptWithPassword(plainText, password, nonSecretPayload); @@ -85,10 +84,10 @@ namespace mRemoteNG.Security //User Error Checks if (string.IsNullOrWhiteSpace(password) || password.Length < MinPasswordLength) - throw new ArgumentException(String.Format("Must have a password of at least {0} characters!", MinPasswordLength), "password"); + throw new ArgumentException($"Must have a password of at least {MinPasswordLength} characters!", nameof(password)); if (secretMessage == null || secretMessage.Length == 0) - throw new ArgumentException("Secret Message Required!", "secretMessage"); + throw new ArgumentException("Secret Message Required!", nameof(secretMessage)); var generator = new Pkcs5S2ParametersGenerator(); @@ -116,10 +115,10 @@ namespace mRemoteNG.Security { //User Error Checks if (key == null || key.Length != KeyBitSize / 8) - throw new ArgumentException(String.Format("Key needs to be {0} bit!", KeyBitSize), "key"); + throw new ArgumentException($"Key needs to be {KeyBitSize} bit!", nameof(key)); if (secretMessage == null || secretMessage.Length == 0) - throw new ArgumentException("Secret Message Required!", "secretMessage"); + throw new ArgumentException("Secret Message Required!", nameof(secretMessage)); //Non-secret Payload Optional nonSecretPayload = nonSecretPayload ?? new byte[] { }; @@ -162,7 +161,7 @@ namespace mRemoteNG.Security private string SimpleDecryptWithPassword(string encryptedMessage, SecureString decryptionKey, int nonSecretPayloadLength = 0) { if (string.IsNullOrWhiteSpace(encryptedMessage)) - throw new ArgumentException("Encrypted Message Required!", "encryptedMessage"); + throw new ArgumentException("Encrypted Message Required!", nameof(encryptedMessage)); var cipherText = Convert.FromBase64String(encryptedMessage); var plainText = SimpleDecryptWithPassword(cipherText, decryptionKey.ConvertToUnsecureString(), nonSecretPayloadLength);