Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: sitescripts/signing.py

Issue 5177883412660224: Issue 2234 - Add a WSGI controller to collect email addresses for the Adblock Browser iOS launch (Closed)
Patch Set: Addressed comment Created April 23, 2015, 2:47 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
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')
Wladimir Palant 2015/04/23 16:04:40 No, it should be .get('submit_email', 'secret') -
Sebastian Noack 2015/04/23 16:29:41 Done.
+
+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
Wladimir Palant 2015/04/23 16:04:40 Do I get it correctly that you are trying to addre
Sebastian Noack 2015/04/23 16:29:41 Sure, that was the idea. But fair enough.
+
+def sign(data):
+ return hmac.new(_SECRET, data, hashlib.sha1).hexdigest()
+
+def verify(data, signature):
+ return constant_time_compare(sign(data), signature)

Powered by Google App Engine
This is Rietveld