Adding support for .NET 8.0 LTS, renaming private fields to start with underscore
This commit is contained in:
@@ -8,9 +8,12 @@ namespace System.Security.Cryptography
|
||||
/// </summary>
|
||||
public class CryptographyHelper
|
||||
{
|
||||
private static int saltLength = 8;
|
||||
// "readonly" not added due to UnitTests
|
||||
#pragma warning disable IDE0044 // Add "readonly" modifier
|
||||
private static int _saltLength = 8;
|
||||
#pragma warning restore IDE0044 // Add "readonly" modifier
|
||||
|
||||
private readonly string masterKeyFile;
|
||||
private readonly string _masterKeyFile;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CryptographyHelper"/> class.
|
||||
@@ -18,17 +21,17 @@ namespace System.Security.Cryptography
|
||||
/// <param name="keyFile">The (absolute) path to the crypto key file. On <c>null</c> the file 'crypto.key' at the executing assembly location will be used.</param>
|
||||
public CryptographyHelper(string keyFile = null)
|
||||
{
|
||||
masterKeyFile = keyFile;
|
||||
_masterKeyFile = keyFile;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(masterKeyFile))
|
||||
masterKeyFile = "crypto.key";
|
||||
if (string.IsNullOrWhiteSpace(_masterKeyFile))
|
||||
_masterKeyFile = "crypto.key";
|
||||
|
||||
if (!Path.IsPathRooted(masterKeyFile))
|
||||
masterKeyFile = Path.Combine(AppContext.BaseDirectory, masterKeyFile);
|
||||
if (!Path.IsPathRooted(_masterKeyFile))
|
||||
_masterKeyFile = Path.Combine(AppContext.BaseDirectory, _masterKeyFile);
|
||||
|
||||
string pw = File.Exists(masterKeyFile) ? File.ReadAllText(masterKeyFile) : null;
|
||||
string pw = File.Exists(_masterKeyFile) ? File.ReadAllText(_masterKeyFile) : null;
|
||||
if (string.IsNullOrWhiteSpace(pw))
|
||||
File.WriteAllText(masterKeyFile, GetRandomString(64));
|
||||
File.WriteAllText(_masterKeyFile, GetRandomString(64));
|
||||
}
|
||||
|
||||
#region Instance methods
|
||||
@@ -46,8 +49,7 @@ namespace System.Security.Cryptography
|
||||
/// <returns>The decrypted data.</returns>
|
||||
public byte[] DecryptAes(byte[] cipher, string password = null)
|
||||
{
|
||||
if (password == null)
|
||||
password = File.ReadAllText(masterKeyFile);
|
||||
password ??= File.ReadAllText(_masterKeyFile);
|
||||
|
||||
return AesDecrypt(cipher, password);
|
||||
}
|
||||
@@ -63,8 +65,7 @@ namespace System.Security.Cryptography
|
||||
/// <returns>The encrypted data (cipher).</returns>
|
||||
public byte[] EncryptAes(byte[] plain, string password = null)
|
||||
{
|
||||
if (password == null)
|
||||
password = File.ReadAllText(masterKeyFile);
|
||||
password ??= File.ReadAllText(_masterKeyFile);
|
||||
|
||||
return AesEncrypt(plain, password);
|
||||
}
|
||||
@@ -110,8 +111,7 @@ namespace System.Security.Cryptography
|
||||
/// <returns>The decrypted data.</returns>
|
||||
public byte[] DecryptTripleDes(byte[] cipher, string password = null)
|
||||
{
|
||||
if (password == null)
|
||||
password = File.ReadAllText(masterKeyFile);
|
||||
password ??= File.ReadAllText(_masterKeyFile);
|
||||
|
||||
return TripleDesDecrypt(cipher, password);
|
||||
}
|
||||
@@ -127,8 +127,7 @@ namespace System.Security.Cryptography
|
||||
/// <returns>The encrypted data (cipher).</returns>
|
||||
public byte[] EncryptTripleDes(byte[] plain, string password = null)
|
||||
{
|
||||
if (password == null)
|
||||
password = File.ReadAllText(masterKeyFile);
|
||||
password ??= File.ReadAllText(_masterKeyFile);
|
||||
|
||||
return TripleDesEncrypt(plain, password);
|
||||
}
|
||||
@@ -183,8 +182,8 @@ namespace System.Security.Cryptography
|
||||
/// <returns>The decrypted data.</returns>
|
||||
public static byte[] AesDecrypt(byte[] cipher, string password)
|
||||
{
|
||||
byte[] salt = new byte[saltLength];
|
||||
Array.Copy(cipher, salt, saltLength);
|
||||
byte[] salt = new byte[_saltLength];
|
||||
Array.Copy(cipher, salt, _saltLength);
|
||||
|
||||
using var gen = new Rfc2898DeriveBytes(password, salt);
|
||||
using var aes = Aes.Create();
|
||||
@@ -197,7 +196,7 @@ namespace System.Security.Cryptography
|
||||
using var ms = new MemoryStream();
|
||||
using var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write);
|
||||
|
||||
cs.Write(cipher, saltLength, cipher.Length - saltLength);
|
||||
cs.Write(cipher, _saltLength, cipher.Length - _saltLength);
|
||||
cs.FlushFinalBlock();
|
||||
|
||||
return ms.ToArray();
|
||||
@@ -224,7 +223,7 @@ namespace System.Security.Cryptography
|
||||
/// <returns>The encrypted data (cipher).</returns>
|
||||
public static byte[] AesEncrypt(byte[] plain, string password)
|
||||
{
|
||||
byte[] salt = GetRandomBytes(saltLength);
|
||||
byte[] salt = GetRandomBytes(_saltLength);
|
||||
|
||||
using var gen = new Rfc2898DeriveBytes(password, salt);
|
||||
using var aes = Aes.Create();
|
||||
@@ -269,8 +268,8 @@ namespace System.Security.Cryptography
|
||||
/// <returns>The decrypted data.</returns>
|
||||
public static byte[] TripleDesDecrypt(byte[] cipher, string password)
|
||||
{
|
||||
byte[] salt = new byte[saltLength];
|
||||
Array.Copy(cipher, salt, saltLength);
|
||||
byte[] salt = new byte[_saltLength];
|
||||
Array.Copy(cipher, salt, _saltLength);
|
||||
|
||||
using var gen = new Rfc2898DeriveBytes(password, salt);
|
||||
using var tdes = TripleDES.Create();
|
||||
@@ -283,7 +282,7 @@ namespace System.Security.Cryptography
|
||||
using var ms = new MemoryStream();
|
||||
using var cs = new CryptoStream(ms, tdes.CreateDecryptor(), CryptoStreamMode.Write);
|
||||
|
||||
cs.Write(cipher, saltLength, cipher.Length - saltLength);
|
||||
cs.Write(cipher, _saltLength, cipher.Length - _saltLength);
|
||||
cs.FlushFinalBlock();
|
||||
|
||||
return ms.ToArray();
|
||||
@@ -297,7 +296,7 @@ namespace System.Security.Cryptography
|
||||
/// <returns>The encrypted data (cipher).</returns>
|
||||
public static byte[] TripleDesEncrypt(byte[] plain, string password)
|
||||
{
|
||||
byte[] salt = GetRandomBytes(saltLength);
|
||||
byte[] salt = GetRandomBytes(_saltLength);
|
||||
|
||||
using var gen = new Rfc2898DeriveBytes(password, salt);
|
||||
using var tdes = TripleDES.Create();
|
||||
|
||||
Reference in New Issue
Block a user