using System.Text.Json; namespace zero.Persistence; public class IdGenerator { const string CHARS = "abcdefghijklmnopqrstuvwxyz0123456789"; const string CHARS_COMPLEX = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-@#.:!?*"; private static Random random = new(); /// /// Create a new unique Id /// public static string Create(int length = -1, Charset charset = Charset.az09) { if (length < 1) { length = 12; } string chars = charset == Charset.az09 ? CHARS : CHARS_COMPLEX; return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray()); //if (length > 0) //{ //return Convert.ToBase64String(Guid.NewGuid().ToByteArray()) // .Replace("/", String.Empty) // .Replace("+", String.Empty) // .Replace("-", String.Empty) // .ToLowerInvariant() // .Substring(0, length); //} //return Guid.NewGuid().ToString(); } /// /// Creates a simple hash from a string /// public static string HashString(string value) { return GetStableHashCode(value).ToString().Replace("-", String.Empty); } /// /// Creates a simple hash from a string /// public static string HashObject(params object[] values) { return GetStableHashCode(JsonSerializer.Serialize(values)).ToString().Replace("-", String.Empty); } /// /// Autofill IDs on an object with [GenerateId] attributes /// public static T Autofill(T model) { // find all Raven Ids List> ravenIds = ObjectTraverser.FindAttribute(model); // set unset Raven Ids foreach (ObjectTraverser.Result item in ravenIds) { string id = item.Property.GetValue(item.Parent, null) as string; if (id.IsNullOrWhiteSpace()) { id = item.Item.Length.HasValue ? Create(item.Item.Length.Value) : Create(); item.Property.SetValue(item.Parent, id); } } return model; } static int GetStableHashCode(string str) { unchecked { int hash1 = 5381; int hash2 = hash1; for (int i = 0; i < str.Length && str[i] != '\0'; i += 2) { hash1 = ((hash1 << 5) + hash1) ^ str[i]; if (i == str.Length - 1 || str[i + 1] == '\0') break; hash2 = ((hash2 << 5) + hash2) ^ str[i + 1]; } return hash1 + (hash2 * 1566083941); } } public enum Charset { /// /// a-z, 0-9 /// az09 = 0, /// /// a-z, A-Z, 0-9, _-@#.:!?* /// azAZ09x = 1 } }