MireaBackend/Security/Services/GeneratorKey.cs
Polianin Nikita 80b46754ad
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
feat: add new generator key
2024-07-05 23:14:45 +03:00

47 lines
1.6 KiB
C#

using System;
using System.Buffers.Text;
using System.Linq;
using System.Text;
namespace Mirea.Api.Security.Services;
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)
{
var key = new byte[size];
using var rng = System.Security.Cryptography.RandomNumberGenerator.Create();
rng.GetNonZeroBytes(key);
return key;
}
public static string GenerateBase64(int size) =>
Convert.ToBase64String(GenerateBytes(size));
public static string GenerateString(int size)
{
var randomBytes = GenerateBytes(size);
Span<byte> utf8Bytes = new byte[Base64.GetMaxEncodedToUtf8Length(randomBytes.Length)];
Base64.EncodeToUtf8(randomBytes, utf8Bytes, out _, out _);
return Encoding.UTF8.GetString(utf8Bytes);
}
}