Made Nuget more generic
Moved and synced RijndaelEncryption
This commit is contained in:
@ -26,22 +26,68 @@ internal class HelperNuget
|
||||
return dateTimeOffset;
|
||||
}
|
||||
|
||||
private static bool ExtractKeyFileAndSetDateFromZipEntry(ILogger<Worker> logger, string[] zipFiles, string keyFileExtension, string keyFileExtensionB, string keyFileExtensionC, bool renameToLower)
|
||||
private static void SetLastWriteTimes(ILogger<Worker> logger, NugetConfiguration nugetConfiguration, FileInfo fileInfo, DateTimeOffset dateTimeOffset)
|
||||
{
|
||||
try
|
||||
{
|
||||
string[] files;
|
||||
if (fileInfo.LastWriteTime != dateTimeOffset.LocalDateTime)
|
||||
File.SetLastWriteTime(fileInfo.FullName, dateTimeOffset.LocalDateTime);
|
||||
if (!string.IsNullOrEmpty(nugetConfiguration.KeyFileExtensionB))
|
||||
{
|
||||
if (fileInfo.DirectoryName is null)
|
||||
throw new NullReferenceException(nameof(fileInfo.DirectoryName));
|
||||
files = Directory.GetFiles(fileInfo.DirectoryName, nugetConfiguration.KeyFileExtensionB, SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileInfo = new(file);
|
||||
if (fileInfo.LastWriteTime != dateTimeOffset.LocalDateTime)
|
||||
File.SetLastWriteTime(fileInfo.FullName, dateTimeOffset.LocalDateTime);
|
||||
}
|
||||
}
|
||||
if (!string.IsNullOrEmpty(nugetConfiguration.KeyFileExtensionC))
|
||||
{
|
||||
if (fileInfo.DirectoryName is null)
|
||||
throw new NullReferenceException(nameof(fileInfo.DirectoryName));
|
||||
files = Directory.GetFiles(fileInfo.DirectoryName, nugetConfiguration.KeyFileExtensionC, SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileInfo = new(file);
|
||||
if (fileInfo.LastWriteTime != dateTimeOffset.LocalDateTime)
|
||||
File.SetLastWriteTime(fileInfo.FullName, dateTimeOffset.LocalDateTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
string checkFile;
|
||||
logger.LogInformation("<{fileInfo.FullName}> is invalid!", fileInfo.FullName);
|
||||
checkFile = string.Concat(fileInfo.FullName, ".err");
|
||||
for (int e = 0; e < short.MaxValue; e++)
|
||||
{
|
||||
if (!File.Exists(checkFile))
|
||||
break;
|
||||
checkFile = string.Concat(checkFile, e);
|
||||
}
|
||||
try
|
||||
{ File.Move(fileInfo.FullName, checkFile); }
|
||||
catch (Exception) { logger.LogInformation("<{fileInfo.FullName}> couldn't be moved!", fileInfo.FullName); }
|
||||
}
|
||||
}
|
||||
|
||||
private static void ExtractKeyFileAndSetDateFromZipEntry(ILogger<Worker> logger, NugetConfiguration nugetConfiguration, string[] zipFiles)
|
||||
{
|
||||
bool result = false;
|
||||
string[] files;
|
||||
string checkFile;
|
||||
string? lowerName;
|
||||
FileInfo fileInfo;
|
||||
string? lowerName;
|
||||
FileInfo extractKeyFileInfo;
|
||||
DateTimeOffset? dateTimeOffset;
|
||||
foreach (string zipFile in zipFiles)
|
||||
{
|
||||
fileInfo = new(zipFile);
|
||||
if (fileInfo.DirectoryName is null)
|
||||
throw new NullReferenceException(nameof(fileInfo.DirectoryName));
|
||||
lowerName = !renameToLower ? null : Path.Combine(fileInfo.DirectoryName, fileInfo.Name.ToLower());
|
||||
if (renameToLower && lowerName is not null && lowerName != fileInfo.FullName)
|
||||
lowerName = !nugetConfiguration.RenameToLower ? null : Path.Combine(fileInfo.DirectoryName, fileInfo.Name.ToLower());
|
||||
if (nugetConfiguration.RenameToLower && lowerName is not null && lowerName != fileInfo.FullName)
|
||||
{
|
||||
files = Directory.GetFiles(fileInfo.DirectoryName, $"{Path.GetFileNameWithoutExtension(fileInfo.Name)}*", SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
@ -50,69 +96,18 @@ internal class HelperNuget
|
||||
if (fileInfo.DirectoryName is null)
|
||||
throw new NullReferenceException(nameof(fileInfo.DirectoryName));
|
||||
}
|
||||
extractKeyFileInfo = new(Path.Combine(fileInfo.DirectoryName, $"{Path.GetFileNameWithoutExtension(fileInfo.Name)}{keyFileExtension}"));
|
||||
extractKeyFileInfo = new(Path.Combine(fileInfo.DirectoryName, $"{Path.GetFileNameWithoutExtension(fileInfo.Name)}{nugetConfiguration.KeyFileExtension}"));
|
||||
if (extractKeyFileInfo.Exists)
|
||||
{
|
||||
if (extractKeyFileInfo.CreationTime.ToString("yyyy-MM-dd") == fileInfo.CreationTime.ToString("yyyy-MM-dd") && extractKeyFileInfo.LastWriteTime.ToString("yyyy-MM-dd") == fileInfo.LastWriteTime.ToString("yyyy-MM-dd"))
|
||||
continue;
|
||||
File.Delete(extractKeyFileInfo.FullName);
|
||||
}
|
||||
try
|
||||
{
|
||||
dateTimeOffset = GetDateTimeOffset(keyFileExtension, fileInfo, extractKeyFileInfo);
|
||||
if (dateTimeOffset is null)
|
||||
continue;
|
||||
if (fileInfo.LastWriteTime != dateTimeOffset.Value.LocalDateTime)
|
||||
{
|
||||
File.SetLastWriteTime(fileInfo.FullName, dateTimeOffset.Value.LocalDateTime);
|
||||
if (!result)
|
||||
result = true;
|
||||
}
|
||||
if (string.IsNullOrEmpty(keyFileExtensionB))
|
||||
continue;
|
||||
files = Directory.GetFiles(fileInfo.DirectoryName, keyFileExtensionB, SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileInfo = new(file);
|
||||
if (fileInfo.LastWriteTime != dateTimeOffset.Value.LocalDateTime)
|
||||
{
|
||||
File.SetLastWriteTime(fileInfo.FullName, dateTimeOffset.Value.LocalDateTime);
|
||||
if (!result)
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
if (string.IsNullOrEmpty(keyFileExtensionC))
|
||||
continue;
|
||||
if (fileInfo.DirectoryName is null)
|
||||
throw new NullReferenceException(nameof(fileInfo.DirectoryName));
|
||||
files = Directory.GetFiles(fileInfo.DirectoryName, keyFileExtensionC, SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileInfo = new(file);
|
||||
if (fileInfo.LastWriteTime != dateTimeOffset.Value.LocalDateTime)
|
||||
{
|
||||
File.SetLastWriteTime(fileInfo.FullName, dateTimeOffset.Value.LocalDateTime);
|
||||
if (!result)
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
logger.LogInformation("<{zipFile}> is invalid!", zipFile);
|
||||
checkFile = string.Concat(zipFile, ".err");
|
||||
for (int e = 0; e < short.MaxValue; e++)
|
||||
{
|
||||
if (!File.Exists(checkFile))
|
||||
break;
|
||||
checkFile = string.Concat(checkFile, e);
|
||||
}
|
||||
try
|
||||
{ File.Move(zipFile, checkFile); }
|
||||
catch (Exception) { logger.LogInformation("<{zipFile}> couldn't be moved!", zipFile); }
|
||||
}
|
||||
DateTimeOffset? dateTimeOffset = GetDateTimeOffset(nugetConfiguration.KeyFileExtension, fileInfo, extractKeyFileInfo);
|
||||
if (dateTimeOffset is null)
|
||||
continue;
|
||||
SetLastWriteTimes(logger, nugetConfiguration, fileInfo, dateTimeOffset.Value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int CopyFiles(NugetConfiguration nugetConfiguration, string[] files)
|
||||
@ -129,6 +124,12 @@ internal class HelperNuget
|
||||
File.Copy(fileInfo.FullName, checkFileInfo.FullName, overwrite: true);
|
||||
results++;
|
||||
}
|
||||
if (string.IsNullOrEmpty(nugetConfiguration.KeyFileExtension))
|
||||
continue;
|
||||
fileInfo = new(file);
|
||||
checkFileInfo = new(Path.Combine(nugetConfiguration.Destination, Path.ChangeExtension(fileInfo.Name, nugetConfiguration.KeyFileExtension)));
|
||||
if (!checkFileInfo.Exists || fileInfo.LastWriteTime != checkFileInfo.LastWriteTime || fileInfo.Length != checkFileInfo.Length)
|
||||
File.Copy(fileInfo.FullName, checkFileInfo.FullName, overwrite: true);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
@ -142,7 +143,7 @@ internal class HelperNuget
|
||||
_ = Directory.CreateDirectory(nugetConfiguration.Destination);
|
||||
string[] files = Directory.GetFiles(nugetConfiguration.Source, nugetConfiguration.SearchPattern, SearchOption.AllDirectories);
|
||||
logger.LogInformation("Found {Files} file(s)", files.Length);
|
||||
_ = ExtractKeyFileAndSetDateFromZipEntry(logger, files, nugetConfiguration.KeyFileExtension, nugetConfiguration.KeyFileExtensionB, nugetConfiguration.KeyFileExtensionC, nugetConfiguration.RenameToLower);
|
||||
ExtractKeyFileAndSetDateFromZipEntry(logger, nugetConfiguration, files);
|
||||
logger.LogInformation("{Files} file(s) verified", files.Length);
|
||||
int filesCopied = CopyFiles(nugetConfiguration, files);
|
||||
logger.LogInformation("Copied {FilesCopied} file(s)", filesCopied);
|
||||
|
100
Helpers/RijndaelEncryption.cs
Normal file
100
Helpers/RijndaelEncryption.cs
Normal file
@ -0,0 +1,100 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace File_Watcher.Helpers;
|
||||
|
||||
public static class RijndaelEncryption
|
||||
{ // cSpell:disable
|
||||
|
||||
/// <summary>
|
||||
/// Change the input key GUID when you use this code in your own program.
|
||||
/// Keep this input key very safe and prevent someone from decoding it some way!!
|
||||
/// Generated 2021-08-10
|
||||
/// </summary>
|
||||
internal const string _InputKey = "970CCEF6-4307-4F6A-9AC8-377DADB889BD";
|
||||
|
||||
/// <summary>
|
||||
/// Encrypt the given text and give the byte array back as a BASE64 string
|
||||
/// </summary>
|
||||
/// <param name="text">The text to encrypt</param>
|
||||
/// <param name="salt">The password salt</param>
|
||||
/// <returns>The encrypted text</returns>
|
||||
public static string Encrypt(string text, string salt)
|
||||
{
|
||||
string result;
|
||||
if (string.IsNullOrEmpty(text))
|
||||
throw new ArgumentNullException(nameof(text));
|
||||
#pragma warning disable SYSLIB0022
|
||||
RijndaelManaged aesAlg = NewRijndaelManaged(salt);
|
||||
#pragma warning restore
|
||||
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
|
||||
MemoryStream msEncrypt = new();
|
||||
using (CryptoStream csEncrypt = new(msEncrypt, encryptor, CryptoStreamMode.Write))
|
||||
using (StreamWriter swEncrypt = new(csEncrypt))
|
||||
swEncrypt.Write(text);
|
||||
result = Convert.ToBase64String(msEncrypt.ToArray());
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a string is base64 encoded
|
||||
/// </summary>
|
||||
/// <param name="base64String">The base64 encoded string</param>
|
||||
/// <returns></returns>
|
||||
public static bool IsBase64String(string base64String)
|
||||
{
|
||||
bool result;
|
||||
base64String = base64String.Trim();
|
||||
#pragma warning restore
|
||||
result = (base64String.Length % 4 == 0) && Regex.IsMatch(base64String, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None);
|
||||
#pragma warning restore
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts the given text
|
||||
/// </summary>
|
||||
/// <param name="cipherText">The encrypted BASE64 text</param>
|
||||
/// <param name="salt">The password salt</param>
|
||||
/// <returns>De gedecrypte text</returns>
|
||||
public static string Decrypt(string cipherText, string salt)
|
||||
{
|
||||
if (string.IsNullOrEmpty(cipherText))
|
||||
throw new ArgumentNullException(nameof(cipherText));
|
||||
if (!IsBase64String(cipherText))
|
||||
throw new Exception("The cipherText input parameter is not base64 encoded");
|
||||
string text;
|
||||
#pragma warning disable SYSLIB0022
|
||||
RijndaelManaged aesAlg = NewRijndaelManaged(salt);
|
||||
#pragma warning restore
|
||||
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
|
||||
byte[] cipher = Convert.FromBase64String(cipherText);
|
||||
using (MemoryStream msDecrypt = new(cipher))
|
||||
{
|
||||
using CryptoStream csDecrypt = new(msDecrypt, decryptor, CryptoStreamMode.Read);
|
||||
using StreamReader srDecrypt = new(csDecrypt);
|
||||
text = srDecrypt.ReadToEnd();
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new RijndaelManaged class and initialize it
|
||||
/// </summary>
|
||||
/// <param name="salt">The password salt</param>
|
||||
/// <returns></returns>
|
||||
#pragma warning disable SYSLIB0022, SYSLIB0041, CA5379
|
||||
private static RijndaelManaged NewRijndaelManaged(string salt)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(salt);
|
||||
byte[] saltBytes = Encoding.ASCII.GetBytes(salt);
|
||||
Rfc2898DeriveBytes key = new(_InputKey, saltBytes);
|
||||
RijndaelManaged aesAlg = new();
|
||||
#pragma warning restore
|
||||
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
|
||||
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
|
||||
return aesAlg;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user