Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

randbytes.py 3.4KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # -*- test-case-name: twisted.test.test_randbytes -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Cryptographically secure random implementation, with fallback on normal random.
  6. """
  7. import os
  8. import random
  9. import warnings
  10. getrandbits = getattr(random, "getrandbits", None)
  11. _fromhex = bytes.fromhex
  12. class SecureRandomNotAvailable(RuntimeError):
  13. """
  14. Exception raised when no secure random algorithm is found.
  15. """
  16. class SourceNotAvailable(RuntimeError):
  17. """
  18. Internal exception used when a specific random source is not available.
  19. """
  20. class RandomFactory:
  21. """
  22. Factory providing L{secureRandom} and L{insecureRandom} methods.
  23. You shouldn't have to instantiate this class, use the module level
  24. functions instead: it is an implementation detail and could be removed or
  25. changed arbitrarily.
  26. """
  27. # This variable is no longer used, and will eventually be removed.
  28. randomSources = ()
  29. getrandbits = getrandbits
  30. def _osUrandom(self, nbytes):
  31. """
  32. Wrapper around C{os.urandom} that cleanly manage its absence.
  33. """
  34. try:
  35. return os.urandom(nbytes)
  36. except (AttributeError, NotImplementedError) as e:
  37. raise SourceNotAvailable(e)
  38. def secureRandom(self, nbytes, fallback=False):
  39. """
  40. Return a number of secure random bytes.
  41. @param nbytes: number of bytes to generate.
  42. @type nbytes: C{int}
  43. @param fallback: Whether the function should fallback on non-secure
  44. random or not. Default to C{False}.
  45. @type fallback: C{bool}
  46. @return: a string of random bytes.
  47. @rtype: C{str}
  48. """
  49. try:
  50. return self._osUrandom(nbytes)
  51. except SourceNotAvailable:
  52. pass
  53. if fallback:
  54. warnings.warn(
  55. "urandom unavailable - "
  56. "proceeding with non-cryptographically secure random source",
  57. category=RuntimeWarning,
  58. stacklevel=2,
  59. )
  60. return self.insecureRandom(nbytes)
  61. else:
  62. raise SecureRandomNotAvailable("No secure random source available")
  63. def _randBits(self, nbytes):
  64. """
  65. Wrapper around C{os.getrandbits}.
  66. """
  67. if self.getrandbits is not None:
  68. n = self.getrandbits(nbytes * 8)
  69. hexBytes = ("%%0%dx" % (nbytes * 2)) % n
  70. return _fromhex(hexBytes)
  71. raise SourceNotAvailable("random.getrandbits is not available")
  72. _maketrans = bytes.maketrans
  73. _BYTES = _maketrans(b"", b"")
  74. def _randModule(self, nbytes):
  75. """
  76. Wrapper around the C{random} module.
  77. """
  78. return b"".join([bytes([random.choice(self._BYTES)]) for i in range(nbytes)])
  79. def insecureRandom(self, nbytes):
  80. """
  81. Return a number of non secure random bytes.
  82. @param nbytes: number of bytes to generate.
  83. @type nbytes: C{int}
  84. @return: a string of random bytes.
  85. @rtype: C{str}
  86. """
  87. for src in ("_randBits", "_randModule"):
  88. try:
  89. return getattr(self, src)(nbytes)
  90. except SourceNotAvailable:
  91. pass
  92. factory = RandomFactory()
  93. secureRandom = factory.secureRandom
  94. insecureRandom = factory.insecureRandom
  95. del factory
  96. __all__ = ["secureRandom", "insecureRandom", "SecureRandomNotAvailable"]