<s> from string import join <EOL> from array import array <EOL> import hashlib <EOL> from time import time <EOL> for typecode in '<STR_LIT>' : <EOL> if len ( array ( typecode , [ <NUM_LIT:0> ] ) . tostring ( ) ) == <NUM_LIT:4> : <EOL> uint32 = typecode <EOL> break <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> class CryptError ( Exception ) : pass <EOL> def _hash ( str ) : return hashlib . sha1 ( str ) . digest ( ) <EOL> _ivlen = <NUM_LIT:16> <EOL> _maclen = <NUM_LIT:8> <EOL> _state = _hash ( ` time ( ) ` ) <EOL> try : <EOL> import os <EOL> _pid = ` os . getpid ( ) ` <EOL> except ImportError , AttributeError : <EOL> _pid = '<STR_LIT>' <EOL> def _expand_key ( key , clen ) : <EOL> blocks = ( clen + <NUM_LIT> ) / <NUM_LIT:20> <EOL> xkey = [ ] <EOL> seed = key <EOL> for i in xrange ( blocks ) : <EOL> seed = hashlib . sha1 ( key + seed ) . digest ( ) <EOL> xkey . append ( seed ) <EOL> j = join ( xkey , '<STR_LIT>' ) <EOL> return array ( uint32 , j ) <EOL> def p3_encrypt ( plain , key ) : <EOL> global _state <EOL> H = _hash <EOL> _state = '<STR_LIT:X>' + _state <EOL> nlist = [ ` time ( ) ` , _pid , _state , ` len ( plain ) ` , plain , key ] <EOL> nonce = H ( join ( nlist , '<STR_LIT:U+002C>' ) ) [ : _ivlen ] <EOL> _state = H ( '<STR_LIT>' + _state + nonce ) <EOL> k_enc , k_auth = H ( '<STR_LIT>' + key + nonce ) , H ( '<STR_LIT>' + key + nonce ) <EOL> n = len ( plain ) <EOL> stream = array ( uint32 , plain + '<STR_LIT>' [ n & <NUM_LIT:3> : ] ) <EOL> xkey = _expand_key ( k_enc , n + <NUM_LIT:4> ) <EOL> for i in xrange ( len ( stream ) ) : <EOL> stream [ i ] = stream [ i ] ^ xkey [ i ] <EOL> ct = nonce + stream . tostring ( ) [ : n ] <EOL> auth = _hmac ( ct , k_auth ) <EOL> return ct + auth [ : _maclen ] <EOL> def p3_decrypt ( cipher , key ) : <EOL> H = _hash <EOL> n = len ( cipher ) - _ivlen - _maclen <EOL> if n < <NUM_LIT:0> : <EOL> raise CryptError , "<STR_LIT>" <EOL> nonce , stream , auth = cipher [ : _ivlen ] , cipher [ _ivlen : - _maclen ] + '<STR_LIT>' [ n & <NUM_LIT:3> : ] , cipher [ - _maclen : ] <EOL> k_enc , k_auth = H ( '<STR_LIT>' + key + nonce ) , H ( '<STR_LIT>' + key + nonce ) <EOL> vauth = _hmac ( cipher [ : - _maclen ] , k_auth ) [ : _maclen ] <EOL> if auth != vauth : <EOL> raise CryptError , "<STR_LIT>" <EOL> stream = array ( uint32 , stream ) <EOL> xkey = _expand_key ( k_enc , n + <NUM_LIT:4> ) <EOL> for i in xrange ( len ( stream ) ) : <EOL> stream [ i ] = stream [ i ] ^ xkey [ i ] <EOL> plain = stream . tostring ( ) [ : n ] <EOL> return plain <EOL> from string import translate <EOL> def _hmac_setup ( ) : <EOL> global _ipad , _opad , _itrans , _otrans <EOL> _itrans = array ( '<STR_LIT:B>' , [ <NUM_LIT:0> ] * <NUM_LIT> ) <EOL> _otrans = array ( '<STR_LIT:B>' , [ <NUM_LIT:0> ] * <NUM_LIT> ) <EOL> for i in xrange ( <NUM_LIT> ) : <EOL> _itrans [ i ] = i ^ <NUM_LIT> <EOL> _otrans [ i ] = i ^ <NUM_LIT> <EOL> _itrans = _itrans . tostring ( ) <EOL> _otrans = _otrans . tostring ( ) <EOL> _ipad = '<STR_LIT>' * <NUM_LIT:64> <EOL> _opad = '<STR_LIT>' * <NUM_LIT:64> <EOL> def _hmac ( msg , key ) : <EOL> if len ( key ) > <NUM_LIT:64> : <EOL> key = hashlib . sha1 ( key ) . digest ( ) <EOL> ki = ( translate ( key , _itrans ) + _ipad ) [ : <NUM_LIT:64> ] <EOL> ko = ( translate ( key , _otrans ) + _opad ) [ : <NUM_LIT:64> ] <EOL> return hashlib . sha1 ( ko + hashlib . sha1 ( ki + msg ) . digest ( ) ) . digest ( ) <EOL> def _time_p3 ( n = <NUM_LIT:1000> , len = <NUM_LIT:20> ) : <EOL> plain = "<STR_LIT:a>" * len <EOL> t = time ( ) <EOL> for i in xrange ( n ) : <EOL> p3_encrypt ( plain , "<STR_LIT>" ) <EOL> dt = time ( ) - t <EOL> print "<STR_LIT>" , n , len , dt , "<STR_LIT>" , n * len / dt , "<STR_LIT>" <EOL> def _speed ( ) : <EOL> _time_p3 ( len = <NUM_LIT:5> ) <EOL> _time_p3 ( ) <EOL> _time_p3 ( len = <NUM_LIT:200> ) <EOL> _time_p3 ( len = <NUM_LIT> , n = <NUM_LIT:100> ) <EOL> def _test ( ) : <EOL> e = p3_encrypt <EOL> d = p3_decrypt <EOL> plain = "<STR_LIT>" <EOL> key = "<STR_LIT>" <EOL> c1 = e ( plain , key ) <EOL> c2 = e ( plain , key ) <EOL> assert c1 != c2 <EOL> assert d ( c2 , key ) == plain <EOL> assert d ( c1 , key ) == plain <EOL> c3 = c2 [ : <NUM_LIT:20> ] + chr ( <NUM_LIT:1> + ord ( c2 [ <NUM_LIT:20> ] ) ) + c2 [ <NUM_LIT> : ] <EOL> try : <EOL> print d ( c3 , key ) <EOL> print "<STR_LIT>" <EOL> except CryptError : <EOL> pass <EOL> try : <EOL> print d ( c2 , '<STR_LIT>' ) <EOL> print "<STR_LIT>" <EOL> except CryptError : <EOL> pass <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _hmac_setup ( ) <EOL> _test ( ) <EOL> _speed ( ) </s>
