From e4ce2a472adc70b0931d63897ce87a9782fdc4de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Mon, 15 Jul 2019 22:48:59 +0200 Subject: [PATCH] changed method for generating something random --- .vscode/launch.json | 28 +++------------- .vscode/tasks.json | 23 +++++++++---- InjectorTest/HelperTest.cs | 4 +-- SmallInjectorDemo/Helper.cs | 50 ++++++++++++++++------------ SmallInjectorDemo/ServiceConsumer.cs | 4 +-- SmallInjectorDemo/ServiceOne.cs | 4 +-- SmallInjectorDemo/ServiceTwo.cs | 4 +-- 7 files changed, 57 insertions(+), 60 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index c478252..3291fe9 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,32 +10,12 @@ "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/InjectorTest/bin/Debug/netcoreapp2.1/InjectorTest.dll", + "program": "${workspaceFolder}/SmallInjectorDemo/bin/Debug/netcoreapp2.2/SmallInjectorDemo.dll", "args": [], - "cwd": "${workspaceFolder}/InjectorTest", - // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window + "cwd": "${workspaceFolder}/SmallInjectorDemo", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console "console": "internalConsole", - "stopAtEntry": false, - "internalConsoleOptions": "openOnSessionStart" - }, - { - "name": ".NET Core Attach", - "type": "coreclr", - "request": "attach", - "processId": "${command:pickProcess}" - } - , - { - "name": ".NET Core Launch (console)", - "type": "coreclr", - "request": "launch", - "preLaunchTask": "build", - "program": "${workspaceFolder}/InjectorTest/bin/Debug/netcoreapp2.1/InjectorTest.dll", - "args": [], - "cwd": "${workspaceFolder}/InjectorTest", - "console": "internalConsole", - "stopAtEntry": false, - "internalConsoleOptions": "openOnSessionStart" + "stopAtEntry": false }, { "name": ".NET Core Attach", diff --git a/.vscode/tasks.json b/.vscode/tasks.json index cb9d711..d2ae2e9 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -7,19 +7,30 @@ "type": "process", "args": [ "build", - "${workspaceFolder}/InjectorTest/InjectorTest.csproj" + "${workspaceFolder}/SmallInjectorDemo/SmallInjectorDemo.csproj" ], - "problemMatcher": "$msCompile" + "problemMatcher": "$tsc" }, { - "label": "build", + "label": "publish", "command": "dotnet", "type": "process", "args": [ - "build", - "${workspaceFolder}/InjectorTest/InjectorTest.csproj" + "publish", + "${workspaceFolder}/SmallInjectorDemo/SmallInjectorDemo.csproj" ], - "problemMatcher": "$msCompile" + "problemMatcher": "$tsc" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/SmallInjectorDemo/SmallInjectorDemo.csproj" + ], + "problemMatcher": "$tsc" } ] } \ No newline at end of file diff --git a/InjectorTest/HelperTest.cs b/InjectorTest/HelperTest.cs index 49c64bf..4f524e2 100644 --- a/InjectorTest/HelperTest.cs +++ b/InjectorTest/HelperTest.cs @@ -8,8 +8,8 @@ namespace InjectorTest [Fact] public void Test() { - const string expected = "[-] HelperTest.Test Id: 0"; - var actual = Helper.WriteMethodString(new TestClock(), 0); + const string expected = "[-] HelperTest.Test Id: "; + var actual = Helper.WriteMethodString(new TestClock(), ""); Assert.Equal(expected, actual); } diff --git a/SmallInjectorDemo/Helper.cs b/SmallInjectorDemo/Helper.cs index 92f02a8..972d853 100644 --- a/SmallInjectorDemo/Helper.cs +++ b/SmallInjectorDemo/Helper.cs @@ -1,7 +1,9 @@ using System; +using System.Buffers.Text; using System.IO; using System.Runtime.CompilerServices; -using System.Security.Cryptography; +using System.Runtime.InteropServices; +using System.Text; namespace SmallInjectorDemo { @@ -10,32 +12,36 @@ namespace SmallInjectorDemo /// public static class Helper { + private const byte ForwardSlashByte = (byte)'/'; + private const byte DashByte = (byte)'-'; + private const byte PlusByte = (byte)'+'; + private const byte UnderscoreByte = (byte)'_'; + /// - /// Return cryptographic stable random integer. + /// Encode Base64 string /// - /// Lower border of result. - /// Upper border of result. - /// Random integer. - public static int NewRandomInteger(int min, int max) + public static string EncodeBase64String(this Guid guid) { - // The random number provider. - using (var rand = new RNGCryptoServiceProvider()) + Span guidBytes = stackalloc byte[16]; + Span encodedBytes = stackalloc byte[24]; + + MemoryMarshal.TryWrite(guidBytes, ref guid); // write bytes from the Guid + Base64.EncodeToUtf8(guidBytes, encodedBytes, out _, out _); + + // replace any characters which are not URL safe + for (var i = 0; i < 22; i++) { - var scale = uint.MaxValue; - while (scale == uint.MaxValue) - { - // Get four random bytes. - var fourBytes = new byte[4]; - rand.GetBytes(fourBytes); - - // Convert that into an uint. - scale = BitConverter.ToUInt32(fourBytes, 0); - } - - // Add min to the scaled difference between max and min. - return (int)(min + (max - min) * (scale / (double)uint.MaxValue)); + if (encodedBytes[i] == ForwardSlashByte) + encodedBytes[i] = DashByte; + if (encodedBytes[i] == PlusByte) + encodedBytes[i] = UnderscoreByte; } + + // skip the last two bytes as these will be '==' padding + var final = Encoding.UTF8.GetString(encodedBytes.Slice(0, 22)); + + return final; } /// @@ -46,7 +52,7 @@ namespace SmallInjectorDemo /// member name /// file path /// - public static string WriteMethodString(IClock clock, int id, [CallerMemberName] string memberName = null, [CallerFilePath] string filepath = null) + public static string WriteMethodString(IClock clock, string id, [CallerMemberName] string memberName = null, [CallerFilePath] string filepath = null) { var classname = Path.GetFileNameWithoutExtension(filepath); return $"[{clock.CurrentDateTime}] {classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}"; diff --git a/SmallInjectorDemo/ServiceConsumer.cs b/SmallInjectorDemo/ServiceConsumer.cs index fdced72..35cfdb1 100644 --- a/SmallInjectorDemo/ServiceConsumer.cs +++ b/SmallInjectorDemo/ServiceConsumer.cs @@ -10,7 +10,7 @@ namespace SmallInjectorDemo private readonly IClock _clock; private readonly IServiceOne _service1; private readonly IServiceTwo _service2; - private readonly int _id; + private readonly string _id; /// /// Creates a new instance of . @@ -23,7 +23,7 @@ namespace SmallInjectorDemo _clock = clock; _service1 = service1; _service2 = service2; - _id = Helper.NewRandomInteger(10, 99); + _id = Guid.NewGuid().EncodeBase64String(); Console.WriteLine(Helper.WriteMethodString(clock, _id)); } diff --git a/SmallInjectorDemo/ServiceOne.cs b/SmallInjectorDemo/ServiceOne.cs index 1ae6dc9..15b42de 100644 --- a/SmallInjectorDemo/ServiceOne.cs +++ b/SmallInjectorDemo/ServiceOne.cs @@ -9,7 +9,7 @@ namespace SmallInjectorDemo public class ServiceOne : IServiceOne { private readonly IClock _clock; - private readonly int _id; + private readonly string _id; /// /// Creates a new instance of . @@ -17,7 +17,7 @@ namespace SmallInjectorDemo public ServiceOne(IClock clock) { _clock = clock; - _id = Helper.NewRandomInteger(10, 99); + _id = Guid.NewGuid().EncodeBase64String(); Console.WriteLine(Helper.WriteMethodString(clock, _id)); } diff --git a/SmallInjectorDemo/ServiceTwo.cs b/SmallInjectorDemo/ServiceTwo.cs index 56ac88e..5a1ed03 100644 --- a/SmallInjectorDemo/ServiceTwo.cs +++ b/SmallInjectorDemo/ServiceTwo.cs @@ -9,7 +9,7 @@ namespace SmallInjectorDemo public class ServiceTwo : IServiceTwo { private readonly IClock _clock; - private readonly int _id; + private readonly string _id; /// /// Creates a new instance of . @@ -17,7 +17,7 @@ namespace SmallInjectorDemo public ServiceTwo(IClock clock) { _clock = clock; - _id = Helper.NewRandomInteger(10, 99); + _id = Guid.NewGuid().EncodeBase64String(); Console.WriteLine(Helper.WriteMethodString(clock, _id)); }