1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import sys
22
23
24
25
26
27
28
29 try:
30 _dev_urandom = open("/dev/urandom", "rb", 0)
31 except EnvironmentError:
32 _dev_urandom = None
33
34
35 try:
36 from Crypto.Util import winrandom
37 except ImportError:
38 winrandom = None
39
40
41
42 try:
43 from Crypto.Util.randpool import RandomPool
44 except ImportError:
45 RandomPool = None
46
47
48
49
50
51
59
60
62 - def __init__(self, numbytes=160, cipher=None, hash=None):
64
65 - def stir(self, s=''):
67
70
73
74
76 """RandomPool that uses the C{winrandom} module for input"""
77 - def __init__(self, numbytes=160, cipher=None, hash=None):
81
82 - def stir(self, s=''):
83 _workaround_windows_cryptgenrandom_bug(self)
84
85
87 """RandomPool that uses the C{/dev/urandom} special device node for input"""
88 - def __init__(self, numbytes=160, cipher=None, hash=None):
90
92 bytes = ""
93 while len(bytes) < n:
94 bytes += _dev_urandom.read(n - len(bytes))
95 return bytes
96
97
100 self._wr = RandomPool()
101 self.randomize()
102
105
106
107
108
109
110 osrandom_source = None
111
112
113 if osrandom_source is None and _dev_urandom is not None:
114 osrandom_source = "/dev/urandom"
115 DefaultRandomPoolClass = DevUrandomPool
116
117
118 if osrandom_source is None and winrandom is not None:
119 osrandom_source = "winrandom"
120 DefaultRandomPoolClass = WinRandomPool
121
122
123 if osrandom_source is None and RandomPool is not None:
124 osrandom_source = "randompool"
125 DefaultRandomPoolClass = FallbackRandomPool
126
127
128 if osrandom_source is None:
129 raise ImportError("Cannot find OS entropy source")
130
131
132
133
134
135
137 """RandomPool wrapper.
138
139 The C{randpool} attribute of this object may be modified by users of this class at runtime.
140 """
141
143 if instance is None:
144 instance = DefaultRandomPoolClass()
145 self.randpool = instance
146
147 - def stir(self, s=''):
149
152
155
158
159
160