2

I have a list of ids and I want to display a fake id for each id in my list using Python.

I used the uuid1() but when there is a duplicated id in my list the program stop and don't generate the same random id for same input id.

 print uuid.uuid1(data['user']['id']).int>>64
3
  • It doesn't work for me. uuid4() is even worse it doesn't generate anything. Commented Oct 15, 2017 at 8:51
  • What is the specific purpose of .int>>64 ? Commented Oct 15, 2017 at 8:52
  • it generate an integer id and size less than 64 bits. Commented Oct 15, 2017 at 8:53

2 Answers 2

1

You could just hash your id :

import hashlib
id = 12
hashlib.sha256(str(id).encode()).hexdigest() # with python2.x you don't need to encode()
# => '6b51d431df5d7f141cbececcf79edf3dd861c3b4069f0b11661a3eefacbba918'

You'll have to store the correspondance somewhere though, because there is no way to retrieve the id from a hash.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help. This solution worked for me but it's too long. Do you know how to shorten or convert to integer?
you could use md5 instead of sha256, md5 are only 32 chars long. Check this : en.wikipedia.org/wiki/List_of_hash_functions
Thank you @Loïc, it's perfect now !
1

from python uuid doc, uuid1 is dependent on current time.

You could use uuid3 to have a reproducible uuid for the same input, by reusing the same namespace:

namespace = uuid.uuid4()
...
print uuid.uuid3(namespace, "1")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.