Skip to content

Commit 6eff27c

Browse files
authored
Merge pull request jsocol#282 from slick666/main
Adds RATELIMIT_HASH_ALGORITHM config
2 parents 7a6d04c + 3eb7d0a commit 6eff27c

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

django_ratelimit/core.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ def _make_cache_key(group, window, rate, value, methods):
140140
methods = ''.join(sorted([m.upper() for m in methods]))
141141
parts.append(methods)
142142
prefix = getattr(settings, 'RATELIMIT_CACHE_PREFIX', 'rl:')
143-
return prefix + hashlib.md5(''.join(parts).encode('utf-8')).hexdigest()
143+
attr = getattr(settings, 'RATELIMIT_HASH_ALGORITHM', hashlib.sha256)
144+
algo_cls = (import_string(f'{attr}')
145+
if isinstance(attr, str)
146+
else attr
147+
)
148+
return prefix + algo_cls(''.join(parts).encode('utf-8')).hexdigest()
144149

145150

146151
def is_ratelimited(request, group=None, fn=None, key=None, rate=None,

docs/settings.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ Settings
1010
An optional cache prefix for ratelimit keys (in addition to the ``PREFIX``
1111
value defined on the cache backend). Defaults to ``'rl:'``.
1212

13+
``RATELIMIT_HASH_ALGORITHM``
14+
-----------------------------
15+
16+
An optional functionion to overide the default hashing algorithm used to derive the cache
17+
key. Defaults to ``'hashlib.sha512'``.
18+
1319
``RATELIMIT_ENABLE``
1420
--------------------
1521

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ deps =
1919
django-redis>=5.2,<6.0
2020
flake8
2121

22+
allowlist_externals =
23+
*/run.sh
24+
2225
commands =
2326
./run.sh test {posargs}
2427
./run.sh flake8

0 commit comments

Comments
 (0)