-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using the external id generator since SQLite will do an error
- Loading branch information
Showing
2 changed files
with
35 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,43 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
using System; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Security.Cryptography; | ||
|
||
namespace BUTR.CrashReportServer.Services; | ||
|
||
public sealed class HexGenerator | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly RandomNumberGenerator _random; | ||
|
||
public HexGenerator(ILogger<HexGenerator> logger, RandomNumberGenerator random) | ||
public HexGenerator(RandomNumberGenerator random) | ||
{ | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
_random = random ?? throw new ArgumentNullException(nameof(random)); | ||
} | ||
|
||
private static int GetHexChars(in ReadOnlySpan<byte> source, Span<char> buffer) | ||
private static int GenerateHexChars(in ReadOnlySpan<byte> source, Span<char> destination) | ||
{ | ||
var idx1 = 0; | ||
var idx2 = 0; | ||
while (idx2 < source.Length) | ||
for (var i = 0; i < source.Length; i++) | ||
{ | ||
var num1 = (byte) ((uint) source[idx2] >> 4); | ||
buffer[idx1] = num1 > (byte) 9 ? (char) ((int) num1 + 55 + 32) : (char) ((int) num1 + 48); | ||
var num2 = (byte) ((uint) source[idx2] & 15U); | ||
int num3; | ||
buffer[num3 = idx1 + 1] = num2 > (byte) 9 ? (char) ((int) num2 + 55 + 32) : (char) ((int) num2 + 48); | ||
++idx2; | ||
idx1 = num3 + 1; | ||
var char1 = source[i] >> 4; | ||
destination[i * 2] = char.ToUpperInvariant(char1 > 9 ? (char) (char1 + 55 + 32) : (char) (char1 + 48)); | ||
var char2 = source[i] & 15; | ||
destination[i * 2 + 1] = char.ToUpperInvariant(char2 > 9 ? (char) (char2 + 55 + 32) : (char) (char2 + 48)); | ||
} | ||
return idx1; | ||
return source.Length * 2; | ||
} | ||
|
||
public string GetHex() | ||
public HashSet<string> GetHex(int maxCount, int length) | ||
{ | ||
Span<byte> input = stackalloc byte[3]; | ||
Span<char> output = stackalloc char[6]; | ||
var charLength = length * 2; | ||
var byteLength = length; | ||
|
||
Span<char> output = stackalloc char[maxCount * charLength]; | ||
Span<byte> input = stackalloc byte[maxCount * byteLength]; | ||
_random.GetBytes(input); | ||
GetHexChars(input, output); | ||
for (var i = 0; i < output.Length; i++) | ||
output[i] = char.ToUpper(output[i]); | ||
return output.ToString(); | ||
|
||
var unique = new HashSet<string>(maxCount); | ||
GenerateHexChars(input, output); | ||
for (var i = 0; i < output.Length; i += charLength) | ||
unique.Add(output.Slice(i, charLength).ToString()); | ||
return unique; | ||
} | ||
} |