Created
February 20, 2014 16:44
-
-
Save imduffy15/9118082 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# encoding: utf-8 | |
import hmac | |
import hashlib | |
from uuid import uuid1 as uuid | |
from base64 import b64encode | |
from urllib import urlencode | |
from functools import wraps | |
def main(): | |
secretkey = 'ExampleSecretKey' | |
data = { | |
'SignatureVersion': '2', | |
'AWSAccessKeyId': 'ExampleAPIKey', | |
'Version': '2013-10-15', | |
'Timestamp': '2014-02-19T23:34:43.868347', | |
'SignatureMethod': 'HmacSHA256', | |
'KeyName': 'Test', | |
'Action': 'DeleteKeyPair' | |
} | |
request_string = _get_request_string(data) | |
print _generate_signature(secretkey, request_string) | |
def _generate_signature(secretkey, request_string): | |
signature = hmac.new( | |
key=secretkey, | |
msg=bytes(request_string), | |
digestmod=hashlib.sha256 | |
).digest() | |
signature = b64encode(signature) | |
return signature | |
def _get_request_string(data): | |
query_string = _get_query_string(data) | |
request_string = '\n'.join( | |
['POST', 'localhost', '/', query_string] | |
) | |
return request_string.encode('utf-8') | |
def _get_query_string(data): | |
params = {} | |
for param in data: | |
if param != 'Signature': | |
params[param] = data[param] | |
keys = sorted(params.keys()) | |
values = map(params.get, keys) | |
query_string = urlencode( | |
list( | |
zip(keys, values) | |
) | |
) | |
return query_string | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment