feat: added feature flag to enable blake2b hashing

This commit is contained in:
Muhammad Anas
2024-04-19 07:22:14 +00:00
committed by Peter Pinch
parent a31ed929f7
commit aea7fcee45
4 changed files with 56 additions and 4 deletions

View File

@@ -7,6 +7,7 @@ so that we can cache any keys, not just ones that memcache would ordinarily acce
import hashlib
from urllib.parse import quote_plus
from django.conf import settings
from django.utils.encoding import smart_str
@@ -14,9 +15,12 @@ def fasthash(string):
"""
Hashes `string` into a string representation of a 128-bit digest.
"""
md4 = hashlib.new("md4")
md4.update(string.encode('utf-8'))
return md4.hexdigest()
if settings.FEATURES.get("ENABLE_BLAKE2B_HASHING", False):
hash_obj = hashlib.new("blake2b", digest_size=16)
else:
hash_obj = hashlib.new("md4")
hash_obj.update(string.encode('utf-8'))
return hash_obj.hexdigest()
def cleaned_string(val):

View File

@@ -3,11 +3,15 @@ Tests for memcache in util app
"""
from django.conf import settings
from django.core.cache import caches
from django.test import TestCase
from django.test import TestCase, override_settings
from common.djangoapps.util.memcache import safe_key
BLAKE2B_ENABLED_FEATURES = settings.FEATURES.copy()
BLAKE2B_ENABLED_FEATURES["ENABLE_BLAKE2B_HASHING"] = True
class MemcacheTest(TestCase):
"""
@@ -51,6 +55,20 @@ class MemcacheTest(TestCase):
# The key should now be valid
assert self._is_valid_key(key), f'Failed for key length {length}'
@override_settings(FEATURES=BLAKE2B_ENABLED_FEATURES)
def test_safe_key_long_with_blake2b_enabled(self):
# Choose lengths close to memcached's cutoff (250)
for length in [248, 249, 250, 251, 252]:
# Generate a key of that length
key = 'a' * length
# Make the key safe
key = safe_key(key, '', '')
# The key should now be valid
assert self._is_valid_key(key), f'Failed for key length {length}'
def test_long_key_prefix_version(self):
# Long key
@@ -65,6 +83,21 @@ class MemcacheTest(TestCase):
key = safe_key('key', 'prefix', 'a' * 300)
assert self._is_valid_key(key)
@override_settings(FEATURES=BLAKE2B_ENABLED_FEATURES)
def test_long_key_prefix_version_with_blake2b_enabled(self):
# Long key
key = safe_key('a' * 300, 'prefix', 'version')
assert self._is_valid_key(key)
# Long prefix
key = safe_key('key', 'a' * 300, 'version')
assert self._is_valid_key(key)
# Long version
key = safe_key('key', 'prefix', 'a' * 300)
assert self._is_valid_key(key)
def test_safe_key_unicode(self):
for unicode_char in self.UNICODE_CHAR_CODES: