From 0ccf7ce8445e7e4a24da120885084a4e8a40295a Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sat, 15 Jun 2024 07:23:30 -0400 Subject: [PATCH 1/3] remove MBS.Desktop from framework-dotnet; it is now in desktop-framework-dotnet --- .../src/lib/MBS.Desktop/DesktopApplication.cs | 6 ------ .../src/lib/MBS.Desktop/MBS.Desktop.csproj | 13 ------------- 2 files changed, 19 deletions(-) delete mode 100644 framework-dotnet/src/lib/MBS.Desktop/DesktopApplication.cs delete mode 100644 framework-dotnet/src/lib/MBS.Desktop/MBS.Desktop.csproj diff --git a/framework-dotnet/src/lib/MBS.Desktop/DesktopApplication.cs b/framework-dotnet/src/lib/MBS.Desktop/DesktopApplication.cs deleted file mode 100644 index 1e0b72a..0000000 --- a/framework-dotnet/src/lib/MBS.Desktop/DesktopApplication.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace MBS.Desktop; - -public class DesktopApplication : MBS.Core.Application -{ - -} diff --git a/framework-dotnet/src/lib/MBS.Desktop/MBS.Desktop.csproj b/framework-dotnet/src/lib/MBS.Desktop/MBS.Desktop.csproj deleted file mode 100644 index 020ce4d..0000000 --- a/framework-dotnet/src/lib/MBS.Desktop/MBS.Desktop.csproj +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - net8.0 - enable - enable - - - From 80bf8317c45a91e43b08ebf49e57673ded386d4b Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sat, 15 Jun 2024 23:06:48 -0400 Subject: [PATCH 2/3] import ArrayExtensions from .NET Classic MBS.Framework --- .../src/lib/MBS.Core/ArrayExtensions.cs | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 framework-dotnet/src/lib/MBS.Core/ArrayExtensions.cs diff --git a/framework-dotnet/src/lib/MBS.Core/ArrayExtensions.cs b/framework-dotnet/src/lib/MBS.Core/ArrayExtensions.cs new file mode 100644 index 0000000..179bf3f --- /dev/null +++ b/framework-dotnet/src/lib/MBS.Core/ArrayExtensions.cs @@ -0,0 +1,104 @@ +// +// ArrayExtensions.cs +// +// Author: +// Mike Becker +// +// Copyright (c) 2019 Mike Becker +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using System.Collections.Generic; + +namespace MBS.Core; + +public static class ArrayExtensions +{ + /// + /// Splits the into two arrays at the specified + /// , where contains the + /// elements before and + /// contains the elements after . + /// + /// The array to split. + /// The index at which to split the array. + /// The array containing elements before the split point. + /// The array containing elements after the split point. + /// The of elements in the array. + public static void Bisect(this T[] array, int index, out T[] left, out T[] right) + { + left = new T[index]; + right = new T[array.Length - index]; + + Array.Copy(array, 0, left, 0, left.Length); + Array.Copy(array, index, right, 0, right.Length); + } + public static void Array_RemoveAt(ref T[] array, int index, int count = 1) + { + T[] old = (T[])array.Clone(); + + int start = index; + int length = count; + if (count < 0) + { + start = index + count; + length = Math.Abs(count); + } + Array.Resize(ref array, old.Length - length); + + Array.Copy(old, 0, array, 0, start); + if (array.Length - (start + length) > -1) + Array.Copy(old, start + length, array, start, array.Length - (start + length)); + } + public static void Array_Append(ref T[] destinationArray, T[] sourceArray) + { + int start = destinationArray.Length; + Array.Resize(ref destinationArray, destinationArray.Length + sourceArray.Length); + Array.Copy(sourceArray, 0, destinationArray, start, sourceArray.Length); + } + + /// + /// Determines if and + /// contain the same elements. + /// + /// + /// if both arrays contain the same elements, + /// otherwise. + /// The first array to search. + /// The second array to search. + /// The 1st type parameter. + public static bool Matches(this IList array1, IList array2) + { + if (array1.Count != array2.Count) + { + // short-circuit if arrays have different lengths + return false; + } + + for (int i = 0; i < array1.Count; i++) + { + if (!array1[i].Equals(array2[i])) + return false; + } + return true; + } + + public static T[] Concat(T[] array1, T[] array2) + { + T[] array3 = new T[array1.Length + array2.Length]; + Array.Copy(array1, 0, array3, 0, array1.Length); + Array.Copy(array2, 0, array3, array1.Length, array2.Length); + return array3; + } +} From 1ec0b7be57c77a502b1f22b49c9b19855a4ea549 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sat, 15 Jun 2024 23:07:03 -0400 Subject: [PATCH 3/3] import NanoId from .NET Classic MBS.Framework --- framework-dotnet/src/lib/MBS.Core/NanoId.cs | 281 ++++++++++++++++++++ 1 file changed, 281 insertions(+) create mode 100755 framework-dotnet/src/lib/MBS.Core/NanoId.cs diff --git a/framework-dotnet/src/lib/MBS.Core/NanoId.cs b/framework-dotnet/src/lib/MBS.Core/NanoId.cs new file mode 100755 index 0000000..6259def --- /dev/null +++ b/framework-dotnet/src/lib/MBS.Core/NanoId.cs @@ -0,0 +1,281 @@ +// +// NanoId.cs - .NET implementation of cryptographically-strong ID generator +// +// Author: +// zhu yu +// Michael Becker +// +// Copyright (c) 2017 zhu yu +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +using System; +using System.Security.Cryptography; +using System.Threading.Tasks; + +namespace MBS.Core +{ + public struct NanoId + { + private readonly string _value; + private readonly bool _isNotEmpty; + + public bool IsEmpty { get { return !_isNotEmpty; } } + + public static readonly NanoId Empty; + + private NanoId(string value) + { + _value = value; + _isNotEmpty = true; + } + public override string ToString() + { + return _value; + } + + public static bool operator ==(NanoId left, NanoId right) + { + return (left.IsEmpty == right.IsEmpty && left._value == right._value); + } + public static bool operator !=(NanoId left, NanoId right) + { + return (left.IsEmpty != right.IsEmpty || left._value != right._value); + } + + public override bool Equals(object obj) + { + if (obj is NanoId) + { + return ((NanoId)obj == this); + } + return base.Equals(obj); + } + public override int GetHashCode() + { + if (IsEmpty) + return base.GetHashCode(); + return _value.GetHashCode(); + } + + /// + /// + /// + private class CryptoRandom : Random + { + private static RandomNumberGenerator _r; +#if !NETSTANDARD2_1 + private readonly byte[] _uint32Buffer = new byte[4]; +#endif + /// + /// + /// + public CryptoRandom() + { + _r = RandomNumberGenerator.Create(); + } + + /// + /// + /// + /// + /// + public override void NextBytes(byte[] buffer) + { + if (buffer == null) throw new ArgumentNullException(nameof(buffer)); + _r.GetBytes(buffer); + } + +#if NETSTANDARD2_1 + /// + public override void NextBytes(Span buffer) + { + RandomNumberGenerator.Fill(buffer); + } +#endif + + /// + /// + /// + /// + public override double NextDouble() + { +#if NETSTANDARD2_1 + Span uint32Buffer = stackalloc byte[4]; + RandomNumberGenerator.Fill(uint32Buffer); + return BitConverter.ToUInt32(uint32Buffer) / (1.0 + UInt32.MaxValue); +#else + _r.GetBytes(_uint32Buffer); + return BitConverter.ToUInt32(_uint32Buffer, 0) / (1.0 + UInt32.MaxValue); +#endif + } + /// + /// + /// + /// + /// + /// + /// + public override int Next(int minValue, int maxValue) + { + if (minValue > maxValue) throw new ArgumentOutOfRangeException(nameof(minValue)); + if (minValue == maxValue) return minValue; + var range = (long)maxValue - minValue; + return (int)((long)Math.Floor(NextDouble() * range) + minValue); + } + /// + /// + /// + /// + public override int Next() + { + return Next(0, int.MaxValue); + } + /// + /// + /// + /// + /// + /// + public override int Next(int maxValue) + { + if (maxValue < 0) throw new ArgumentOutOfRangeException(nameof(maxValue)); + return Next(0, maxValue); + } + } + + + public const string DefaultAlphabet = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + public const string DefaultAlphabetNoSpecialChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + private static readonly CryptoRandom Random = new CryptoRandom(); + /// + /// + /// + /// + /// + /// + // public static async Task GenerateAsync(string alphabet = DefaultAlphabet, int size = 21) => await Task.Run(() => Generate(Random, alphabet, size)); + + /// + /// + /// + /// + /// + /// + public static NanoId Generate(string alphabet = DefaultAlphabet, int size = 21) => Generate(Random, alphabet, size); + + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static NanoId Generate(Random random, string alphabet = DefaultAlphabet, int size = 21) + { + + if (random == null) + { + throw new ArgumentNullException("random cannot be null."); + } + + if (alphabet == null) + { + throw new ArgumentNullException("alphabet cannot be null."); + } + + if (alphabet.Length <= 0 || alphabet.Length >= 256) + { + throw new ArgumentOutOfRangeException("alphabet must contain between 1 and 255 symbols."); + } + + if (size <= 0) + { + throw new ArgumentOutOfRangeException("size must be greater than zero."); + } + + // See https://github.com/ai/nanoid/blob/master/format.js for + // explanation why masking is use (`random % alphabet` is a common + // mistake security-wise). + var mask = (2 << 31 - Clz32((alphabet.Length - 1) | 1)) - 1; + var step = (int)Math.Ceiling(1.6 * mask * size / alphabet.Length); + +#if NETSTANDARD2_1 + Span idBuilder = stackalloc char[size]; + Span bytes = stackalloc byte[step]; +#else + var idBuilder = new char[size]; + var bytes = new byte[step]; +#endif + + int cnt = 0; + + while (true) + { + + random.NextBytes(bytes); + + for (var i = 0; i < step; i++) + { + + var alphabetIndex = bytes[i] & mask; + + if (alphabetIndex >= alphabet.Length) continue; + idBuilder[cnt] = alphabet[alphabetIndex]; + if (++cnt == size) + { + return new NanoId(new string(idBuilder)); + } + + } + + } + + } + + /// + /// Counts leading zeros of . + /// + /// Input number. + /// Number of leading zeros. + /// + /// Courtesy of spender/Sunsetquest see https://stackoverflow.com/a/10439333/623392. + /// + internal static int Clz32(int x) + { + const int numIntBits = sizeof(int) * 8; //compile time constant + //do the smearing + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + //count the ones + x -= x >> 1 & 0x55555555; + x = (x >> 2 & 0x33333333) + (x & 0x33333333); + x = (x >> 4) + x & 0x0f0f0f0f; + x += x >> 8; + x += x >> 16; + return numIntBits - (x & 0x0000003f); //subtract # of 1s from 32 + } + } +}