Skip to content

Commit 916dd9e

Browse files
mweineltasherf
andauthored
Remove get_random_bytes from cryptography backend (#381)
* 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/ * remove unused import * fix import order --------- Co-authored-by: Asher Foa <[email protected]>
1 parent 675f4df commit 916dd9e

File tree

2 files changed

+2
-31
lines changed

2 files changed

+2
-31
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 & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from cryptography.exceptions import InvalidSignature, InvalidTag
55
from cryptography.hazmat.backends import default_backend
6-
from cryptography.hazmat.bindings.openssl.binding import Binding
76
from cryptography.hazmat.primitives import hashes, hmac, serialization
87
from cryptography.hazmat.primitives.asymmetric import ec, padding, rsa
98
from cryptography.hazmat.primitives.asymmetric.utils import decode_dss_signature, encode_dss_signature
@@ -25,34 +24,12 @@
2524
is_ssh_key,
2625
long_to_base64,
2726
)
27+
from . import get_random_bytes
2828
from .base import Key
2929

3030
_binding = None
3131

3232

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-
5633
class CryptographyECKey(Key):
5734
SHA256 = hashes.SHA256
5835
SHA384 = hashes.SHA384

0 commit comments

Comments
 (0)