0

I want to generate a collision checked, completely unique numeric id for each model. The reason I cannot use auto incrementing id is because I don't want ids to be predictable. I also wanna avoid using UUID, but I am fine with alphanumeric id as well. I am not replacing the primary key, but I am using it get objects in my API

Something like this:

8230693489356

Or:

a435Ga256hb35

But not:

ab66e4dd-4fee-4fb3-9519-efdc20e5d792

This is not a duplicate because:

  • The question refered does not solve my problem, it did not solve the askers question either, there is no accepted answer.

  • The top answer does not create a collision checked unique id, collisions can happen

  • Most of the answers suggest UUID, which is not what I want.

  • The question simply asks for a unique id, I am asking for something specific.

Sebastian Olsen
  • 10,318
  • 9
  • 46
  • 91
  • Possible duplicate of [How can I generate a unique ID in Python?](http://stackoverflow.com/questions/1210458/how-can-i-generate-a-unique-id-in-python) – sobolevn Apr 20 '16 at 08:48
  • @Sebastian Olsen You have find solution ? because i have answer for you ! – Mbambadev Apr 20 '16 at 09:31

1 Answers1

0

Since you wanted to to be unique Id and not just another random id

I've appended the Timestamp and hostname to a randomly generated Integer. Since you mentioned that it should not be easily guessable I've hashed above String with md5 to generate the final key.

import hashlib
import random
import time
import string
import socket
import sys
hostname = socket.gethostname()
def new_id(n):
    return hashlib.md5((str(random.randint(1,sys.maxsize))+hostname+str(time.time()*1000)).encode('utf-8')).hexdigest()[:n]

>>> new_id(11)
'8c05917c1c7'
Vishnu667
  • 768
  • 1
  • 16
  • 39
  • Are you 100% sure this is collision proof? What if someone posts at the exact same time? – Sebastian Olsen Apr 20 '16 at 11:35
  • @SebastianOlsen If someone posts at the same time from the same hostname. you have a random set of alphaNumeric characters. The possiblility of just the random keys is itself extremely unlikely When you combine it with the timestamp and the hostname. It should be pretty unique. – Vishnu667 Apr 20 '16 at 12:34
  • TypeError: Unicode-objects must be encoded before hashing – Sebastian Olsen Apr 20 '16 at 14:46
  • @SebastianOlsen I've updated the code now it works on both python 2.7 and 3 – Vishnu667 Apr 20 '16 at 15:37
  • So what does the `n` argument specify? If it does specify number of characters, that would be great. Having a relatively short but unique identifier is just what I am looking for. – Sebastian Olsen Apr 20 '16 at 15:41
  • n is the number of random alphanumeric words passed to the hash function The size of the output will depend on the hash function being used. `sha224` will generate a 56 letter string irrespective of the n parameter. More the value is set to n the more random the hash is going to be. – Vishnu667 Apr 20 '16 at 16:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109705/discussion-between-sebastian-olsen-and-vishnu667). – Sebastian Olsen Apr 20 '16 at 16:06