feat: add new generator key
This commit is contained in:
parent
3898463bc4
commit
80b46754ad
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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];
|
||||||
|
Loading…
Reference in New Issue
Block a user