mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
Patch #934711: Expose platform-specific entropy.
This commit is contained in:
parent
6ab78cd0c0
commit
dc3883f671
5 changed files with 126 additions and 1 deletions
21
Lib/os.py
21
Lib/os.py
|
@ -656,3 +656,24 @@ try:
|
|||
_make_statvfs_result)
|
||||
except NameError: # statvfs_result may not exist
|
||||
pass
|
||||
|
||||
if not _exists("urandom"):
|
||||
_urandomfd = None
|
||||
def urandom(n):
|
||||
"""urandom(n) -> str
|
||||
|
||||
Return a string of n random bytes suitable for cryptographic use.
|
||||
|
||||
"""
|
||||
global _urandomfd
|
||||
if not _urandomfd:
|
||||
try:
|
||||
_urandomfd = open("/dev/urandom", O_RDONLY)
|
||||
except:
|
||||
_urandomfd = NotImplementedError
|
||||
if _urandomfd is NotImplementedError:
|
||||
raise NotImplementedError("/dev/urandom (or equivalent) not found")
|
||||
bytes = ""
|
||||
while len(bytes) < n:
|
||||
bytes += read(_urandomfd, n - len(bytes))
|
||||
return bytes
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue