Skip to content

Commit 87793a9

Browse files
committed
Saner random byte generation
1 parent a152660 commit 87793a9

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

bson/objectid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _raise_invalid_id(oid):
4242

4343
def _random_bytes():
4444
"""Get the 5-byte random field of an ObjectId."""
45-
return struct.pack(">Q", SystemRandom().randint(0, 0xFFFFFFFFFF))[3:]
45+
return os.urandom(5)
4646

4747

4848
class ObjectId(object):

pymongo/auth.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import functools
1818
import hashlib
1919
import hmac
20+
import os
2021
import socket
2122

2223
try:
@@ -38,7 +39,6 @@
3839

3940
from base64 import standard_b64decode, standard_b64encode
4041
from collections import namedtuple
41-
from random import SystemRandom
4242

4343
from bson.binary import Binary
4444
from bson.py3compat import string_type, _unicode, PY3
@@ -253,8 +253,7 @@ def _authenticate_scram(credentials, sock_info, mechanism):
253253
_hmac = hmac.HMAC
254254

255255
user = username.encode("utf-8").replace(b"=", b"=3D").replace(b",", b"=2C")
256-
nonce = standard_b64encode(
257-
(("%s" % (SystemRandom().random(),))[2:]).encode("utf-8"))
256+
nonce = standard_b64encode(os.urandom(32))
258257
first_bare = b"n=" + user + b",r=" + nonce
259258

260259
cmd = SON([('saslStart', 1),

0 commit comments

Comments
 (0)