Skip to content

Linux's /dev/urandom and Python's os.urandom does not pass  #10

Closed

Description

@phmngcthanh

import os print( bin(int.from_bytes(os.urandom(1024),'big')))
does not pass Maurer test (09) and approx. Entropy test (12)
image

print( bin(int.from_bytes(open('/dev/urandom','rb').read(1024),'big'))) does not pass Maurer test (09) and randomly, does not pass approx. Entropy test (12) too

image
image

Activity

  1. phmngcthanh commented on Nov 26, 2022

    @phmngcthanh
    Author

    I tried on Python 3.6.9 (default, Jun 29 2022, 11:45:57) [GCC 8.4.0] on linux, using Jetson Nano's Linux
    I wonder, does it imply that both incompliance to NIST, or something else

  2. ThexXTURBOXx commented on Jun 28, 2023

    @ThexXTURBOXx
    Contributor

    Maurer's Test is only applicable for bit strings of length >= 387840.
    As for the approximate entropy test, I think that with enough data (at least 10^6 or something), it might actually pass.
    If your bit string is not long enough, the tests might "randomly" fail.

    For even more reference, see section 4.3: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-22r1a.pdf

  3. stevenang commented on Apr 5, 2026

    @stevenang
    Owner

    This is expected behaviour — os.urandom(1024) produces only 8,192 bits, well below the 387,840-bit minimum required by Maurer's Universal test. The test was silently returning (-1.0, False) which was confusing. This has been fixed in PR #33: the test now returns a clear error message instead. The Approximate Entropy result with short inputs may also be statistically unreliable — addressed by the minimum-length guards added in PR #34. Closing as addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions