<s> import base64 <EOL> import hashlib <EOL> import hmac <EOL> import httplib <EOL> import os <EOL> import time <EOL> import urlparse <EOL> def generate_ext ( content_type , body ) : <EOL> """<STR_LIT>""" <EOL> if content_type is not None and body is not None and len ( content_type ) > <NUM_LIT:0> and len ( body ) > <NUM_LIT:0> : <EOL> content_type_plus_body = content_type + body <EOL> content_type_plus_body_hash = hashlib . sha1 ( content_type_plus_body ) <EOL> ext = content_type_plus_body_hash . hexdigest ( ) <EOL> else : <EOL> ext = "<STR_LIT>" <EOL> return ext <EOL> def build_normalized_request_string ( ts , nonce , http_method , host , port , request_path , ext ) : <EOL> """<STR_LIT>""" <EOL> normalized_request_string = ts + '<STR_LIT:\n>' + nonce + '<STR_LIT:\n>' + http_method + '<STR_LIT:\n>' + request_path + '<STR_LIT:\n>' + host + '<STR_LIT:\n>' + str ( port ) + '<STR_LIT:\n>' + ext + '<STR_LIT:\n>' <EOL> return normalized_request_string <EOL> def generate_nonce ( ) : <EOL> """<STR_LIT>""" <EOL> return base64 . b64encode ( os . urandom ( <NUM_LIT:8> ) ) <EOL> def generate_signature ( mac_key , normalized_request_string ) : <EOL> """<STR_LIT>""" <EOL> mac_key += '<STR_LIT:=>' * ( <NUM_LIT:4> - len ( mac_key ) % <NUM_LIT:4> ) <EOL> mac_key = base64 . urlsafe_b64decode ( mac_key ) <EOL> return base64 . b64encode ( <EOL> hmac . new ( mac_key , normalized_request_string , hashlib . sha1 ) . digest ( ) ) <EOL> def generate_authorization_header_value ( <EOL> http_method , <EOL> url , <EOL> mac_key_identifier , <EOL> mac_key , <EOL> content_type , <EOL> body ) : <EOL> url_parts = urlparse . urlparse ( url ) <EOL> port = url_parts . port <EOL> if not port : <EOL> if url_parts . scheme == '<STR_LIT>' : <EOL> port = str ( httplib . HTTPS_PORT ) <EOL> else : <EOL> port = str ( httplib . HTTP_PORT ) <EOL> ts = str ( int ( time . time ( ) ) ) <EOL> nonce = generate_nonce ( ) <EOL> ext = generate_ext ( content_type , body ) <EOL> normalized_request_string = build_normalized_request_string ( <EOL> ts , <EOL> nonce , <EOL> http_method , <EOL> url_parts . hostname , <EOL> port , <EOL> url_parts . path , <EOL> ext ) <EOL> signature = generate_signature ( mac_key , normalized_request_string ) <EOL> return '<STR_LIT>' % ( <EOL> mac_key_identifier , <EOL> ts , <EOL> nonce , <EOL> ext , <EOL> signature ) </s>
