1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from base64 import urlsafe_b64encode
9 from django.utils.hashcompat import sha_constructor
10 from django.conf import settings
13 # Use the system (hardware-based) random number generator if it exists.
14 if hasattr(random, 'SystemRandom'):
15 randrange = random.SystemRandom().randrange
17 randrange = random.randrange
18 MAX_SESSION_KEY = 18446744073709551616L # 2 << 63
21 def get_random_hash(seed):
22 sha_digest = sha_constructor('%s%s%s%s' %
23 (randrange(0, MAX_SESSION_KEY), time.time(), unicode(seed).encode('utf-8', 'replace'),
24 settings.SECRET_KEY)).digest()
25 return urlsafe_b64encode(sha_digest).replace('=', '').replace('_', '-').lower()
31 result.setdefault(tag.category, []).append(tag)