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 142155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:53:29+00:00 2026-05-11T07:53:29+00:00

I am using an api which takes a name of 21 char max to

  • 0

I am using an api which takes a name of 21 char max to represent an internal session which has a lifetime of around ‘two days’. I would like the name not to be meaningfull using some kind of hasing ? md5 generates 40 chars, is there something else i could use ?

For now i use ‘userid[:10]’ + creation time: ddhhmmss + random 3 chars.

Thanks,

  • 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. 2026-05-11T07:53:30+00:00Added an answer on May 11, 2026 at 7:53 am

    If I read your question correctly, you want to generate some arbitrary identifier token which must be 21 characters max. Does it need to be highly resistant to guessing? The example you gave isn’t ‘crytographically strong’ in that it can be guessed by searching well less than 1/2 of the entire possible keyspace.

    You don’t say if the characters can be all 256 ASCII characters, or if it needs to be limited to, say, printable ASCII (33-127, inclusive), or some smaller range.

    There is a Python module designed for UUIDs (Universals Unique IDentifiers). You likely want uuid4 which generates a random UUID, and uses OS support if available (on Linux, Mac, FreeBSD, and likely others).

    >>> import uuid >>> u = uuid.uuid4() >>> u UUID('d94303e7-1be4-49ef-92f2-472bc4b4286d') >>> u.bytes '\xd9C\x03\xe7\x1b\xe4I\xef\x92\xf2G+\xc4\xb4(m' >>> len(u.bytes) 16 >>>  

    16 random bytes is very unguessable, and there’s no need to use the full 21 bytes your API allows, if all you want is to have an unguessable opaque identifier.

    If you can’t use raw bytes like that, which is probably a bad idea because it’s harder to use in logs and other debug messages and harder to compare by eye, then convert the bytes into something a bit more readable, like using base-64 encoding, with the result chopped down to 21 (or whatever) bytes:

    >>> u.bytes.encode('base64') '2UMD5xvkSe+S8kcrxLQobQ==\n' >>> len(u.bytes.encode('base64'))  25 >>> u.bytes.encode('base64')[:21] '2UMD5xvkSe+S8kcrxLQob' >>>  

    This gives you an extremely high quality random string of length 21.

    You might not like the ‘+’ or ‘/’ which can be in a base-64 string, since without proper escaping that might interfere with URLs. Since you already think to use ‘random 3 chars’, I don’t think this is a worry of yours. If it is, you could replace those characters with something else (‘-‘ and ‘.’ might work), or remove them if present.

    As others have pointed out, you could use .encode(‘hex’) and get the hex equivalent, but that’s only 4 bits of randomness/character * 21 characters max gives you 84 bits of randomness instead of twice that. Every bit doubles your keyspace, making the theoretical search space much, much smaller. By a factor of 2E24 smaller.

    Your keyspace is still 2E24 in size, even with hex encoding, so I think it’s more a theoretical concern. I wouldn’t worry about people doing brute force attacks against your system.

    Edit:

    P.S.: The uuid.uuid4 function uses libuuid if available. That gets its entropy from os.urandom (if available) otherwise from the current time and the local ethernet MAC address. If libuuid is not available then the uuid.uuid4 function gets the bytes directly from os.urandom (if available) otherwise it uses the random module. The random module uses a default seed based on os.urandom (if available) otherwise a value based on the current time. Probing takes place for every function call, so if you don’t have os.urandom then the overhead is a bit bigger than you might expect.

    Take home message? If you know you have os.urandom then you could do

    os.urandom(16).encode('base64')[:21] 

    but if you don’t want to worry about its availability then use the uuid module.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a REST api(using Restler) which takes in username and password
I'm using a third-party API ( CryptEncrypt , to be precise) which takes a
I am using an API which provides a Java version but not a Python
I am using google data api which gives date in datetime format. I want
Assuming I'm using some graphic API which allows me to draw bezier curves by
I am using a third party API which performs what I would assume are
I have a web site with an API which publishes the information using JSON.
I am connecting to an API service which authenticates users using cookies. I make
I'm using Flickr API: flickr.photos.search in my app and couldn't find any argument which
I have a process in c++ in which I am using window API. I

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.