changed method for generating something random

This commit is contained in:
Holger Börchers 2019-07-15 22:48:59 +02:00
parent a625f19d0d
commit e4ce2a472a
7 changed files with 57 additions and 60 deletions

28
.vscode/launch.json vendored
View File

@ -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",

23
.vscode/tasks.json vendored
View File

@ -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"
}
]
}

View File

@ -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);
}

View File

@ -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
/// </summary>
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>
/// Return cryptographic stable random integer.
/// Encode Base64 string
/// </summary>
/// <param name="min">Lower border of result.</param>
/// <param name="max">Upper border of result.</param>
/// <returns>Random integer.</returns>
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<byte> guidBytes = stackalloc byte[16];
Span<byte> 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;
}
/// <summary>
@ -46,7 +52,7 @@ namespace SmallInjectorDemo
/// <param name="memberName">member name</param>
/// <param name="filepath">file path</param>
/// <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);
return $"[{clock.CurrentDateTime}] {classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}";

View File

@ -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;
/// <summary>
/// Creates a new instance of <see cref="ServiceConsumer"/>.
@ -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));
}

View File

@ -9,7 +9,7 @@ namespace SmallInjectorDemo
public class ServiceOne : IServiceOne
{
private readonly IClock _clock;
private readonly int _id;
private readonly string _id;
/// <summary>
/// Creates a new instance of <see cref="ServiceOne"/>.
@ -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));
}

View File

@ -9,7 +9,7 @@ namespace SmallInjectorDemo
public class ServiceTwo : IServiceTwo
{
private readonly IClock _clock;
private readonly int _id;
private readonly string _id;
/// <summary>
/// Creates a new instance of <see cref="ServiceTwo"/>.
@ -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));
}