.NET Short Unique Identifier

前端 未结 23 1103
忘掉有多难
忘掉有多难 2020-12-07 11:27

I need a unique identifier in .NET (cannot use GUID as it is too long for this case).

Do people think that the algorithm used here is a good candidate or do you have

相关标签:
23条回答
  • 2020-12-07 12:21
    var ticks = new DateTime(2016,1,1).Ticks;
    var ans = DateTime.Now.Ticks - ticks;
    var uniqueId = ans.ToString("x");
    

    Keep a baseline date (which in this case is 1st Jan 2016) from when you will start generating these ids. This will make your ids smaller.

    Generated Number: 3af3c14996e54

    0 讨论(0)
  • 2020-12-07 12:21

    I know it's quite far from posted date... :)

    I have a generator which produces only 9 Hexa characters, eg: C9D6F7FF3, C9D6FB52C

    public class SlimHexIdGenerator : IIdGenerator
    {
        private readonly DateTime _baseDate = new DateTime(2016, 1, 1);
        private readonly IDictionary<long, IList<long>> _cache = new Dictionary<long, IList<long>>();
    
        public string NewId()
        {
            var now = DateTime.Now.ToString("HHmmssfff");
            var daysDiff = (DateTime.Today - _baseDate).Days;
            var current = long.Parse(string.Format("{0}{1}", daysDiff, now));
            return IdGeneratorHelper.NewId(_cache, current);
        }
    }
    
    
    static class IdGeneratorHelper
    {
        public static string NewId(IDictionary<long, IList<long>> cache, long current)
        {
            if (cache.Any() && cache.Keys.Max() < current)
            {
                cache.Clear();
            }
    
            if (!cache.Any())
            {
                cache.Add(current, new List<long>());
            }
    
            string secondPart;
            if (cache[current].Any())
            {
                var maxValue = cache[current].Max();
                cache[current].Add(maxValue + 1);
                secondPart = maxValue.ToString(CultureInfo.InvariantCulture);
            }
            else
            {
                cache[current].Add(0);
                secondPart = string.Empty;
            }
    
            var nextValueFormatted = string.Format("{0}{1}", current, secondPart);
            return UInt64.Parse(nextValueFormatted).ToString("X");
        }
    }
    
    0 讨论(0)
  • 2020-12-07 12:22

    As far as I know, just stripping off a part of a GUID isn't guaranteed to be unique - in fact, it's far from being unique.

    The shortest thing that I know that guarantees global uniqueness is featured in this blog post by Jeff Atwood. In the linked post, he discusses multiple ways to shorten a GUID, and in the end gets it down to 20 bytes via Ascii85 encoding.

    However, if you absolutely need a solution that's no longer than 15 bytes, I'm afraid you have no other choice than to use something which is not guaranteed to be globally unique.

    0 讨论(0)
  • 2020-12-07 12:22

    Just in case merely removing hyphens will do for anyone:

    Guid.NewGuid().ToString("n")
    

    This generates perfectly unique strings of 32 characters:

    5db4cee3bfd8436395d37fca2d48d5b3
    82fac271c76148a3a0667c00a5da990d
    
    0 讨论(0)
  • 2020-12-07 12:22

    you can use

    code = await UserManager.GenerateChangePhoneNumberTokenAsync(input.UserId, input.MobileNumber);
    

    its 6 nice characters only, 599527 ,143354

    and when user virify it simply

    var result = await UserManager.VerifyChangePhoneNumberTokenAsync(input.UserId, input.Token, input.MobileNumber);
    

    hope this help you

    0 讨论(0)
提交回复
热议问题