Skip to content

Commit a44cd5a

Browse files
committed
fix: use secrets module to generate random bytes
1 parent a2ea7bb commit a44cd5a

4 files changed

Lines changed: 9 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77

88
`joserfc` is a Python library that provides a comprehensive implementation of several essential JSON Object Signing and Encryption (JOSE) standards.
99

10-
[![GitHub Sponsor](https://badgen.net/badge/support/joserfc/blue?icon=github)](https://github.com/sponsors/lepture)
1110
[![Build Status](https://github.com/authlib/joserfc/actions/workflows/test.yml/badge.svg)](https://github.com/authlib/joserfc/actions)
1211
[![PyPI Version](https://badgen.net/pypi/v/joserfc)](https://pypi.org/project/joserfc)
13-
[![PyPI Downloads](https://static.pepy.tech/personalized-badge/joserfc?period=month&units=international_system&left_color=black&right_color=brightgreen&left_text=downloads/month)](https://pepy.tech/projects/joserfc)
12+
[![PyPI Downloads](https://badgen.net/pypi/dm/joserfc)](https://pepy.tech/projects/joserfc)
1413
[![Code Coverage](https://codecov.io/gh/authlib/joserfc/branch/main/graph/badge.svg?token=WCD9X8HKI1)](https://codecov.io/gh/authlib/joserfc)
1514
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=authlib_joserfc&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=authlib_joserfc)
1615
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=authlib_joserfc&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=authlib_joserfc)

src/joserfc/rfc7516/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
2-
import os
32
import typing as t
3+
import secrets
44
from abc import ABCMeta, abstractmethod
55
from ..registry import Header, HeaderRegistryDict
66
from ..errors import InvalidKeyTypeError, InvalidKeyLengthError
@@ -168,10 +168,10 @@ class JWEEncModel(object, metaclass=ABCMeta):
168168
cek_size: int
169169

170170
def generate_cek(self) -> bytes:
171-
return os.urandom(self.cek_size // 8)
171+
return secrets.token_bytes(self.cek_size // 8)
172172

173173
def generate_iv(self) -> bytes:
174-
return os.urandom(self.iv_size // 8)
174+
return secrets.token_bytes(self.iv_size // 8)
175175

176176
def check_iv(self, iv: bytes) -> bytes:
177177
if len(iv) * 8 != self.iv_size: # pragma: no cover

src/joserfc/rfc7518/jwe_algs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import annotations
2-
from os import urandom
2+
import secrets
33
from cryptography.hazmat.primitives.asymmetric import padding
44
from cryptography.hazmat.primitives import hashes
55
from cryptography.hazmat.backends import default_backend
@@ -151,7 +151,7 @@ def encrypt_cek(self, cek: bytes, recipient: Recipient[OctKey]) -> bytes:
151151
#: The "iv" (initialization vector) Header Parameter value is the
152152
#: base64url-encoded representation of the 96-bit IV value
153153
iv_size = 96
154-
iv = urandom(iv_size // 8)
154+
iv = secrets.token_bytes(iv_size // 8)
155155

156156
cipher = Cipher(AES(op_key), GCM(iv), backend=default_backend())
157157
enc = cipher.encryptor()
@@ -264,7 +264,7 @@ def compute_derived_key(self, key: bytes, p2s: bytes, p2c: int) -> bytes:
264264
def encrypt_cek(self, cek: bytes, recipient: Recipient[OctKey]) -> bytes:
265265
headers = recipient.headers()
266266
if "p2s" not in headers:
267-
p2s = urandom(16)
267+
p2s = secrets.token_bytes(16)
268268
recipient.add_header("p2s", urlsafe_b64encode(p2s).decode("ascii"))
269269
else:
270270
p2s = urlsafe_b64decode(to_bytes(headers["p2s"]))

src/joserfc/rfc7518/oct_key.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22
from typing import Any
3-
from os import urandom
3+
import secrets
44
from ..util import (
55
to_bytes,
66
urlsafe_b64decode,
@@ -68,8 +68,7 @@ def generate_key(
6868
if key_size % 8 != 0:
6969
raise ValueError("Invalid bit size for oct key")
7070

71-
value = urandom(key_size // 8)
72-
raw_key = to_bytes(value)
71+
raw_key = secrets.token_bytes(key_size // 8)
7372
key: OctKey = cls(raw_key, raw_key, parameters)
7473
if auto_kid:
7574
key.ensure_kid()

0 commit comments

Comments
 (0)