<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import inspect <EOL> import os <EOL> import sys <EOL> from vistrails . core import debug <EOL> from vistrails . core . utils import VistrailsInternalError <EOL> from itertools import izip <EOL> import struct <EOL> import unittest <EOL> try : <EOL> import hashlib <EOL> md5_hash = hashlib . md5 <EOL> except ImportError : <EOL> import md5 <EOL> md5_hash = md5 . new <EOL> class KeyChain ( object ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __keys = { } <EOL> def count ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . __keys . keys ( ) ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __keys . clear ( ) <EOL> def set_key ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> args = inspect . getargvalues ( inspect . currentframe ( ) . f_back ) [ <NUM_LIT:3> ] <EOL> try : <EOL> caller = id ( args [ '<STR_LIT>' ] ) <EOL> newkey = str ( caller ) + str ( key ) <EOL> hashkey = md5_hash ( newkey ) . hexdigest ( ) [ : <NUM_LIT:16> ] <EOL> cryptvalue = crypt ( hashkey , value ) <EOL> self . __keys [ hashkey ] = cryptvalue <EOL> except KeyError : <EOL> debug . critical ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def get_key ( self , key ) : <EOL> """<STR_LIT>""" <EOL> result = "<STR_LIT>" <EOL> args = inspect . getargvalues ( inspect . currentframe ( ) . f_back ) [ <NUM_LIT:3> ] <EOL> try : <EOL> caller = id ( args [ '<STR_LIT>' ] ) <EOL> newkey = str ( caller ) + str ( key ) <EOL> hashkey = md5_hash ( newkey ) . hexdigest ( ) [ : <NUM_LIT:16> ] <EOL> if self . __keys . has_key ( hashkey ) : <EOL> return crypt ( hashkey , self . __keys [ hashkey ] ) <EOL> else : <EOL> debug . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return "<STR_LIT>" <EOL> except KeyError : <EOL> debug . critical ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> """<STR_LIT>""" <EOL> def crypt ( key , data , iv = '<STR_LIT>' , n = <NUM_LIT:32> ) : <EOL> """<STR_LIT>""" <EOL> def keygen ( key , iv , n ) : <EOL> while True : <EOL> iv = xtea_encrypt ( key , iv , n ) <EOL> for k in iv : <EOL> yield ord ( k ) <EOL> xor = [ chr ( x ^ y ) for ( x , y ) in izip ( map ( ord , data ) , keygen ( key , iv , n ) ) ] <EOL> return "<STR_LIT>" . join ( xor ) <EOL> def xtea_encrypt ( key , block , n = <NUM_LIT:32> , endian = "<STR_LIT:!>" ) : <EOL> """<STR_LIT>""" <EOL> v0 , v1 = struct . unpack ( endian + "<STR_LIT>" , block ) <EOL> k = struct . unpack ( endian + "<STR_LIT>" , key ) <EOL> sum , delta , mask = <NUM_LIT:0> L , <NUM_LIT> L , <NUM_LIT> L <EOL> for round in xrange ( n ) : <EOL> v0 = ( v0 + ( ( ( v1 << <NUM_LIT:4> ^ v1 >> <NUM_LIT:5> ) + v1 ) ^ ( sum + k [ sum & <NUM_LIT:3> ] ) ) ) & mask <EOL> sum = ( sum + delta ) & mask <EOL> v1 = ( v1 + ( ( ( v0 << <NUM_LIT:4> ^ v0 >> <NUM_LIT:5> ) + v0 ) ^ ( sum + k [ sum >> <NUM_LIT:11> & <NUM_LIT:3> ] ) ) ) & mask <EOL> return struct . pack ( endian + "<STR_LIT>" , v0 , v1 ) <EOL> def xtea_decrypt ( key , block , n = <NUM_LIT:32> , endian = "<STR_LIT:!>" ) : <EOL> """<STR_LIT>""" <EOL> v0 , v1 = struct . unpack ( endian + "<STR_LIT>" , block ) <EOL> k = struct . unpack ( endian + "<STR_LIT>" , key ) <EOL> delta , mask = <NUM_LIT> L , <NUM_LIT> L <EOL> sum = ( delta * n ) & mask <EOL> for round in xrange ( n ) : <EOL> v1 = ( v1 - ( ( ( v0 << <NUM_LIT:4> ^ v0 >> <NUM_LIT:5> ) + v0 ) ^ ( sum + k [ sum >> <NUM_LIT:11> & <NUM_LIT:3> ] ) ) ) & mask <EOL> sum = ( sum - delta ) & mask <EOL> v0 = ( v0 - ( ( ( v1 << <NUM_LIT:4> ^ v1 >> <NUM_LIT:5> ) + v1 ) ^ ( sum + k [ sum & <NUM_LIT:3> ] ) ) ) & mask <EOL> return struct . pack ( endian + "<STR_LIT>" , v0 , v1 ) <EOL> class A ( object ) : <EOL> def set_key ( self , chain , key , value ) : <EOL> chain . set_key ( key , value ) <EOL> def get_key ( self , chain , key ) : <EOL> return chain . get_key ( key ) <EOL> class TestKeyChain ( unittest . TestCase ) : <EOL> def testusage ( self ) : <EOL> keyChain = KeyChain ( ) <EOL> this = A ( ) <EOL> other = A ( ) <EOL> this . set_key ( keyChain , "<STR_LIT>" , "<STR_LIT:value>" ) <EOL> self . assertEquals ( keyChain . count ( ) , <NUM_LIT:1> ) <EOL> value = this . get_key ( keyChain , "<STR_LIT>" ) <EOL> self . assertEquals ( value , "<STR_LIT:value>" ) <EOL> value = other . get_key ( keyChain , "<STR_LIT>" ) <EOL> self . assertEquals ( value , "<STR_LIT>" ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
