-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.py
52 lines (37 loc) · 1.13 KB
/
sign.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import base64
import hashlib
import hmac
import time
import coding
import settings
"""Return codes for check_sig."""
OKAY = 0
TIMEOUT = 1
BAD = 2
def no_time_32(message):
sig = hmac.new(settings.message_key, message, hashlib.sha1)
return coding.b32enc(sig.digest())
def no_time(message):
sig = hmac.new(settings.message_key, message, hashlib.sha1)
return coding.b64enc(sig.digest())
def dns_safe(message):
sig = hmac.new(settings.message_key, message, hashlib.sha1)
return base64.b32encode(sig.digest()).lower()
def sign(message):
created = coding.urlenc_int(int(time.time()))
sig = no_time(message + created)
return created + "|" + sig
def check_sig(message, sig, timeout=False):
"""timeout is in seconds."""
if not sig or "|" not in sig:
return BAD
created, sig = sig.split("|", 1)
if not sig == no_time(message + created):
return BAD
if timeout:
now = time.time()
if now - timeout > coding.urldec_int(created):
return TIMEOUT
return OKAY
def list_size_exempt(list_name):
return list_name + "+big/" + no_time_32(list_name)[:10]