From 28d9e1672960ccc54696ea1c99755e02a99c2699 Mon Sep 17 00:00:00 2001 From: FH Date: Mon, 19 Oct 2020 11:22:06 +0200 Subject: [PATCH] initial commit --- .gitignore | 3 + ExtensionsEx.cs | 37 ++++ FCS.Lib.csproj | 73 +++++++ FCS.Lib.csproj.user | 6 + Generators.cs | 298 +++++++++++++++++++++++++++++ IRepository.cs | 49 +++++ IRepositoryAsync.cs | 122 ++++++++++++ IRepositoryEx.cs | 122 ++++++++++++ Mogrifiers.cs | 356 ++++++++++++++++++++++++++++++++++ Properties/AssemblyInfo.cs | 20 ++ Properties/AssemblyInfo.tt | 41 ++++ Squid.cs | 382 +++++++++++++++++++++++++++++++++++++ StringOptions.cs | 57 ++++++ 13 files changed, 1566 insertions(+) create mode 100644 .gitignore create mode 100644 ExtensionsEx.cs create mode 100644 FCS.Lib.csproj create mode 100644 FCS.Lib.csproj.user create mode 100644 Generators.cs create mode 100644 IRepository.cs create mode 100644 IRepositoryAsync.cs create mode 100644 IRepositoryEx.cs create mode 100644 Mogrifiers.cs create mode 100644 Properties/AssemblyInfo.cs create mode 100644 Properties/AssemblyInfo.tt create mode 100644 Squid.cs create mode 100644 StringOptions.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea85d98 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/bin/**/** +/obj/**/** +/App_Data/**/** diff --git a/ExtensionsEx.cs b/ExtensionsEx.cs new file mode 100644 index 0000000..d6f47df --- /dev/null +++ b/ExtensionsEx.cs @@ -0,0 +1,37 @@ +// *********************************************************************** +// Assembly : FCS.Lib +// Author : FH +// Created : 27-08-2016 +// +// Last Modified By : Frede H. +// Last Modified On : 2020-08-30 +// *********************************************************************** +// +// Copyright © FCS 2015-2020 +// +// +// *********************************************************************** + +using System; +using System.Collections.Generic; + +namespace FCS.Lib +{ + /// + /// Class ExtensionsEx. + /// + public static class ExtensionsEx + { + /// + /// ForEach loop + /// + /// + /// The items. + /// The action. + public static void ForEach(this IEnumerable items, Action action) + { + foreach (var item in items) + action(item); + } + } +} \ No newline at end of file diff --git a/FCS.Lib.csproj b/FCS.Lib.csproj new file mode 100644 index 0000000..2e79741 --- /dev/null +++ b/FCS.Lib.csproj @@ -0,0 +1,73 @@ + + + + + 8.0 + + + Debug + AnyCPU + {1F1FECFD-E07E-4B5C-A7F9-67FB89EE94A3} + Library + Properties + FCS.Lib + FCS.Lib + v4.8 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + AssemblyInfo.tt + True + True + + + + + + + + TextTemplatingFileGenerator + AssemblyInfo.cs + + + + + "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\TextTransform.exe" -out "$(ProjectDir)Properties\AssemblyInfo.cs" "$(ProjectDir)Properties\AssemblyInfo.tt" + + \ No newline at end of file diff --git a/FCS.Lib.csproj.user b/FCS.Lib.csproj.user new file mode 100644 index 0000000..9b86104 --- /dev/null +++ b/FCS.Lib.csproj.user @@ -0,0 +1,6 @@ + + + + ShowAllFiles + + \ No newline at end of file diff --git a/Generators.cs b/Generators.cs new file mode 100644 index 0000000..3a7749f --- /dev/null +++ b/Generators.cs @@ -0,0 +1,298 @@ +// *********************************************************************** +// Assembly : FCS.Lib +// Author : FH +// Created : 2020-07-01 +// +// Last Modified By : FH +// Last Modified On : 2020-09-11 +// *********************************************************************** +// +// Copyright © FCS 2015-2020 +// +// +// *********************************************************************** + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Security.Cryptography; + +namespace FCS.Lib +{ + /// + /// Class StringUtils. + /// + public static class Generators + { + /// + /// Shorts the URL generator. + /// + /// System.String. + public static string ShortUrlGenerator() + { + return ShortUrlGenerator(6); + } + + /// + /// Randoms the string. + /// + /// The lengt h. + /// System.String. + public static string ShortUrlGenerator(int length) + { + const string charsLower = "cdfghjkmnpqrstvwxyz"; + const string charsUpper = "BCDFGHJKLMNPQRSTVWXYZ-_"; + const string charsNumeric = "23456789"; + + // Create a local array containing supported short-url characters + // grouped by types. + var charGroups = new[] + { + charsLower.ToCharArray(), + charsUpper.ToCharArray(), + charsNumeric.ToCharArray() + }; + + // Use this array to track the number of unused characters in each + // character group. + var charsLeftInGroup = new int[charGroups.Length]; + + // Initially, all characters in each group are not used. + for (var i = 0; i < charsLeftInGroup.Length; i++) + charsLeftInGroup[i] = charGroups[i].Length; + + // Use this array to track (iterate through) unused character groups. + var leftGroupsOrder = new int[charGroups.Length]; + + // Initially, all character groups are not used. + for (var i = 0; i < leftGroupsOrder.Length; i++) + leftGroupsOrder[i] = i; + + // Using our private randomizer + var random = RandomSeed(); + + // This array will hold short-url characters. + // Allocate appropriate memory for the short-url. + var shortUrl = new char[random.Next(length, length)]; + + // Index of the last non-processed group. + var lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1; + + // Generate short-url characters one at a time. + for (var i = 0; i < shortUrl.Length; i++) + { + // If only one character group remained unprocessed, process it; + // otherwise, pick a random character group from the unprocessed + // group list. To allow a special character to appear in the + // first position, increment the second parameter of the Next + // function call by one, i.e. lastLeftGroupsOrderIdx + 1. + int nextLeftGroupsOrderIdx; + if (lastLeftGroupsOrderIdx == 0) + nextLeftGroupsOrderIdx = 0; + else + nextLeftGroupsOrderIdx = random.Next(0, + lastLeftGroupsOrderIdx); + + // Get the actual index of the character group, from which we will + // pick the next character. + var nextGroupIdx = leftGroupsOrder[nextLeftGroupsOrderIdx]; + + // Get the index of the last unprocessed characters in this group. + var lastCharIdx = charsLeftInGroup[nextGroupIdx] - 1; + + // If only one unprocessed character is left, pick it; otherwise, + // get a random character from the unused character list. + var nextCharIdx = lastCharIdx == 0 ? 0 : random.Next(0, lastCharIdx + 1); + + // Add this character to the short-url. + shortUrl[i] = charGroups[nextGroupIdx][nextCharIdx]; + + // If we processed the last character in this group, start over. + if (lastCharIdx == 0) + { + charsLeftInGroup[nextGroupIdx] = + charGroups[nextGroupIdx].Length; + } + // There are more unprocessed characters left. + else + { + // Swap processed character with the last unprocessed character + // so that we don't pick it until we process all characters in + // this group. + if (lastCharIdx != nextCharIdx) + { + var temp = charGroups[nextGroupIdx][lastCharIdx]; + charGroups[nextGroupIdx][lastCharIdx] = + charGroups[nextGroupIdx][nextCharIdx]; + charGroups[nextGroupIdx][nextCharIdx] = temp; + } + + // Decrement the number of unprocessed characters in + // this group. + charsLeftInGroup[nextGroupIdx]--; + } + + // If we processed the last group, start all over. + if (lastLeftGroupsOrderIdx == 0) + { + lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1; + } + // There are more unprocessed groups left. + else + { + // Swap processed group with the last unprocessed group + // so that we don't pick it until we process all groups. + if (lastLeftGroupsOrderIdx != nextLeftGroupsOrderIdx) + { + var temp = leftGroupsOrder[lastLeftGroupsOrderIdx]; + leftGroupsOrder[lastLeftGroupsOrderIdx] = + leftGroupsOrder[nextLeftGroupsOrderIdx]; + leftGroupsOrder[nextLeftGroupsOrderIdx] = temp; + } + + // Decrement the number of unprocessed groups. + lastLeftGroupsOrderIdx--; + } + } + + // Convert password characters into a string and return the result. + return new string(shortUrl); + } + + public static string GenerateUsername(StringOptions options = null) + { + options ??= new StringOptions + { + RequiredLength = 10, + RequireDigit = true, + RequireLowercase = true, + RequireUppercase = true, + RequiredUniqueChars = 4, + RequireNonLetterOrDigit = false, + RequireNonAlphanumeric = false + }; + return GenerateRandomString(options); + } + + public static string GeneratePassword(StringOptions options = null) + { + options ??= new StringOptions + { + RequiredLength = 10, + RequireDigit = true, + RequireLowercase = true, + RequireUppercase = true, + RequiredUniqueChars = 4, + RequireNonLetterOrDigit = true, + RequireNonAlphanumeric = true + }; + return GenerateRandomString(options); + } + + public static string GenerateRandomText(int length) + { + const string consonants = "bdfghjklmnprstvyBDFGHJKLMNPRSTVY"; + const string vowels = "aeiouAEIOU"; + + var rndString = ""; + var randomNum = RandomSeed(); + + while (rndString.Length < length) + { + rndString += consonants[randomNum.Next(consonants.Length)]; + if (rndString.Length < length) + rndString += vowels[randomNum.Next(vowels.Length)]; + } + + return rndString; + } + + /// + /// Generates the random password. + /// + /// The options. + /// System.String. + public static string GenerateRandomString(StringOptions options = null) + { + options ??= new StringOptions + { + RequiredLength = 10, + RequireDigit = true, + RequireLowercase = true, + RequireUppercase = true, + RequiredUniqueChars = 4, + RequireNonLetterOrDigit = true, + RequireNonAlphanumeric = true + }; + + var randomChars = new[] + { + "ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase + "abcdefghijkmnopqrstuvwxyz", // lowercase + "0123456789", // digits + "!@$?_-" // non-alphanumeric + }; + + // Using our private randomizer + var rand = RandomSeed(); + + var chars = new List(); + + if (options.RequireUppercase) + chars.Insert(rand.Next(0, chars.Count), + randomChars[0][rand.Next(0, randomChars[0].Length)]); + + if (options.RequireLowercase) + chars.Insert(rand.Next(0, chars.Count), + randomChars[1][rand.Next(0, randomChars[1].Length)]); + + if (options.RequireDigit) + chars.Insert(rand.Next(0, chars.Count), + randomChars[2][rand.Next(0, randomChars[2].Length)]); + + if (options.RequireNonAlphanumeric) + chars.Insert(rand.Next(0, chars.Count), + randomChars[3][rand.Next(0, randomChars[3].Length)]); + + var rcs = randomChars[rand.Next(0, randomChars.Length)]; + for (var i = chars.Count; + i < options.RequiredLength + || chars.Distinct().Count() < options.RequiredUniqueChars; + i++) + chars.Insert(rand.Next(0, chars.Count), + rcs[rand.Next(0, rcs.Length)]); + + return new string(chars.ToArray()); + } + + /// + /// Randoms the seed. + /// + /// Random. + public static Random RandomSeed() + { + // As the default randomizer is based on the current time + // so it produces the same "random" number within a second + // Use a crypto randomizer to create the seed value + + // 4-byte array to fill with random bytes + var randomBytes = new byte[4]; + + // Generate 4 random bytes. + using (var rng = new RNGCryptoServiceProvider()) + { + rng.GetBytes(randomBytes); + } + + // Convert 4 bytes into a 32-bit integer value. + var seed = ((randomBytes[0] & 0x7f) << 24) | + (randomBytes[1] << 16) | + (randomBytes[2] << 8) | + randomBytes[3]; + + // Return a truly randomized random generator + return new Random(seed); + } + } +} \ No newline at end of file diff --git a/IRepository.cs b/IRepository.cs new file mode 100644 index 0000000..dc8ac50 --- /dev/null +++ b/IRepository.cs @@ -0,0 +1,49 @@ +// *********************************************************************** +// Assembly : FCS.Lib +// Author : FH +// Created : 05-13-2020 +// +// Last Modified By : FH +// Last Modified On : 2020-08-30 +// *********************************************************************** +// +// Copyright © FCS 2015-2020 +// +// +// *********************************************************************** + +namespace FCS.Lib +{ + /// + /// Interface IRepository + /// + /// + /// The type of the TKey. + public interface IRepository where T : class + { + /// + /// Gets the specified identifier. + /// + /// The identifier. + /// T. + T GetById(TKey id); + + /// + /// Creates the specified entity. + /// + /// The entity. + void Create(T entity); + + /// + /// Updates the specified entity. + /// + /// The entity. + void Update(T entity); + + /// + /// Deletes the specified identifier. + /// + /// The identifier. + void Delete(TKey id); + } +} \ No newline at end of file diff --git a/IRepositoryAsync.cs b/IRepositoryAsync.cs new file mode 100644 index 0000000..cf60032 --- /dev/null +++ b/IRepositoryAsync.cs @@ -0,0 +1,122 @@ +// *********************************************************************** +// Assembly : FCS.Lib +// Author : FH +// Created : 03-10-2015 +// +// Last Modified By : FH +// Last Modified On : 2020-08-30 +// *********************************************************************** +// +// Copyright © FCS 2015-2020 +// +// +// *********************************************************************** + +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; + +namespace FCS.Lib +{ + /// + /// Interface IRepositoryEx + /// + /// The type of the t entity + public interface IRepositoryAsync where TEntity : class + { + /// + /// Get all entities asynchronous + /// + /// Predicate + /// Task<System.Boolean> + Task AllAsync(Expression> predicate); + + /// + /// Get all entities synchronous + /// + /// Predicate + /// Task<System.Boolean> + Task AnyAsync(Expression> predicate); + + /// + /// Find matching entity asynchronous + /// + /// Predicate + /// Task<TEntity> + Task FindAsync(Expression> predicate); + + /// + /// Find first matching entity asynchronous + /// + /// Predicate + /// Task<TEntity> + Task FirstAsync(Expression> predicate); + + /// + /// Get first entity matching query or default entity asynchronous + /// + /// Predicate + /// Task<TEntity> + Task FirstOrDefaultAsync(Expression> predicate); + + /// + /// Add an entity + /// + /// The entity. + void Add(TEntity entity); + + /// + /// Attach the entity + /// + /// The entity. + void Attach(TEntity entity); + + /// + /// Delete the entity + /// + /// The entity. + void Delete(TEntity entity); + + /// + /// Anies the specified predicate. + /// + /// The predicate. + /// true if XXXX, false otherwise. + bool Any(Expression> predicate); + + /// + /// Get all entities + /// + /// IQueryable<TEntity> + IQueryable All(); + + /// + /// Find all matching entities matching query + /// + /// Predicate + /// IQueryable<TEntity> + IQueryable Find(Expression> predicate); + + /// + /// Find first entity matching query + /// + /// Predicate + /// TEntity + TEntity First(Expression> predicate); + + /// + /// Find first matching entity or default entity + /// + /// Predicate + /// TEntity + TEntity FirstOrDefault(Expression> predicate); + + /// + /// Get entity by id + /// + /// The identifier. + /// TEntity + TEntity GetById(string id); + } +} \ No newline at end of file diff --git a/IRepositoryEx.cs b/IRepositoryEx.cs new file mode 100644 index 0000000..eee7e35 --- /dev/null +++ b/IRepositoryEx.cs @@ -0,0 +1,122 @@ +// *********************************************************************** +// Assembly : FCS.Lib +// Author : FH +// Created : 03-10-2015 +// +// Last Modified By : FH +// Last Modified On : 2020-08-30 +// *********************************************************************** +// +// Copyright © FCS 2015-2020 +// +// +// *********************************************************************** + +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; + +namespace FCS.Lib +{ + /// + /// Interface IRepositoryEx + /// + /// The type of the t entity + public interface IRepositoryEx where TEntity : class + { + /// + /// Get all entities asynchronous + /// + /// Predicate + /// Task<System.Boolean> + Task AllAsync(Expression> predicate); + + /// + /// Get all entities synchronous + /// + /// Predicate + /// Task<System.Boolean> + Task AnyAsync(Expression> predicate); + + /// + /// Find matching entity asynchronous + /// + /// Predicate + /// Task<TEntity> + Task FindAsync(Expression> predicate); + + /// + /// Find first matching entity asynchronous + /// + /// Predicate + /// Task<TEntity> + Task FirstAsync(Expression> predicate); + + /// + /// Get first entity matching query or default entity asynchronous + /// + /// Predicate + /// Task<TEntity> + Task FirstOrDefaultAsync(Expression> predicate); + + /// + /// Add an entity + /// + /// The entity. + void Add(TEntity entity); + + /// + /// Attach the entity + /// + /// The entity. + void Attach(TEntity entity); + + /// + /// Delete the entity + /// + /// The entity. + void Delete(TEntity entity); + + /// + /// Anies the specified predicate. + /// + /// The predicate. + /// true if XXXX, false otherwise. + bool Any(Expression> predicate); + + /// + /// Get all entities + /// + /// IQueryable<TEntity> + IQueryable All(); + + /// + /// Find all matching entities matching query + /// + /// Predicate + /// IQueryable<TEntity> + IQueryable Find(Expression> predicate); + + /// + /// Find first entity matching query + /// + /// Predicate + /// TEntity + TEntity First(Expression> predicate); + + /// + /// Find first matching entity or default entity + /// + /// Predicate + /// TEntity + TEntity FirstOrDefault(Expression> predicate); + + /// + /// Get entity by id + /// + /// The identifier. + /// TEntity + TEntity GetById(string id); + } +} \ No newline at end of file diff --git a/Mogrifiers.cs b/Mogrifiers.cs new file mode 100644 index 0000000..18d2744 --- /dev/null +++ b/Mogrifiers.cs @@ -0,0 +1,356 @@ +// *********************************************************************** +// Assembly : FCS.Lib +// Author : FH +// Created : 27-08-2016 +// +// Last Modified By : Frede H. +// Last Modified On : 2020-08-30 +// *********************************************************************** +// +// Copyright © FCS 2015-2020 +// +// +// *********************************************************************** + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; +using System.IO; +using System.Text; + +namespace FCS.Lib +{ + /// + /// Class Converters + /// + public static class Mogrifiers + { + /// + /// Reverse boolean + /// + /// if set to true [value]. + /// true if XXXX, false otherwise. + public static bool BoolReverse(bool value) + { + return !value; + } + + /// + /// Boolean to integer + /// + /// if set to true [value]. + /// System.Int32. + public static int BoolToInt(bool value) + { + return value ? 1 : 0; + } + + /// + /// Boolean to string + /// + /// if set to true [value]. + /// System.String. + public static string BoolToString(bool value) + { + return value ? "true" : "false"; + } + + + /// + /// Enum to integer + /// + /// The enumeration. + /// System.Int32. + public static int EnumToInt(object enumeration) + { + return Convert.ToInt32(enumeration, CultureInfo.InvariantCulture); + } + + + /// + /// Enum to string. + /// + /// The value. + /// System.String. + public static string EnumToString(Enum value) + { + return value == null ? string.Empty : value.ToString(); + } + + /// + /// Integer to boolean. + /// + /// The value. + /// true if XXXX, false otherwise. + public static bool IntToBool(int value) + { + return value == 1; + } + + /// + /// Integer to enum. + /// + /// + /// The value. + /// T. + public static T IntToEnum(int value) + { + return (T) Enum.ToObject(typeof(T), value); + } + + /// + /// Integer to letter. + /// + /// The value. + /// System.String. + public static string IntToLetter(int value) + { + var empty = string.Empty; + var num = 97; + var str = ""; + var num1 = 0; + var num2 = 97; + for (var i = 0; i <= value; i++) + { + num1++; + empty = string.Concat(str, Convert.ToString(Convert.ToChar(num), CultureInfo.InvariantCulture)); + num++; + if (num1 != 26) continue; + num1 = 0; + str = Convert.ToChar(num2).ToString(CultureInfo.InvariantCulture); + num2++; + num = 97; + } + + return empty; + } + + /// + /// Lists to string using semicolon(;) + /// + /// + /// The list. + /// System.String. + public static string ListToString(List list) + { + return ListToString(list, ";"); + } + + /// + /// Lists to string userdefined delimiter + /// + /// + /// The list. + /// The delimiter. + /// System.String. + public static string ListToString(List list, string delimiter) + { + var empty = string.Empty; + if (list == null) return empty; + var enumerator = (IEnumerator) list.GetType().GetMethod("GetEnumerator")?.Invoke(list, null); + while (enumerator != null && enumerator.MoveNext()) + if (enumerator.Current != null) + empty = string.Concat(empty, enumerator.Current.ToString(), delimiter); + + return empty; + } + + + /// + /// String to bool. + /// + /// The value. + /// true if XXXX, false otherwise. + [SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "")] + public static bool StringToBool(string value) + { + if (string.IsNullOrEmpty(value)) + return false; + + var flag = false; + var upper = value.ToUpperInvariant(); + if (string.Compare(upper, "true", StringComparison.OrdinalIgnoreCase) == 0) + { + flag = true; + } + else if (string.CompareOrdinal(upper, "false") == 0) + { + } + else if (string.CompareOrdinal(upper, "1") == 0) + { + flag = true; + } + + return flag; + } + + /// + /// String to decimal. + /// + /// The in string. + /// System.Nullable<System.Decimal>. + public static decimal? StringToDecimal(string inString) + { + if (string.IsNullOrEmpty(inString)) return 0; + return + !decimal.TryParse(inString.Replace(",", "").Replace(".", ""), NumberStyles.Number, + CultureInfo.InvariantCulture, out var num) + ? (decimal?) null + : decimal.Divide(num, new decimal((long) 100)); + } + + /// + /// String to enum. + /// + /// + /// The value. + /// T. + public static T StringToEnum(string value) + { + return (T) Enum.Parse(typeof(T), value, true); + } + + /// + /// String to list using semicolon(;). + /// + /// + /// The value. + /// List<T>. + public static List StringToList(string value) + { + return StringToList(value, ";"); + } + + /// + /// String to list userdefined delimiter. + /// + /// + /// The value. + /// The delimiter. + /// List<T>. + /// value + /// delimiter + /// value + /// delimiter + public static List StringToList(string value, string delimiter) + { + if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value)); + if (string.IsNullOrEmpty(delimiter)) throw new ArgumentNullException(nameof(delimiter)); + + var ts = new List(); + var strArrays = value.Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + foreach (var str in strArrays) + { + var o = typeof(T).FullName; + if (o == null) continue; + var upperInvariant = o.ToUpperInvariant(); + if (string.CompareOrdinal(upperInvariant, "system.string") == 0) + { + ts.Add((T) Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture)); + } + else if (string.CompareOrdinal(upperInvariant, "system.int32") == 0) + { + ts.Add((T) Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture)); + } + else if (string.CompareOrdinal(upperInvariant, "system.guid") == 0) + { + var guid = new Guid(str); + ts.Add((T) Convert.ChangeType(guid, typeof(T), CultureInfo.InvariantCulture)); + } + } + + return ts; + } + + /// + /// String to stream using system default encoding. + /// + /// The value. + /// Stream. + public static Stream StringToStream(string value) + { + return StringToStream(value, Encoding.Default); + } + + + /// + /// Strings to stream with userdefined encoding. + /// + /// The value. + /// The encoding. + /// Stream. + public static Stream StringToStream(string value, Encoding encoding) + { + return encoding == null ? null : new MemoryStream(encoding.GetBytes(value ?? "")); + } + + /// + /// Returns timestamp for current date-time object. + /// + /// System.Int32. + public static long CurrentDateTimeToTimeStamp() + { + return Convert.ToUInt32(DateTimeToTimeStamp(DateTime.Now)); + } + + /// + /// Convert a DateTime object to timestamp + /// + /// The date time. + /// System.Int64. + public static long DateTimeToTimeStamp(DateTime dateTime) + { + var bigDate = new DateTime(2038, 1, 19, 0, 0, 0, 0); + var nixDate = new DateTime(1970, 1, 1, 0, 0, 0, 0); + + if (dateTime >= bigDate) + return Convert.ToUInt32((bigDate - nixDate).TotalSeconds) + + Convert.ToUInt32((dateTime - bigDate).TotalSeconds); + + return Convert.ToUInt32((dateTime - nixDate).TotalSeconds); + } + + /// + /// Convert timestamp to DataTime format + /// + /// The time stamp. + /// DateTime. + public static DateTime TimeStampToDateTime(long timeStamp) + { + var nixDate = new DateTime(1970, 1, 1, 0, 0, 0, 0); + return nixDate.AddSeconds(timeStamp); + } + + /// + /// Convert timespan to seconds + /// + /// The timespan. + /// System.Int32. + public static long TimeSpanToSeconds(TimeSpan timespan) + { + return Convert.ToUInt32(timespan.Ticks / 10000000L); + } + + /// + /// Converts seconds to timespan + /// + /// The seconds. + /// TimeSpan. + public static TimeSpan SecondsToTimeSpan(long seconds) + { + return TimeSpan.FromTicks(10000000L * seconds); + } + + /// + /// Converts timespan to minutes + /// + /// The timespan. + /// System.Int32. + public static long TimespanToMinutes(TimeSpan timespan) + { + return Convert.ToUInt32(timespan.Ticks / 10000000L) / 60; + } + } +} \ No newline at end of file diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ab7f98f --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +// This code was generated by a tool. Any changes made manually will be lost +// the next time this code is regenerated. + +using System.Reflection; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("FCSInno")] +[assembly: AssemblyDescription("FCSInno")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Frede Hundewadt")] +[assembly: AssemblyProduct("FCSInno")] +[assembly: AssemblyCopyright("Copyright © FCS 2015-2020")] +[assembly: AssemblyTrademark("FCS © Innotec Danmark A/S")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] +[assembly: Guid("aaf08873-14e5-411d-8ec8-629782ac8f03")] + +[assembly: AssemblyVersion("2.1.20289.1156")] +[assembly: AssemblyFileVersion("2.1.20289.1156")] \ No newline at end of file diff --git a/Properties/AssemblyInfo.tt b/Properties/AssemblyInfo.tt new file mode 100644 index 0000000..e3327a0 --- /dev/null +++ b/Properties/AssemblyInfo.tt @@ -0,0 +1,41 @@ +<#@ template debug="true" hostspecific="false" language="C#" #> +<#@ output extension=".cs" #> +<# + int major = 2; + int minor = 1; // Official == even, Preview == odd + int build = 1; + int revision = 0; + + string version = String.Format("{0}.{1}.{2}.{3}", major, minor, build, revision); + + if (minor % 2 == 1) // Preview build + { + // Auto-generate the build and revision + + DateTime buildTime = DateTime.Now; + + build = (1000 * (buildTime.Year % 100)) + buildTime.DayOfYear; + revision = (100 * buildTime.Hour) + buildTime.Minute; + version = String.Format("{0}.{1}.{2:00000}.{3:0000}", major, minor, build, revision); + } + #> +// This code was generated by a tool. Any changes made manually will be lost +// the next time this code is regenerated. + +using System.Reflection; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("FCSInno")] +[assembly: AssemblyDescription("FCSInno")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Frede Hundewadt")] +[assembly: AssemblyProduct("FCSInno")] +[assembly: AssemblyCopyright("Copyright © FCS 2015-2020")] +[assembly: AssemblyTrademark("FCS © Innotec Danmark A/S")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] +[assembly: Guid("aaf08873-14e5-411d-8ec8-629782ac8f03")] + +[assembly: AssemblyVersion("<#= version #>")] +[assembly: AssemblyFileVersion("<#= version #>")] \ No newline at end of file diff --git a/Squid.cs b/Squid.cs new file mode 100644 index 0000000..e9f7274 --- /dev/null +++ b/Squid.cs @@ -0,0 +1,382 @@ +// *********************************************************************** +// Assembly : FCS.Lib +// Author : FH +// Created : 2020-07-01 +// +// Last Modified By : FH +// Last Modified On : 2020-08-30 +// *********************************************************************** +// +// Copyright © FCS 2015-2020 +// +// Derived from https:github.com/csharpvitamins/CSharpVitamins.ShortGuid +// *********************************************************************** + +using System; +using System.Diagnostics; + +namespace FCS.Lib +{ + /// + /// A wrapper for handling URL-safe Base64 encoded globally unique identifiers (GUID). + /// + /// Special characters are replaced (/, +) or removed (==). + [DebuggerDisplay("{" + nameof(Value) + "}")] + public readonly struct Squid : IEquatable + { + /// + /// A read-only object of the Squid struct. + /// Value is guaranteed to be all zeroes. + /// Equivalent to . + /// + public static readonly Squid Empty = new Squid(Guid.Empty); + + /// + /// Creates a new Squid from a Squid encoded string. + /// + /// A valid Squid encodd string. + public Squid(string value) + { + Value = value; + Guid = Decode(value); + } + + /// + /// Creates a new Squid with the given . + /// + /// A valid System.Guid object. + public Squid(Guid obj) + { + Value = Encode(obj); + Guid = obj; + } + + /// + /// Gets the underlying for the encoded Squid. + /// + /// The unique identifier. +#pragma warning disable CA1720 // Identifier contains type name + public Guid Guid { get; } +#pragma warning restore CA1720 // Identifier contains type name + + /// + /// The encoded string value of the + /// as an URL-safe Base64 string. + /// + /// The value. + public string Value { get; } + + /// + /// Returns the encoded URL-safe Base64 string. + /// + /// A that represents this instance. + public override string ToString() + { + return Value; + } + + /// + /// Returns a value indicating whether this object and a specified object represent the same type and value. + /// Compares for equality against other string, Guid and Squid types. + /// + /// A Systerm.String, System.Guid or Squid object + /// true if the specified is equal to this instance; otherwise, false. + public override bool Equals(object obj) + { + return obj is Squid other && Equals(other); + } + + /// + /// Equality comparison + /// + /// A valid Squid object + /// A boolean indicating equality. + public bool Equals(Squid obj) + { + return Guid.Equals(obj.Guid) && Value == obj.Value; + } + + /// + /// Returns the hash code for the underlying . + /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + public override int GetHashCode() + { + unchecked + { + return (Guid.GetHashCode() * 397) ^ (Value != null ? Value.GetHashCode() : 0); + } + } + + /// + /// Initialises a new object of the Squid using . + /// + /// New Squid object + public static Squid NewGuid() + { + return new Squid(Guid.NewGuid()); + } + + /// + /// Encode string as a new Squid encoded string. + /// The encoding is similar to Base64 with + /// non-URL safe characters replaced, and padding removed. + /// + /// A valid .Tostring(). + /// A 22 character URL-safe Base64 string. + public static string Encode(string value) + { + var guid = new Guid(value); + return Encode(guid); + } + + /// + /// Encode a object to Squid. + /// The encoding is similar to Base64 with + /// non-URL safe characters replaced, and padding removed. + /// + /// A valid object. + /// A 22 character URL-safe Base64 string. + public static string Encode(Guid obj) + { + var encoded = Convert.ToBase64String(obj.ToByteArray()); + encoded = encoded + .Replace("/", "_") + .Replace("+", "-"); + return encoded.Substring(0, 22); + } + + /// + /// Decode Squid string to a . + /// See also or + /// . + /// + /// A valid Squid encoded string. + /// A new object from the parsed string. + public static Guid Decode(string value) + { + if (value == null) return Empty; + value = value + .Replace("_", "/") + .Replace("-", "+"); + + var blob = Convert.FromBase64String(value + "=="); + return new Guid(blob); + } + + /// + /// Squid to Guid. + /// + /// A valid Squid object. + /// System.Guid object. + public static Guid FromSquid(Squid obj) + { + return obj.Guid; + } + + /// + /// String to Squid. + /// + /// String value to convert + /// A Squid object. + public static Squid FromString(string value) + { + if (string.IsNullOrEmpty(value)) + return Empty; + return TryParse(value, out Squid obj) ? obj : Empty; + } + + /// + /// Decodes the given value to a . + /// + /// The Squid encoded string to decode. + /// A new object from the parsed string. + /// A boolean indicating if the decode was successful. + public static bool TryDecode(string value, out Guid obj) + { + try + { + // Decode as Squid + obj = Decode(value); + return true; + } +#pragma warning disable CA1031 // Do not catch general exception types + catch (Exception) +#pragma warning restore CA1031 // Do not catch general exception types + { + // Return empty Guid + obj = Guid.Empty; + return false; + } + } + + /// + /// Tries to parse the given string value and + /// outputs the object. + /// + /// The Squid encoded string or string representation of a Guid. + /// A new object from the parsed string. + /// A boolean indicating if the parse was successful. + public static bool TryParse(string value, out Squid obj) + { + // Parse as Squid string. + if (TryDecode(value, out var oGuid)) + { + obj = oGuid; + return true; + } + + // Parse as Guid string. + if (Guid.TryParse(value, out oGuid)) + { + obj = oGuid; + return true; + } + + obj = Empty; + return false; + } + + /// + /// Tries to parse the string value and + /// outputs the underlying object. + /// + /// The Squid encoded string or string representation of a Guid. + /// A new object from the parsed string. + /// A boolean indicating if the parse was successful. + public static bool TryParse(string value, out Guid obj) + { + // Try a Squid string. + if (TryDecode(value, out obj)) + return true; + + // Try a Guid string. + if (Guid.TryParse(value, out obj)) + return true; + + obj = Guid.Empty; + return false; + } + + #region Operators + + /// + /// Determines if both Squid objects have the same + /// underlying value. + /// + /// The x. + /// The y. + /// The result of the operator. + public static bool operator ==(Squid x, Squid y) + { + return x.Guid == y.Guid; + } + + /// + /// Determines if both objects have the same + /// underlying value. + /// + /// The x. + /// The y. + /// The result of the operator. + public static bool operator ==(Squid x, Guid y) + { + return x.Guid == y; + } + + /// + /// Determines if both objects have the same + /// underlying value. + /// + /// The x. + /// The y. + /// The result of the operator. + public static bool operator ==(Guid x, Squid y) + { + return y == x; // NB: order of arguments + } + + /// + /// Determines if both Squid objects do not have the same + /// underlying value. + /// + /// The x. + /// The y. + /// The result of the operator. + public static bool operator !=(Squid x, Squid y) + { + return !(x == y); + } + + /// + /// Determines if both objects do not have the same + /// underlying value. + /// + /// The x. + /// The y. + /// The result of the operator. + public static bool operator !=(Squid x, Guid y) + { + return !(x == y); + } + + /// + /// Determines if both objects do not have the same + /// underlying value. + /// + /// The x. + /// The y. + /// The result of the operator. + public static bool operator !=(Guid x, Squid y) + { + return !(x == y); + } + + /// + /// Implicitly converts the Squid to + /// its string equivalent. + /// + /// The o squid. + /// The result of the conversion. + public static implicit operator string(Squid oSquid) + { + return oSquid.Value; + } + + /// + /// Implicitly converts the Squid to + /// its equivalent. + /// + /// The o squid. + /// The result of the conversion. + public static implicit operator Guid(Squid oSquid) + { + return oSquid.Guid; + } + + /// + /// Implicitly converts the string to a Squid. + /// + /// The value. + /// The result of the conversion. + public static implicit operator Squid(string value) + { + if (string.IsNullOrEmpty(value)) + return Empty; + + return TryParse(value, out Squid oSquid) ? oSquid : Empty; + } + + /// + /// Implicitly converts the to a Squid. + /// + /// The o unique identifier. + /// The result of the conversion. + public static implicit operator Squid(Guid oGuid) + { + return oGuid == Guid.Empty ? Empty : new Squid(oGuid); + } + + #endregion + } +} \ No newline at end of file diff --git a/StringOptions.cs b/StringOptions.cs new file mode 100644 index 0000000..6a42ded --- /dev/null +++ b/StringOptions.cs @@ -0,0 +1,57 @@ +// *********************************************************************** +// Assembly : FCS.Lib +// Author : FH +// Created : 2020-09-09 +// +// Last Modified By : FH +// Last Modified On : 2020-08-30 +// *********************************************************************** +// +// Copyright © FCS 2015-2020 +// +// +// *********************************************************************** +namespace FCS.Lib +{ + /// + /// Class PasswordOptions. + /// + public class StringOptions + { + /// + /// Gets or sets the length of the required. + /// + /// The length of the required. + public int RequiredLength { get; set; } + /// + /// Gets or sets a value indicating whether [require non letter or digit]. + /// + /// true if [require non letter or digit]; otherwise, false. + public bool RequireNonLetterOrDigit { get; set; } + /// + /// Gets or sets a value indicating whether [require digit]. + /// + /// true if [require digit]; otherwise, false. + public bool RequireDigit { get; set; } + /// + /// Gets or sets a value indicating whether [require lowercase]. + /// + /// true if [require lowercase]; otherwise, false. + public bool RequireLowercase { get; set; } + /// + /// Gets or sets a value indicating whether [require uppercase]. + /// + /// true if [require uppercase]; otherwise, false. + public bool RequireUppercase { get; set; } + /// + /// Gets or sets the required unique chars. + /// + /// The required unique chars. + public int RequiredUniqueChars { get; set; } + /// + /// Gets or sets a value indicating whether [require non alphanumeric]. + /// + /// true if [require non alphanumeric]; otherwise, false. + public bool RequireNonAlphanumeric { get; set; } + } +} \ No newline at end of file