<s> from __future__ import absolute_import , division , print_function , with_statement <EOL> import hashlib <EOL> import hmac <EOL> import re <EOL> try : <EOL> from urllib import urlencode <EOL> except ImportError : <EOL> from urllib . parse import urlencode <EOL> try : <EOL> import urlparse <EOL> except ImportError : <EOL> import urllib . parse as urlparse <EOL> def derive_signature ( key , qs ) : <EOL> """<STR_LIT>""" <EOL> key , qs = ( key or "<STR_LIT>" , qs or "<STR_LIT>" ) <EOL> return hmac . new ( key . encode ( ) , qs . encode ( ) , hashlib . sha1 ) . hexdigest ( ) <EOL> def sign ( key , qs ) : <EOL> """<STR_LIT>""" <EOL> sig = derive_signature ( key , qs ) <EOL> return "<STR_LIT>" % ( qs , urlencode ( [ ( "<STR_LIT>" , sig ) ] ) ) <EOL> def verify_signature ( key , qs ) : <EOL> """<STR_LIT>""" <EOL> unsigned_qs = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , qs ) <EOL> sig = derive_signature ( key , unsigned_qs ) <EOL> return urlparse . parse_qs ( qs ) . get ( "<STR_LIT>" , [ None ] ) [ <NUM_LIT:0> ] == sig <EOL> def main ( ) : <EOL> import sys <EOL> import tornado . options <EOL> from tornado . options import define , options , parse_command_line <EOL> define ( "<STR_LIT:key>" , help = "<STR_LIT>" , type = str ) <EOL> args = parse_command_line ( ) <EOL> if not options . key : <EOL> tornado . options . print_help ( ) <EOL> sys . exit ( ) <EOL> qs = args [ <NUM_LIT:0> ] <EOL> if qs and qs [ <NUM_LIT:0> ] == "<STR_LIT:?>" : <EOL> print ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> print ( "<STR_LIT>" % qs ) <EOL> print ( "<STR_LIT>" % derive_signature ( options . key , qs ) ) <EOL> print ( "<STR_LIT>" % sign ( options . key , qs ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
