Skip to content

Commit 04aa53f

Browse files
committed
Merge branch 'release/2.1.5'
2 parents 51a0828 + a134680 commit 04aa53f

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

threema/gateway/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
__author__ = 'Lennart Grahl <lennart.grahl@threema.ch>'
3737
__status__ = 'Production'
38-
__version__ = '2.1.4'
38+
__version__ = '2.1.5'
3939
__all__ = (
4040
'feature_level',
4141
'ReceptionCapability',

threema/gateway/e2e.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import abc
55
import enum
66
import os
7-
import random
87
import binascii
98
import struct
109
import mimetypes
@@ -16,7 +15,7 @@
1615
import libnacl.secret
1716
import libnacl.encode
1817

19-
from . import ReceptionCapability
18+
from . import ReceptionCapability, util
2019
from .exception import *
2120
from .key import Key
2221

@@ -45,7 +44,7 @@ def encrypt(private, public, data, nonce=None):
4544
data.
4645
"""
4746
# Generate 0 < padding < 256
48-
padding_length = random.randint(1, 255)
47+
padding_length = util.randint(1, 255)
4948

5049
# Add padding to the payload
5150
padding = bytes([padding_length] * padding_length)

threema/gateway/util.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
"""
44
import asyncio
55

6+
import libnacl
7+
68
from .key import Key
79

810
__all__ = (
911
'read_key_or_key_file',
1012
'raise_server_error',
13+
'randint',
1114
)
1215

1316

@@ -50,3 +53,13 @@ def raise_server_error(response, error):
5053
status = response.status
5154
yield from response.release()
5255
raise error(status)
56+
57+
58+
def randint(a, b):
59+
"""
60+
Return a cryptographically secure random integer N such that
61+
``a <= N <= b``.
62+
"""
63+
n = libnacl.randombytes_uniform(b) + a
64+
assert a <= n <= b
65+
return n

0 commit comments

Comments
 (0)