Files
mixtape/zero.Core/Utils/IdGenerator.cs
T

50 lines
1.0 KiB
C#
Raw Normal View History

2020-05-07 11:45:28 +02:00
using System;
2021-06-15 15:33:19 +02:00
using System.Linq;
2020-05-07 11:45:28 +02:00
namespace zero.Core.Utils
{
public class IdGenerator
{
2021-06-15 15:33:19 +02:00
private static Random random = new();
2021-03-18 13:58:26 +01:00
public static string Classic()
{
return Guid.NewGuid().ToString();
}
2021-06-15 15:33:19 +02:00
/// <summary>
/// Create a new random Id
/// </summary>
public static string CreateRandom(int length)
{
const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
}
2020-05-07 11:45:28 +02:00
/// <summary>
/// Create a new unique Id
/// </summary>
public static string Create(int length = -1)
{
2021-03-17 14:49:10 +01:00
if (length < 1)
2020-05-07 11:45:28 +02:00
{
2021-03-17 14:49:10 +01:00
length = 12;
}
2021-06-15 15:33:19 +02:00
2021-03-17 14:49:10 +01:00
//if (length > 0)
//{
2020-05-07 11:45:28 +02:00
return Convert.ToBase64String(Guid.NewGuid().ToByteArray())
.Replace("/", String.Empty)
.Replace("+", String.Empty)
.Replace("-", String.Empty)
.ToLowerInvariant()
.Substring(0, length);
2021-03-17 14:49:10 +01:00
//}
2020-05-07 11:45:28 +02:00
2021-03-17 14:49:10 +01:00
//return Guid.NewGuid().ToString();
2020-05-07 11:45:28 +02:00
}
}
}