<s> import subprocess <EOL> import hashlib <EOL> import random <EOL> import re <EOL> class Cryptic : <EOL> MAGIC = "<STR_LIT>" <EOL> MAGIC_SEARCH = "<STR_LIT>" <EOL> HASH_MAGIC = "<STR_LIT>" <EOL> HASH_MAGIC_SEARCH = "<STR_LIT>" <EOL> FAMILY = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> EXTRA = { } <EOL> VALID = "<STR_LIT>" <EOL> NUM_ALPHA = "<STR_LIT>" <EOL> ALPHA_NUM = { } <EOL> ENCODING = [ [ <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:32> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:16> , <NUM_LIT:32> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:8> , <NUM_LIT:32> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:64> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:32> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:16> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:32> , <NUM_LIT:64> ] ] <EOL> def __init__ ( self ) : <EOL> for fam in range ( len ( self . FAMILY ) ) : <EOL> for c in range ( len ( self . FAMILY [ fam ] ) ) : <EOL> token = self . FAMILY [ fam ] <EOL> self . EXTRA [ token [ c ] ] = ( <NUM_LIT:3> - fam ) <EOL> self . NUM_ALPHA = '<STR_LIT>' . join ( self . FAMILY ) <EOL> self . VALID = "<STR_LIT:[>" + self . MAGIC + self . NUM_ALPHA + "<STR_LIT:]>" <EOL> for num_alpha in range ( len ( self . NUM_ALPHA ) ) : <EOL> self . ALPHA_NUM [ self . NUM_ALPHA [ num_alpha ] ] = num_alpha <EOL> def _randc ( self , count = <NUM_LIT:1> ) : <EOL> ret = "<STR_LIT>" <EOL> while ( count > <NUM_LIT:0> ) : <EOL> ret = ret + self . NUM_ALPHA [ random . randint ( <NUM_LIT:0> , len ( self . NUM_ALPHA ) - <NUM_LIT:1> ) ] <EOL> count = count - <NUM_LIT:1> <EOL> return ret <EOL> def _gap_encode ( self , pc , prev , enc ) : <EOL> literal_pc = ord ( pc ) <EOL> crypt = '<STR_LIT>' <EOL> gaps = [ ] <EOL> for mod in reversed ( enc ) : <EOL> gaps . insert ( <NUM_LIT:0> , int ( literal_pc / mod ) ) <EOL> literal_pc %= mod <EOL> for gap in gaps : <EOL> gap += self . ALPHA_NUM [ prev ] + <NUM_LIT:1> <EOL> prev = self . NUM_ALPHA [ gap % len ( self . NUM_ALPHA ) ] <EOL> c = prev <EOL> crypt += c ; <EOL> return crypt <EOL> def encrypt ( self , plain , salt = None ) : <EOL> if salt is None : <EOL> salt = self . _randc ( <NUM_LIT:1> ) <EOL> rand = self . _randc ( self . EXTRA [ salt ] ) <EOL> pos = <NUM_LIT:0> <EOL> prev = salt <EOL> crypt = self . MAGIC + str ( salt ) + str ( rand ) <EOL> for p_index in range ( len ( plain ) ) : <EOL> p = plain [ p_index ] <EOL> encode = self . ENCODING [ pos % len ( self . ENCODING ) ] <EOL> crypt += self . _gap_encode ( p , prev , encode ) <EOL> prev = crypt [ - <NUM_LIT:1> ] <EOL> pos += <NUM_LIT:1> <EOL> return crypt <EOL> def _nibble ( self , cref , length ) : <EOL> nib = cref [ <NUM_LIT:0> : length ] <EOL> cref = cref [ length : ] <EOL> return nib , cref <EOL> def _gap ( self , c1 , c2 ) : <EOL> return ( ( self . ALPHA_NUM [ c2 ] - self . ALPHA_NUM [ c1 ] ) % len ( self . NUM_ALPHA ) ) - <NUM_LIT:1> <EOL> def _gap_decode ( self , gaps , dec ) : <EOL> num = <NUM_LIT:0> <EOL> if len ( gaps ) != len ( dec ) : <EOL> return None <EOL> else : <EOL> for i in range ( len ( gaps ) ) : <EOL> num += gaps [ i ] * dec [ i ] <EOL> return chr ( num % <NUM_LIT> ) <EOL> def decrypt ( self , crypt ) : <EOL> if ( crypt == None or len ( crypt ) == <NUM_LIT:0> ) : <EOL> print "<STR_LIT>" <EOL> return None <EOL> valid_chars = re . compile ( self . VALID ) <EOL> if ( valid_chars . match ( crypt ) != None ) : <EOL> match_object = re . match ( self . MAGIC_SEARCH , crypt ) <EOL> chars = crypt [ match_object . end ( ) : ] <EOL> first , chars = self . _nibble ( chars , <NUM_LIT:1> ) <EOL> var , chars = self . _nibble ( chars , self . EXTRA [ first ] ) <EOL> prev = first <EOL> decrypt_str = '<STR_LIT>' <EOL> while len ( chars ) > <NUM_LIT:0> : <EOL> decode = self . ENCODING [ len ( decrypt_str ) % len ( self . ENCODING ) ] <EOL> length = len ( decode ) <EOL> nibble , chars = self . _nibble ( chars , length ) <EOL> gaps = [ ] <EOL> for i in range ( len ( nibble ) ) : <EOL> gaps . append ( self . _gap ( prev , nibble [ i ] ) ) <EOL> prev = nibble [ i ] <EOL> decrypt_str += self . _gap_decode ( gaps , decode ) <EOL> return decrypt_str <EOL> else : <EOL> print Crypt + "<STR_LIT>" <EOL> def hashify ( self , plain_text ) : <EOL> cmd = "<STR_LIT>" + self . HASH_MAGIC + "<STR_LIT:U+0020>" + plain_text <EOL> try : <EOL> output = subprocess . check_output ( cmd , shell = True ) <EOL> return output . strip ( ) <EOL> except subprocess . CalledProcessError as e : <EOL> print "<STR_LIT>" + cmd + "<STR_LIT>" + str ( e . returncode ) + "<STR_LIT>" + e . output <EOL> return None <EOL> def authenticate_hash ( self , plain_text , hash_text ) : <EOL> match_object = re . match ( self . HASH_MAGIC_SEARCH , hash_text ) <EOL> if match_object is not None : <EOL> hashed = self . hashify ( plain_text ) <EOL> if ( hashed is not None and hashed == hash_text ) : <EOL> return True <EOL> else : <EOL> return False <EOL> else : <EOL> print "<STR_LIT>" <EOL> return None <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> cryptic = Cryptic ( ) <EOL> print cryptic . decrypt ( cryptic . encrypt ( '<STR_LIT>' ) ) <EOL> print cryptic . decrypt ( cryptic . encrypt ( '<STR_LIT>' ) ) <EOL> print cryptic . decrypt ( cryptic . encrypt ( '<STR_LIT>' ) ) <EOL> hash_text = cryptic . hashify ( "<STR_LIT>" ) <EOL> print cryptic . authenticate_hash ( "<STR_LIT>" , hash_text ) <EOL> hash_text = cryptic . hashify ( "<STR_LIT>" ) <EOL> print cryptic . authenticate_hash ( "<STR_LIT>" , hash_text ) <EOL> hash_text = cryptic . hashify ( "<STR_LIT>" ) <EOL> print cryptic . authenticate_hash ( "<STR_LIT>" , hash_text ) </s>
