Skip to content

Instantly share code, notes, and snippets.

@najeira
Created May 31, 2013 03:36
Show Gist options
  • Save najeira/5682804 to your computer and use it in GitHub Desktop.
Save najeira/5682804 to your computer and use it in GitHub Desktop.
verify Amazon SNS message
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