Made Nuget more generic
Moved and synced RijndaelEncryption
This commit is contained in:
parent
4eb70db231
commit
878750b284
2
.vscode/tasks.json
vendored
2
.vscode/tasks.json
vendored
@ -22,7 +22,7 @@
|
|||||||
"-p",
|
"-p",
|
||||||
"${workspaceFolder}/File-Watcher.csproj",
|
"${workspaceFolder}/File-Watcher.csproj",
|
||||||
"set",
|
"set",
|
||||||
"asdf",
|
"_UserSecretsId",
|
||||||
"6516d19d6569"
|
"6516d19d6569"
|
||||||
],
|
],
|
||||||
"problemMatcher": "$msCompile"
|
"problemMatcher": "$msCompile"
|
||||||
|
@ -26,22 +26,68 @@ internal class HelperNuget
|
|||||||
return dateTimeOffset;
|
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
|
||||||
{
|
{
|
||||||
bool result = false;
|
|
||||||
string[] files;
|
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;
|
string checkFile;
|
||||||
string? lowerName;
|
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)
|
||||||
|
{
|
||||||
|
string[] files;
|
||||||
FileInfo fileInfo;
|
FileInfo fileInfo;
|
||||||
|
string? lowerName;
|
||||||
FileInfo extractKeyFileInfo;
|
FileInfo extractKeyFileInfo;
|
||||||
DateTimeOffset? dateTimeOffset;
|
|
||||||
foreach (string zipFile in zipFiles)
|
foreach (string zipFile in zipFiles)
|
||||||
{
|
{
|
||||||
fileInfo = new(zipFile);
|
fileInfo = new(zipFile);
|
||||||
if (fileInfo.DirectoryName is null)
|
if (fileInfo.DirectoryName is null)
|
||||||
throw new NullReferenceException(nameof(fileInfo.DirectoryName));
|
throw new NullReferenceException(nameof(fileInfo.DirectoryName));
|
||||||
lowerName = !renameToLower ? null : Path.Combine(fileInfo.DirectoryName, fileInfo.Name.ToLower());
|
lowerName = !nugetConfiguration.RenameToLower ? null : Path.Combine(fileInfo.DirectoryName, fileInfo.Name.ToLower());
|
||||||
if (renameToLower && lowerName is not null && lowerName != fileInfo.FullName)
|
if (nugetConfiguration.RenameToLower && lowerName is not null && lowerName != fileInfo.FullName)
|
||||||
{
|
{
|
||||||
files = Directory.GetFiles(fileInfo.DirectoryName, $"{Path.GetFileNameWithoutExtension(fileInfo.Name)}*", SearchOption.TopDirectoryOnly);
|
files = Directory.GetFiles(fileInfo.DirectoryName, $"{Path.GetFileNameWithoutExtension(fileInfo.Name)}*", SearchOption.TopDirectoryOnly);
|
||||||
foreach (string file in files)
|
foreach (string file in files)
|
||||||
@ -50,69 +96,18 @@ internal class HelperNuget
|
|||||||
if (fileInfo.DirectoryName is null)
|
if (fileInfo.DirectoryName is null)
|
||||||
throw new NullReferenceException(nameof(fileInfo.DirectoryName));
|
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.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"))
|
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;
|
continue;
|
||||||
File.Delete(extractKeyFileInfo.FullName);
|
File.Delete(extractKeyFileInfo.FullName);
|
||||||
}
|
}
|
||||||
try
|
DateTimeOffset? dateTimeOffset = GetDateTimeOffset(nugetConfiguration.KeyFileExtension, fileInfo, extractKeyFileInfo);
|
||||||
{
|
|
||||||
dateTimeOffset = GetDateTimeOffset(keyFileExtension, fileInfo, extractKeyFileInfo);
|
|
||||||
if (dateTimeOffset is null)
|
if (dateTimeOffset is null)
|
||||||
continue;
|
continue;
|
||||||
if (fileInfo.LastWriteTime != dateTimeOffset.Value.LocalDateTime)
|
SetLastWriteTimes(logger, nugetConfiguration, fileInfo, dateTimeOffset.Value);
|
||||||
{
|
|
||||||
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); }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int CopyFiles(NugetConfiguration nugetConfiguration, string[] files)
|
private static int CopyFiles(NugetConfiguration nugetConfiguration, string[] files)
|
||||||
@ -129,6 +124,12 @@ internal class HelperNuget
|
|||||||
File.Copy(fileInfo.FullName, checkFileInfo.FullName, overwrite: true);
|
File.Copy(fileInfo.FullName, checkFileInfo.FullName, overwrite: true);
|
||||||
results++;
|
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;
|
return results;
|
||||||
}
|
}
|
||||||
@ -142,7 +143,7 @@ internal class HelperNuget
|
|||||||
_ = Directory.CreateDirectory(nugetConfiguration.Destination);
|
_ = Directory.CreateDirectory(nugetConfiguration.Destination);
|
||||||
string[] files = Directory.GetFiles(nugetConfiguration.Source, nugetConfiguration.SearchPattern, SearchOption.AllDirectories);
|
string[] files = Directory.GetFiles(nugetConfiguration.Source, nugetConfiguration.SearchPattern, SearchOption.AllDirectories);
|
||||||
logger.LogInformation("Found {Files} file(s)", files.Length);
|
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);
|
logger.LogInformation("{Files} file(s) verified", files.Length);
|
||||||
int filesCopied = CopyFiles(nugetConfiguration, files);
|
int filesCopied = CopyFiles(nugetConfiguration, files);
|
||||||
logger.LogInformation("Copied {FilesCopied} file(s)", filesCopied);
|
logger.LogInformation("Copied {FilesCopied} file(s)", filesCopied);
|
||||||
|
@ -2,10 +2,10 @@ using System.Security.Cryptography;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace File_Watcher.Models;
|
namespace File_Watcher.Helpers;
|
||||||
|
|
||||||
public static class RijndaelEncryption
|
public static class RijndaelEncryption
|
||||||
{
|
{ // cSpell:disable
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Change the input key GUID when you use this code in your own program.
|
/// Change the input key GUID when you use this code in your own program.
|
||||||
@ -25,7 +25,7 @@ public static class RijndaelEncryption
|
|||||||
string result;
|
string result;
|
||||||
if (string.IsNullOrEmpty(text))
|
if (string.IsNullOrEmpty(text))
|
||||||
throw new ArgumentNullException(nameof(text));
|
throw new ArgumentNullException(nameof(text));
|
||||||
#pragma warning disable
|
#pragma warning disable SYSLIB0022
|
||||||
RijndaelManaged aesAlg = NewRijndaelManaged(salt);
|
RijndaelManaged aesAlg = NewRijndaelManaged(salt);
|
||||||
#pragma warning restore
|
#pragma warning restore
|
||||||
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
|
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
|
||||||
@ -46,7 +46,7 @@ public static class RijndaelEncryption
|
|||||||
{
|
{
|
||||||
bool result;
|
bool result;
|
||||||
base64String = base64String.Trim();
|
base64String = base64String.Trim();
|
||||||
#pragma warning disable
|
#pragma warning restore
|
||||||
result = (base64String.Length % 4 == 0) && Regex.IsMatch(base64String, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None);
|
result = (base64String.Length % 4 == 0) && Regex.IsMatch(base64String, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None);
|
||||||
#pragma warning restore
|
#pragma warning restore
|
||||||
return result;
|
return result;
|
||||||
@ -65,7 +65,7 @@ public static class RijndaelEncryption
|
|||||||
if (!IsBase64String(cipherText))
|
if (!IsBase64String(cipherText))
|
||||||
throw new Exception("The cipherText input parameter is not base64 encoded");
|
throw new Exception("The cipherText input parameter is not base64 encoded");
|
||||||
string text;
|
string text;
|
||||||
#pragma warning disable
|
#pragma warning disable SYSLIB0022
|
||||||
RijndaelManaged aesAlg = NewRijndaelManaged(salt);
|
RijndaelManaged aesAlg = NewRijndaelManaged(salt);
|
||||||
#pragma warning restore
|
#pragma warning restore
|
||||||
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
|
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
|
||||||
@ -84,11 +84,10 @@ public static class RijndaelEncryption
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="salt">The password salt</param>
|
/// <param name="salt">The password salt</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
#pragma warning disable
|
#pragma warning disable SYSLIB0022, SYSLIB0041, CA5379
|
||||||
private static RijndaelManaged NewRijndaelManaged(string salt)
|
private static RijndaelManaged NewRijndaelManaged(string salt)
|
||||||
{
|
{
|
||||||
if (salt == null)
|
ArgumentNullException.ThrowIfNull(salt);
|
||||||
throw new ArgumentNullException(nameof(salt));
|
|
||||||
byte[] saltBytes = Encoding.ASCII.GetBytes(salt);
|
byte[] saltBytes = Encoding.ASCII.GetBytes(salt);
|
||||||
Rfc2898DeriveBytes key = new(_InputKey, saltBytes);
|
Rfc2898DeriveBytes key = new(_InputKey, saltBytes);
|
||||||
RijndaelManaged aesAlg = new();
|
RijndaelManaged aesAlg = new();
|
@ -52,7 +52,7 @@ public partial class Worker : BackgroundService
|
|||||||
private async Task Body(CancellationToken cancellationToken)
|
private async Task Body(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (!_IsWindowsService)
|
if (!_IsWindowsService)
|
||||||
throw new EvaluateException("Set break point and skip!");
|
throw new EvaluateException($"Set break point and skip to run {_AppSettings.Helper}!");
|
||||||
if (!_IsWindowsService)
|
if (!_IsWindowsService)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < int.MaxValue; i++)
|
for (int i = 0; i < int.MaxValue; i++)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user