Skip to content
Advertisement

JavaScript crypto.randomBytes(8) to Python Code

I am working with Python in an API. The API is coded using Javascript, and I do not know how would be the equivalent code of this line in Python:

crypto.randomBytes(8).join("")

I found out this link https://docs.python.org/3/library/uuid.html, but I do not know what to do to get exactly the same result.

Thank you for your help

Advertisement

Answer

Try:

from random import randint
bytes([randint(0, 255) for _ in range(8)])

EDIT: If you need secure random bytes:

from secrets import token_bytes
token_bytes(8)

docs

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement