changed method for generating something random
This commit is contained in:
parent
a625f19d0d
commit
e4ce2a472a
28
.vscode/launch.json
vendored
28
.vscode/launch.json
vendored
@ -10,32 +10,12 @@
|
|||||||
"request": "launch",
|
"request": "launch",
|
||||||
"preLaunchTask": "build",
|
"preLaunchTask": "build",
|
||||||
// If you have changed target frameworks, make sure to update the program path.
|
// 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": [],
|
"args": [],
|
||||||
"cwd": "${workspaceFolder}/InjectorTest",
|
"cwd": "${workspaceFolder}/SmallInjectorDemo",
|
||||||
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
|
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
|
||||||
"console": "internalConsole",
|
"console": "internalConsole",
|
||||||
"stopAtEntry": false,
|
"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"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": ".NET Core Attach",
|
"name": ".NET Core Attach",
|
||||||
|
23
.vscode/tasks.json
vendored
23
.vscode/tasks.json
vendored
@ -7,19 +7,30 @@
|
|||||||
"type": "process",
|
"type": "process",
|
||||||
"args": [
|
"args": [
|
||||||
"build",
|
"build",
|
||||||
"${workspaceFolder}/InjectorTest/InjectorTest.csproj"
|
"${workspaceFolder}/SmallInjectorDemo/SmallInjectorDemo.csproj"
|
||||||
],
|
],
|
||||||
"problemMatcher": "$msCompile"
|
"problemMatcher": "$tsc"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "build",
|
"label": "publish",
|
||||||
"command": "dotnet",
|
"command": "dotnet",
|
||||||
"type": "process",
|
"type": "process",
|
||||||
"args": [
|
"args": [
|
||||||
"build",
|
"publish",
|
||||||
"${workspaceFolder}/InjectorTest/InjectorTest.csproj"
|
"${workspaceFolder}/SmallInjectorDemo/SmallInjectorDemo.csproj"
|
||||||
],
|
],
|
||||||
"problemMatcher": "$msCompile"
|
"problemMatcher": "$tsc"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "watch",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"watch",
|
||||||
|
"run",
|
||||||
|
"${workspaceFolder}/SmallInjectorDemo/SmallInjectorDemo.csproj"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$tsc"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
@ -8,8 +8,8 @@ namespace InjectorTest
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void Test()
|
public void Test()
|
||||||
{
|
{
|
||||||
const string expected = "[-] HelperTest.Test Id: 0";
|
const string expected = "[-] HelperTest.Test Id: ";
|
||||||
var actual = Helper.WriteMethodString(new TestClock(), 0);
|
var actual = Helper.WriteMethodString(new TestClock(), "");
|
||||||
Assert.Equal(expected, actual);
|
Assert.Equal(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Buffers.Text;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Security.Cryptography;
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace SmallInjectorDemo
|
namespace SmallInjectorDemo
|
||||||
{
|
{
|
||||||
@ -10,32 +12,36 @@ namespace SmallInjectorDemo
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Helper
|
public static class Helper
|
||||||
{
|
{
|
||||||
|
private const byte ForwardSlashByte = (byte)'/';
|
||||||
|
private const byte DashByte = (byte)'-';
|
||||||
|
private const byte PlusByte = (byte)'+';
|
||||||
|
private const byte UnderscoreByte = (byte)'_';
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Return cryptographic stable random integer.
|
/// Encode Base64 string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="min">Lower border of result.</param>
|
public static string EncodeBase64String(this Guid guid)
|
||||||
/// <param name="max">Upper border of result.</param>
|
|
||||||
/// <returns>Random integer.</returns>
|
|
||||||
public static int NewRandomInteger(int min, int max)
|
|
||||||
{
|
{
|
||||||
// The random number provider.
|
Span<byte> guidBytes = stackalloc byte[16];
|
||||||
using (var rand = new RNGCryptoServiceProvider())
|
Span<byte> encodedBytes = stackalloc byte[24];
|
||||||
{
|
|
||||||
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.
|
MemoryMarshal.TryWrite(guidBytes, ref guid); // write bytes from the Guid
|
||||||
scale = BitConverter.ToUInt32(fourBytes, 0);
|
Base64.EncodeToUtf8(guidBytes, encodedBytes, out _, out _);
|
||||||
|
|
||||||
|
// replace any characters which are not URL safe
|
||||||
|
for (var i = 0; i < 22; i++)
|
||||||
|
{
|
||||||
|
if (encodedBytes[i] == ForwardSlashByte)
|
||||||
|
encodedBytes[i] = DashByte;
|
||||||
|
|
||||||
|
if (encodedBytes[i] == PlusByte)
|
||||||
|
encodedBytes[i] = UnderscoreByte;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add min to the scaled difference between max and min.
|
// skip the last two bytes as these will be '==' padding
|
||||||
return (int)(min + (max - min) * (scale / (double)uint.MaxValue));
|
var final = Encoding.UTF8.GetString(encodedBytes.Slice(0, 22));
|
||||||
|
|
||||||
}
|
return final;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -46,7 +52,7 @@ namespace SmallInjectorDemo
|
|||||||
/// <param name="memberName">member name</param>
|
/// <param name="memberName">member name</param>
|
||||||
/// <param name="filepath">file path</param>
|
/// <param name="filepath">file path</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
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);
|
var classname = Path.GetFileNameWithoutExtension(filepath);
|
||||||
return $"[{clock.CurrentDateTime}] {classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}";
|
return $"[{clock.CurrentDateTime}] {classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}";
|
||||||
|
@ -10,7 +10,7 @@ namespace SmallInjectorDemo
|
|||||||
private readonly IClock _clock;
|
private readonly IClock _clock;
|
||||||
private readonly IServiceOne _service1;
|
private readonly IServiceOne _service1;
|
||||||
private readonly IServiceTwo _service2;
|
private readonly IServiceTwo _service2;
|
||||||
private readonly int _id;
|
private readonly string _id;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of <see cref="ServiceConsumer"/>.
|
/// Creates a new instance of <see cref="ServiceConsumer"/>.
|
||||||
@ -23,7 +23,7 @@ namespace SmallInjectorDemo
|
|||||||
_clock = clock;
|
_clock = clock;
|
||||||
_service1 = service1;
|
_service1 = service1;
|
||||||
_service2 = service2;
|
_service2 = service2;
|
||||||
_id = Helper.NewRandomInteger(10, 99);
|
_id = Guid.NewGuid().EncodeBase64String();
|
||||||
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ namespace SmallInjectorDemo
|
|||||||
public class ServiceOne : IServiceOne
|
public class ServiceOne : IServiceOne
|
||||||
{
|
{
|
||||||
private readonly IClock _clock;
|
private readonly IClock _clock;
|
||||||
private readonly int _id;
|
private readonly string _id;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of <see cref="ServiceOne"/>.
|
/// Creates a new instance of <see cref="ServiceOne"/>.
|
||||||
@ -17,7 +17,7 @@ namespace SmallInjectorDemo
|
|||||||
public ServiceOne(IClock clock)
|
public ServiceOne(IClock clock)
|
||||||
{
|
{
|
||||||
_clock = clock;
|
_clock = clock;
|
||||||
_id = Helper.NewRandomInteger(10, 99);
|
_id = Guid.NewGuid().EncodeBase64String();
|
||||||
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ namespace SmallInjectorDemo
|
|||||||
public class ServiceTwo : IServiceTwo
|
public class ServiceTwo : IServiceTwo
|
||||||
{
|
{
|
||||||
private readonly IClock _clock;
|
private readonly IClock _clock;
|
||||||
private readonly int _id;
|
private readonly string _id;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of <see cref="ServiceTwo"/>.
|
/// Creates a new instance of <see cref="ServiceTwo"/>.
|
||||||
@ -17,7 +17,7 @@ namespace SmallInjectorDemo
|
|||||||
public ServiceTwo(IClock clock)
|
public ServiceTwo(IClock clock)
|
||||||
{
|
{
|
||||||
_clock = clock;
|
_clock = clock;
|
||||||
_id = Helper.NewRandomInteger(10, 99);
|
_id = Guid.NewGuid().EncodeBase64String();
|
||||||
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user