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 uuid
12 import random
13 import thread
14 import time
15
17 """ Generate a md5 hash with the given text """
18
19 return hashlib.md5(text).hexdigest()
20
21
22 -def hash(text, digest_alg = 'md5'):
23 """
24 Generates hash with the given text using the specified
25 digest hashing algorithm
26 """
27 if not isinstance(digest_alg,str):
28 h = digest_alg(text)
29 else:
30 h = hashlib.new(digest_alg)
31 h.update(text)
32 return h.hexdigest()
33
35 """
36 Returns a hashlib digest algorithm from a string
37 """
38 if not isinstance(value,str):
39 return value
40 value = value.lower()
41 if value == "md5":
42 return hashlib.md5
43 elif value == "sha1":
44 return hashlib.sha1
45 elif value == "sha224":
46 return hashlib.sha224
47 elif value == "sha256":
48 return hashlib.sha256
49 elif value == "sha384":
50 return hashlib.sha384
51 elif value == "sha512":
52 return hashlib.sha512
53 else:
54 raise ValueError("Invalid digest algorithm")
55
56 web2py_uuid_locker = thread.allocate_lock()
57 node_id = uuid.getnode()
58 milliseconds = int(time.time() * 1e3)
59
61 a = random.randrange(256)
62 b = (node_id >> 4*i) % 256
63 c = (milliseconds >> 4*i) % 256
64 return (a + b + c) % 256
65
73