Created
May 31, 2013 03:36
-
-
Save najeira/5682804 to your computer and use it in GitHub Desktop.
verify Amazon SNS message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def verify(message): | |
# the string to sign | |
sign_str = [] | |
for key in ("Message", "MessageId", "Subject", "Timestamp", "TopicArn", "Type"): | |
if key == "Subject" and key not in message: | |
continue | |
sign_str.append(key) | |
sign_str.append(message[key]) | |
sign_str.append("") | |
sign_str = "¥n".join(sign_str) | |
# certificate file | |
from Crypto.PublicKey import RSA | |
cert = open('aws_sns.pem').read() | |
rkey = RSA.importKey(cert) | |
# verify | |
from Crypto.Hash import SHA | |
from Crypto.Signature import PKCS1_v1_5 | |
signature = base64.standard_b64decode(message["Signature"]) | |
h = SHA.new(sign_str) | |
p = PKCS1_v1_5.new(rkey) | |
valid = p.verify(h, signature) | |
return valid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment