Welcome, guest | Sign In | My Account | Store | Cart

A simple function that will generate a secure and unique session key.

Python, 6 lines
1
2
3
4
5
6
def generate_key(self, uid):
    import md5, os, base64, random
    m = md5.new()
    m.update(os.urandom(random.randint(15,25)))
    m.update(uid)
    return base64.standard_b64encode(m.digest())

3 comments

john stinson (author) 9 years, 9 months ago  # | flag

I'd love some comment on this. I think it does the job well but would like to know if there is any improvement to be made.

James Mills 9 years, 9 months ago  # | flag

What's wrong with:

from uuid import uuid4


key = uuid4().hexdigest()
James Mills 9 years, 9 months ago  # | flag

Sorry:

key.hex