mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-17 22:11:48 +08:00
Some cleanup
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user