"Fossies" - the Fresh Open Source Software Archive 
Member "pyzor-1.0.0/pyzor/account.py" (10 Dec 2014, 2546 Bytes) of package /linux/privat/pyzor-1.0.0.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "account.py" see the
Fossies "Dox" file reference documentation.
1 """A collection of utilities that facilitate working with Pyzor accounts.
2
3 Note that accounts are not necessary (on the client or server), as an
4 "anonymous" account always exists."""
5
6 import time
7 import hashlib
8
9 import pyzor
10
11
12 def sign_msg(hashed_key, timestamp, msg, hash_=hashlib.sha1):
13 """Converts the key, timestamp (epoch seconds), and msg into a digest.
14
15 lower(H(H(M) + ':' T + ':' + K))
16 M is message
17 T is integer epoch timestamp
18 K is hashed_key
19 H is the hash function (currently SHA1)
20 """
21 msg = msg.as_string().strip().encode("utf8")
22 digest = hash_()
23 digest.update(hash_(msg).digest())
24 digest.update((":%d:%s" % (timestamp, hashed_key)).encode("utf8"))
25 return digest.hexdigest().lower()
26
27
28 def hash_key(key, user, hash_=hashlib.sha1):
29 """Returns the hash key for this username and password.
30
31 lower(H(U + ':' + lower(K)))
32 K is key (hex string)
33 U is username
34 H is the hash function (currently SHA1)
35 """
36 result = ("%s:%s" % (user, key.lower())).encode("utf8")
37 return hash_(result).hexdigest().lower()
38
39
40 def verify_signature(msg, user_key):
41 """Verify that the provided message is correctly signed.
42
43 The message must have "User", "Time", and "Sig" headers.
44
45 If the signature is valid, then the function returns normally.
46 If the signature is not valid, then a pyzor.SignatureError() exception
47 is raised."""
48 timestamp = int(msg["Time"])
49 user = msg["User"]
50 provided_signature = msg["Sig"]
51 # Check that this signature is not too old.
52 if abs(time.time() - timestamp) > pyzor.MAX_TIMESTAMP_DIFFERENCE:
53 raise pyzor.SignatureError("Timestamp not within allowed range.")
54 # Calculate what the correct signature is.
55 hashed_user_key = hash_key(user_key, user)
56 # The signature is not part of the message that is signed.
57 del msg["Sig"]
58 correct_signature = sign_msg(hashed_user_key, timestamp, msg)
59 if correct_signature != provided_signature:
60 raise pyzor.SignatureError("Invalid signature.")
61
62
63 class Account(object):
64 def __init__(self, username, salt, key):
65 self.username = username
66 self.salt = salt
67 self.key = key
68
69
70 def key_from_hexstr(s):
71 try:
72 salt, key = s.split(",")
73 except ValueError:
74 raise ValueError("Invalid number of parts for key; perhaps you "
75 "forgot the comma at the beginning for the "
76 "salt divider?")
77 return salt, key
78
79 AnonymousAccount = Account(pyzor.anonymous_user, None, "")