Index: sitescripts/signing.py |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/sitescripts/signing.py |
@@ -0,0 +1,17 @@ |
+import hmac |
+import hashlib |
+ |
+from sitescripts.utils import get_config |
+ |
+_SECRET = get_config().get('DEFAULT', 'secret') |
+ |
+def constant_time_compare(s1, s2): |
+ if len(s1) != len(s2): |
+ return False |
+ return reduce(lambda a, b: a | b, (ord(c1) ^ ord(c2) for c1, c2 in zip(s1, s2))) == 0 |
+ |
+def sign(data): |
+ return hmac.new(_SECRET, data, hashlib.sha1).hexdigest() |
+ |
+def verify(data, signature): |
+ return constant_time_compare(sign(data), signature) |