From 4f6ca3524a9acfd1299569a59aea2fef18723c0f Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Wed, 18 Dec 2024 17:51:02 +0100 Subject: [PATCH] =?UTF-8?q?feat(AsymCryptParams):=20DateTagFormat=20als=20?= =?UTF-8?q?Subtext=20des=20Dateinamens=20f=C3=BCr=20die=20periodische=20Ak?= =?UTF-8?q?tualisierung=20von=20pem-Dateien=20hinzugef=C3=BCgt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Config/AsymCryptParams.cs | 24 +++++-- .../Config/StringExtensions.cs | 71 +++++++++++++++++++ 2 files changed, 90 insertions(+), 5 deletions(-) diff --git a/DigitalData.Core.Security/Config/AsymCryptParams.cs b/DigitalData.Core.Security/Config/AsymCryptParams.cs index 13b658b..c4ef697 100644 --- a/DigitalData.Core.Security/Config/AsymCryptParams.cs +++ b/DigitalData.Core.Security/Config/AsymCryptParams.cs @@ -36,13 +36,28 @@ namespace DigitalData.Core.Security.Config /// public string KeyNameSeparator { get; init; } = ":"; + /// + ///This is the subtext of the pem file name. For the file to be automatically renewed, the name must be assigned to change periodically. For example, by default MM/2 will be refreshed every 2 months. + ///
+ /// - is used when converting to tag. + ///
+ /// - If the format contains the "//" symbol, the method divides the numeric value derived from the left side of the format + /// by the numeric value of the right side of the format string. For instance: + ///
+ /// - If the date is 02.03.2024 and the format is "MM//2", it extracts the month (02), divides it by 2 (resulting in 1), + /// and returns "1". + ///
+ /// - If the format does not contain "//", the method uses the default format. + ///
+ /// This method provides a way to format the date based on typical or customized rules, including mathematical operations like division. + ///
+ public string DateTagFormat { get; init; } = "MM//2"; + public IEnumerable Decryptors { get; init; } = new List(); public RSADecryptor? Vault { get; init; } - public AsymCryptParams() - { - AfterCreate += () => + public AsymCryptParams() => AfterCreate += () => { // Create root folder if it does not exist if (!Directory.Exists(PemDirectory)) @@ -53,7 +68,7 @@ namespace DigitalData.Core.Security.Config // set default path if (decryptor.IsPemNull) { - var file_name_params = new List { decryptor.Issuer, decryptor.Audience, KeySizeInBits }; + var file_name_params = new List { decryptor.Issuer, decryptor.Audience, KeySizeInBits, DateTime.Now.ToTag(DateTagFormat) }; if (decryptor.IsEncrypted) file_name_params.Add(Secrets.Version); @@ -77,6 +92,5 @@ namespace DigitalData.Core.Security.Config } } }; - } } } \ No newline at end of file diff --git a/DigitalData.Core.Security/Config/StringExtensions.cs b/DigitalData.Core.Security/Config/StringExtensions.cs index 716ff3d..379af2e 100644 --- a/DigitalData.Core.Security/Config/StringExtensions.cs +++ b/DigitalData.Core.Security/Config/StringExtensions.cs @@ -9,5 +9,76 @@ public static byte[] ToBytes(this string str) => System.Text.Encoding.UTF8.GetBytes(str); public static string BytesToString(this byte[] bytes) => System.Text.Encoding.UTF8.GetString(bytes); + + /// + /// Converts a to a formatted string based on the specified format string. + ///
+ /// - If the format contains the "//" symbol, the method divides the numeric value derived from the left side of the format + /// by the numeric value of the right side of the format string. For instance: + ///
+ /// - If the date is 02.03.2024 and the format is "MM//2", it extracts the month (02), divides it by 2 (resulting in 1), + /// and returns "1". + ///
+ /// - If the format does not contain "//", the method uses the default format. + ///
+ /// This method provides a way to format the date based on typical or customized rules, including mathematical operations like division. + ///
+ /// The value to be formatted. + /// The format string that dictates the formatting of the date. If the format includes the "//" symbol, + /// it splits the string at "//" and divides the left-side value by the right-side value. The format string can include standard + /// format patterns. + /// A string representation of the formatted date, or the result of the division operation if "//" is present in the format. + /// Thrown if the format string is invalid, such as having an incorrect number of parts after "//". + /// Thrown if the right side of the "//" contains a zero, resulting in division by zero. + /// Thrown if either the left-side or right-side value of "//" cannot be parsed as an integer. + public static string ToTag(this DateTime date, string format) + { + if (format is not null && format.Contains("//")) + { + var subStrings = format.Split("//"); + + if (subStrings.Length != 2) + throw new ArgumentException("Format is invalid. It must contain exactly one '//' separator."); + + var formattedLeft = date.ToString(subStrings[0]); + + if (!int.TryParse(formattedLeft, out var dateValue)) + throw new FormatException("The left-side value of the format could not be parsed to an integer."); + + if (!int.TryParse(subStrings[1], out var mode)) + throw new FormatException("The right-side value of the format could not be parsed to an integer."); + + if (mode == 0) + throw new DivideByZeroException("Division by zero is not allowed."); + + var result = dateValue / mode; + return result.ToString(); + } + + return date.ToString(format); + } + + /// + /// Converts a to a formatted string based on the specified format string. + ///
+ /// - If the format contains the "//" symbol, the method divides the numeric value derived from the left side of the format + /// by the numeric value of the right side of the format string. For instance: + ///
+ /// - If the date is 02.03.2024 and the format is "MM//2", it extracts the month (02), divides it by 2 (resulting in 1), + /// and returns "1". + ///
+ /// - If the format does not contain "//", the method uses the default format. + ///
+ /// This method provides a way to format the date based on typical or customized rules, including mathematical operations like division. + ///
+ /// The value to be formatted. It will convert to DateTime to use the method shared with DateTime. + /// The format string that dictates the formatting of the date. If the format includes the "//" symbol, + /// it splits the string at "//" and divides the left-side value by the right-side value. The format string can include standard + /// format patterns. + /// A string representation of the formatted date, or the result of the division operation if "//" is present in the format. + /// Thrown if the format string is invalid, such as having an incorrect number of parts after "//". + /// Thrown if the right side of the "//" contains a zero, resulting in division by zero. + /// Thrown if either the left-side or right-side value of "//" cannot be parsed as an integer. + public static string ToTag(this DateOnly date, string format) => date.ToDateTime(new()).ToTag(format); } } \ No newline at end of file