Ticket #2249: hashes.py

File hashes.py, 1.1 KB (added by nikl@…, 9 years ago)

i had put this is in django/utils/hashes.py

Line 
1"""
2This module contains helper functions for dealing with hash algorithms.
3this was written to "uncouple" other modules from the actual algorithms used.
4can be changed through the settings variable FAVORITE_HASH_ALGO.
5
6currently used from the algorithms' interfaces:
7 * hash.new("some string").hexdigest()
8   returns a hexdigest for the given string, used in:
9   * django/contrib/sessions/models.py
10   * django/contrib/admin/views/decorators.py
11 * hash.digest_size
12   returns the digest_size (REMEMBER: hexdigest is twice as long), used in:
13   * django/contrib/sessions/models.py
14   * django/contrib/admin/views/decorators.py
15"""
16from django.conf import settings
17
18DEFAULT_HASH_ALGO="md5"
19KNOWN_HASH_ALGOS=["md5", "sha"]
20
21def _verify_hash_algo(algo):
22    """ TODO: the verification of the hash algos is _way_ too simple..
23    """
24    if not algo in KNOWN_HASH_ALGOS:
25        algo = DEFAULT_HASH_ALGO
26    return algo
27
28# TODO: a global variable is not really the nicest way :(
29_algo = _verify_hash_algo(settings.FAVORITE_HASH_ALGO)
30
31if _algo == "md5":
32    import md5 as hash
33elif _algo == "sha":
34    import sha as hash
Back to Top