Skip to content

Commit 752abd2

Browse files
committed
Remove get_random_bytes from cryptography backend
The RAND_bytes binding has been removed in cryptography 45.0. The recommendation[1] is now to rely on `os.urandom`, which is already implemented in the native backend. The pycrpto implementation was removed earlier, so this removes the leftover attempt to import it. Closes: #380 [1] https://cryptography.io/en/latest/random-numbers/
1 parent 675f4df commit 752abd2

File tree

2 files changed

+2
-30
lines changed

2 files changed

+2
-30
lines changed

jose/backends/__init__.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
try:
2-
from jose.backends.cryptography_backend import get_random_bytes # noqa: F401
3-
except ImportError:
4-
try:
5-
from jose.backends.pycrypto_backend import get_random_bytes # noqa: F401
6-
except ImportError:
7-
from jose.backends.native import get_random_bytes # noqa: F401
1+
from jose.backends.native import get_random_bytes # noqa: F401
82

93
try:
104
from jose.backends.cryptography_backend import CryptographyRSAKey as RSAKey # noqa: F401

jose/backends/cryptography_backend.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,11 @@
2626
long_to_base64,
2727
)
2828
from .base import Key
29+
from . import get_random_bytes
2930

3031
_binding = None
3132

3233

33-
def get_random_bytes(num_bytes):
34-
"""
35-
Get random bytes
36-
37-
Currently, Cryptography returns OS random bytes. If you want OpenSSL
38-
generated random bytes, you'll have to switch the RAND engine after
39-
initializing the OpenSSL backend
40-
Args:
41-
num_bytes (int): Number of random bytes to generate and return
42-
Returns:
43-
bytes: Random bytes
44-
"""
45-
global _binding
46-
47-
if _binding is None:
48-
_binding = Binding()
49-
50-
buf = _binding.ffi.new("char[]", num_bytes)
51-
_binding.lib.RAND_bytes(buf, num_bytes)
52-
rand_bytes = _binding.ffi.buffer(buf, num_bytes)[:]
53-
return rand_bytes
54-
55-
5634
class CryptographyECKey(Key):
5735
SHA256 = hashes.SHA256
5836
SHA384 = hashes.SHA384

0 commit comments

Comments
 (0)