randpool.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. __revision__ = "$Id$"
  27. from Crypto.pct_warnings import RandomPool_DeprecationWarning
  28. import Crypto.Random
  29. import warnings
  30. class RandomPool:
  31. """Deprecated. Use Random.new() instead.
  32. See http://www.pycrypto.org/randpool-broken
  33. """
  34. def __init__(self, numbytes = 160, cipher=None, hash=None, file=None):
  35. warnings.warn("This application uses RandomPool, which is BROKEN in older releases. See http://www.pycrypto.org/randpool-broken",
  36. RandomPool_DeprecationWarning)
  37. self.__rng = Crypto.Random.new()
  38. self.bytes = numbytes
  39. self.bits = self.bytes * 8
  40. self.entropy = self.bits
  41. def get_bytes(self, N):
  42. return self.__rng.read(N)
  43. def _updateEntropyEstimate(self, nbits):
  44. self.entropy += nbits
  45. if self.entropy < 0:
  46. self.entropy = 0
  47. elif self.entropy > self.bits:
  48. self.entropy = self.bits
  49. def _randomize(self, N=0, devname="/dev/urandom"):
  50. """Dummy _randomize() function"""
  51. self.__rng.flush()
  52. def randomize(self, N=0):
  53. """Dummy randomize() function"""
  54. self.__rng.flush()
  55. def stir(self, s=''):
  56. """Dummy stir() function"""
  57. self.__rng.flush()
  58. def stir_n(self, N=3):
  59. """Dummy stir_n() function"""
  60. self.__rng.flush()
  61. def add_event(self, s=''):
  62. """Dummy add_event() function"""
  63. self.__rng.flush()
  64. def getBytes(self, N):
  65. """Dummy getBytes() function"""
  66. return self.get_bytes(N)
  67. def addEvent(self, event, s=""):
  68. """Dummy addEvent() function"""
  69. return self.add_event()