feat: add new generator key
All checks were successful
Build and Deploy Docker Container / build-and-deploy (push) Successful in 5m10s
.NET Test Pipeline / build-and-test (push) Successful in 5m43s

This commit is contained in:
Polianin Nikita 2024-07-05 23:14:45 +03:00
parent 3898463bc4
commit 80b46754ad
2 changed files with 20 additions and 1 deletions

View File

@ -79,7 +79,7 @@ public class Program
if (string.IsNullOrEmpty(secretForward!.SecretForwardToken)) if (string.IsNullOrEmpty(secretForward!.SecretForwardToken))
{ {
secretForward.SecretForwardToken = GeneratorKey.GenerateBase64(18); secretForward.SecretForwardToken = GeneratorKey.GenerateAlphaNumeric(16);
secretForward.SaveSetting(); secretForward.SaveSetting();
} }

View File

@ -1,11 +1,30 @@
using System; using System;
using System.Buffers.Text; using System.Buffers.Text;
using System.Linq;
using System.Text; using System.Text;
namespace Mirea.Api.Security.Services; namespace Mirea.Api.Security.Services;
public static class GeneratorKey public static class GeneratorKey
{ {
public static string GenerateAlphaNumeric(int size, string? excludes = null, string? includes = null)
{
var random = new Random();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
string charsForGenerate = excludes?
.Aggregate(chars, (current, ex) => current.Replace(ex.ToString(), string.Empty)) ?? chars;
if (!string.IsNullOrEmpty(includes))
charsForGenerate = includes
.Aggregate(charsForGenerate, (current, include) =>
current.Contains(include) ? current : current + include);
return new string(Enumerable.Repeat(charsForGenerate, size)
.Select(s => s[random.Next(s.Length)])
.ToArray());
}
public static ReadOnlySpan<byte> GenerateBytes(int size) public static ReadOnlySpan<byte> GenerateBytes(int size)
{ {
var key = new byte[size]; var key = new byte[size];