OLD | NEW |
(Empty) | |
| 1 # |
| 2 # randpool.py : Cryptographically strong random number generation |
| 3 # |
| 4 # Part of the Python Cryptography Toolkit |
| 5 # |
| 6 # Written by Andrew M. Kuchling, Mark Moraes, and others |
| 7 # |
| 8 # =================================================================== |
| 9 # The contents of this file are dedicated to the public domain. To |
| 10 # the extent that dedication to the public domain is not available, |
| 11 # everyone is granted a worldwide, perpetual, royalty-free, |
| 12 # non-exclusive license to exercise all rights associated with the |
| 13 # contents of this file for any purpose whatsoever. |
| 14 # No rights are reserved. |
| 15 # |
| 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 # SOFTWARE. |
| 24 # =================================================================== |
| 25 # |
| 26 |
| 27 __revision__ = "$Id$" |
| 28 |
| 29 from Crypto.pct_warnings import RandomPool_DeprecationWarning |
| 30 import Crypto.Random |
| 31 import warnings |
| 32 |
| 33 class RandomPool: |
| 34 """Deprecated. Use Random.new() instead. |
| 35 |
| 36 See http://www.pycrypto.org/randpool-broken |
| 37 """ |
| 38 def __init__(self, numbytes = 160, cipher=None, hash=None, file=None): |
| 39 warnings.warn("This application uses RandomPool, which is BROKEN in olde
r releases. See http://www.pycrypto.org/randpool-broken", |
| 40 RandomPool_DeprecationWarning) |
| 41 self.__rng = Crypto.Random.new() |
| 42 self.bytes = numbytes |
| 43 self.bits = self.bytes * 8 |
| 44 self.entropy = self.bits |
| 45 |
| 46 def get_bytes(self, N): |
| 47 return self.__rng.read(N) |
| 48 |
| 49 def _updateEntropyEstimate(self, nbits): |
| 50 self.entropy += nbits |
| 51 if self.entropy < 0: |
| 52 self.entropy = 0 |
| 53 elif self.entropy > self.bits: |
| 54 self.entropy = self.bits |
| 55 |
| 56 def _randomize(self, N=0, devname="/dev/urandom"): |
| 57 """Dummy _randomize() function""" |
| 58 self.__rng.flush() |
| 59 |
| 60 def randomize(self, N=0): |
| 61 """Dummy randomize() function""" |
| 62 self.__rng.flush() |
| 63 |
| 64 def stir(self, s=''): |
| 65 """Dummy stir() function""" |
| 66 self.__rng.flush() |
| 67 |
| 68 def stir_n(self, N=3): |
| 69 """Dummy stir_n() function""" |
| 70 self.__rng.flush() |
| 71 |
| 72 def add_event(self, s=''): |
| 73 """Dummy add_event() function""" |
| 74 self.__rng.flush() |
| 75 |
| 76 def getBytes(self, N): |
| 77 """Dummy getBytes() function""" |
| 78 return self.get_bytes(N) |
| 79 |
| 80 def addEvent(self, event, s=""): |
| 81 """Dummy addEvent() function""" |
| 82 return self.add_event() |
OLD | NEW |