Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rand.randbytes() instead of os.urandom() #493

Open
socketpair opened this issue Nov 2, 2023 · 4 comments
Open

Use rand.randbytes() instead of os.urandom() #493

socketpair opened this issue Nov 2, 2023 · 4 comments

Comments

@socketpair
Copy link

[mmarkk@asus home]$ python -m timeit -s 'import random' 'random.randbytes(8)'
5000000 loops, best of 5: 93.9 nsec per loop
[mmarkk@asus home]$ python -m timeit -s 'import os' 'os.urandom(8)'
1000000 loops, best of 5: 248 nsec per loop
@Zaczero
Copy link

Zaczero commented Nov 12, 2023

randbytes is not cryptographically secure
see https://docs.python.org/3/library/random.html#random.randbytes
vs https://docs.python.org/3/library/os.html#os.urandom

@socketpair
Copy link
Author

socketpair commented Nov 12, 2023

@Zaczero but ulid is not about security/cryptography, right ? But speed is important.

@Zaczero
Copy link

Zaczero commented Nov 12, 2023

Depends on the implementation, this specific one relies on cryptographically secure randomness: https://github.com/ahawker/ulid#components. ULID spec also recommends doing so: https://github.com/ulid/spec#components.

@Zaczero
Copy link

Zaczero commented Nov 12, 2023

If you don't care about unpredictability, maybe consider using the thread lock feature - https://github.com/ahawker/ulid#thread-lock - it removes the randomness for quick sequential id generations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants