summaryrefslogtreecommitdiffstats
path: root/Lib/os.py
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2004-08-29 15:46:35 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2004-08-29 15:46:35 (GMT)
commitdc3883f671893fe338ecbd25e84316728c6c48b9 (patch)
tree1c6a79019126d16b7c96c3e37354e131a529e235 /Lib/os.py
parent6ab78cd0c055462af4f5a6c59b9f310a83734c45 (diff)
downloadcpython-dc3883f671893fe338ecbd25e84316728c6c48b9.zip
cpython-dc3883f671893fe338ecbd25e84316728c6c48b9.tar.gz
cpython-dc3883f671893fe338ecbd25e84316728c6c48b9.tar.bz2
Patch #934711: Expose platform-specific entropy.
Diffstat (limited to 'Lib/os.py')
-rw-r--r--Lib/os.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/Lib/os.py b/Lib/os.py
index 5b79981..86b3566 100644
--- a/Lib/os.py
+++ b/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