1
2
3
4 """
5 This file is part of the web2py Web Framework
6 Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
7 License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
8 """
9
10 import hashlib
11 import hmac
12 import uuid
13 import random
14 import thread
15 import time
16
18 """ Generate a md5 hash with the given text """
19 return hashlib.md5(text).hexdigest()
20
22 """
23 Generates hash with the given text using the specified
24 digest hashing algorithm
25 """
26 if not digest_alg:
27 raise RuntimeError, "simple_hash with digest_alg=None"
28 elif not isinstance(digest_alg,str):
29 h = digest_alg(text)
30 else:
31 h = hashlib.new(digest_alg)
32 h.update(text)
33 return h.hexdigest()
34
36 """
37 Returns a hashlib digest algorithm from a string
38 """
39 if not isinstance(value,str):
40 return value
41 value = value.lower()
42 if value == "md5":
43 return hashlib.md5
44 elif value == "sha1":
45 return hashlib.sha1
46 elif value == "sha224":
47 return hashlib.sha224
48 elif value == "sha256":
49 return hashlib.sha256
50 elif value == "sha384":
51 return hashlib.sha384
52 elif value == "sha512":
53 return hashlib.sha512
54 else:
55 raise ValueError("Invalid digest algorithm")
56
57 -def hmac_hash(value, key, digest_alg='md5', salt=None):
58 if ':' in key:
59 digest_alg, key = key.split(':')
60 digest_alg = get_digest(digest_alg)
61 d = hmac.new(key,value,digest_alg)
62 if salt:
63 d.update(str(salt))
64 return d.hexdigest()
65
66 web2py_uuid_locker = thread.allocate_lock()
67 node_id = uuid.getnode()
68 milliseconds = int(time.time() * 1e3)
69
71 a = random.randrange(256)
72 b = (node_id >> 4*i) % 256
73 c = (milliseconds >> 4*i) % 256
74 return (a + b + c) % 256
75
83