Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 797169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:45:35+00:00 2026-05-14T22:45:35+00:00

I would like to generate a long UUID – something like the session key

  • 0

I would like to generate a long UUID – something like the session key used by gmail. It should be at least 256 chars and no more than 512. It can contain all alpha-numeric chars and a few special chars (the ones below the function keys on the keyboard). Has this been done already or is there a sample out there?

C++ or C#

Update: A GUID is not enough. We already have been seeing collisions and need to remedy this. 512 is the max as of now because it will prevent us from changing stuff that was already shipped.

Update 2: For the guys who are insisting about how unique the GUID is, if someone wants to guess your next session ID, they don’t have to compute the combinations for the next 1 trillion years. All they have to do is use constrain the time factor and they will be done in hours.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:45:36+00:00Added an answer on May 14, 2026 at 10:45 pm

    As per your update2 you are correct on Guids are predicable even the msdn references that. here is a method that uses a crptographicly strong random number generator to create the ID.

    static long counter; //store and load the counter from persistent storage every time the program loads or closes.
    
    public static string CreateRandomString(int length)
    {
        long count = System.Threading.Interlocked.Increment(ref counter);
        int PasswordLength = length;
        String _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ23456789";
        Byte[] randomBytes = new Byte[PasswordLength];
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        rng.GetBytes(randomBytes);
        char[] chars = new char[PasswordLength];
        int allowedCharCount = _allowedChars.Length;
        for (int i = 0; i < PasswordLength; i++)
        {
            while(randomBytes[i] > byte.MaxValue - (byte.MaxValue % allowedCharCount))
            {
                byte[] tmp = new byte[1];
                rng.GetBytes(tmp);
                randomBytes[i] = tmp[0];
            }
            chars[i] = _allowedChars[(int)randomBytes[i] % allowedCharCount];
        }
        byte[] buf = new byte[8];
        buf[0] = (byte) count;
        buf[1] = (byte) (count >> 8);
        buf[2] = (byte) (count >> 16);
        buf[3] = (byte) (count >> 24);
        buf[4] = (byte) (count >> 32);
        buf[5] = (byte) (count >> 40);
        buf[6] = (byte) (count >> 48);
        buf[7] = (byte) (count >> 56);
        return Convert.ToBase64String(buf) + new string(chars);
    }
    

    EDIT I know there is some biasing because allowedCharCount is not evenly divisible by 255, you can get rid of the bias throwing away and getting a new random number if it lands in the no-mans-land of the remainder.

    EDIT2 – This is not guaranteed to be unique, you could hold a static 64 bit(or higher if necessary) monotonic counter encode it to base46 and have that be the first 4-5 characters of the id.

    UPDATE – Now guaranteed to be unique

    UPDATE 2: Algorithm is now slower but removed biasing.

    EDIT: I just ran a test, I wanted to let you know that ToBase64String can return non alphnumeric charaters (like 1 encodes to "AQAAAAAAAAA=") just so you are aware.

    New Version:

    Taking from Matt Dotson’s answer on this page, if you are no so worried about the keyspace you can do it this way and it will run a LOT faster.

    public static string CreateRandomString(int length)
    {
        length -= 12; //12 digits are the counter
        if (length <= 0)
            throw new ArgumentOutOfRangeException("length");
        long count = System.Threading.Interlocked.Increment(ref counter);
        Byte[] randomBytes = new Byte[length * 3 / 4];
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        rng.GetBytes(randomBytes);
    
        byte[] buf = new byte[8];
        buf[0] = (byte)count;
        buf[1] = (byte)(count >> 8);
        buf[2] = (byte)(count >> 16);
        buf[3] = (byte)(count >> 24);
        buf[4] = (byte)(count >> 32);
        buf[5] = (byte)(count >> 40);
        buf[6] = (byte)(count >> 48);
        buf[7] = (byte)(count >> 56);
        return Convert.ToBase64String(buf) + Convert.ToBase64String(randomBytes);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.