~tribaal/txaws/xss-hardening

« back to all changes in this revision

Viewing changes to txaws/util.py

  • Committer: Tristan Seligmann
  • Date: 2009-04-27 23:53:32 UTC
  • mfrom: (3.1.5 client)
  • Revision ID: mithrandi@mithrandi.net-20090427235332-l8azkl1kvn5pjtay
mergeĀ lp:~lifeless/txaws/client

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Generally useful utilities for AWS web services not specific to a service.
 
2
 
 
3
New things in this module should be of relevance to more than one of amazon's
 
4
services.
 
5
"""
 
6
 
 
7
__all__ = ['hmac_sha1', 'iso8601time']
 
8
 
 
9
from base64 import b64encode
 
10
from hashlib import sha1
 
11
import hmac
 
12
import time
 
13
# Import XML from somwhere; here in one place to prevent duplication.
 
14
try:
 
15
    from xml.etree.ElementTree import XML
 
16
except ImportError:
 
17
    from elementtree.ElementTree import XML
 
18
 
 
19
def hmac_sha1(secret, data):
 
20
    digest = hmac.new(secret, data, sha1).digest()
 
21
    return b64encode(digest)
 
22
 
 
23
 
 
24
def iso8601time(time_tuple):
 
25
    """Format time_tuple as a ISO8601 time string.
 
26
 
 
27
    :param time_tuple: Either None, to use the current time, or a tuple tuple.
 
28
    """
 
29
    if time_tuple:
 
30
        return time.strftime("%Y-%m-%dT%H:%M:%SZ", time_tuple)
 
31
    else:
 
32
        return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())