<s> import hashlib <EOL> import hmac <EOL> from Crypto . Cipher import AES <EOL> from Crypto . Random . random import randrange <EOL> from pycoin . encoding import ( <EOL> public_pair_to_bitcoin_address , public_pair_to_sec , <EOL> sec_to_public_pair , secret_exponent_to_wif ) <EOL> from pycoin . ecdsa import Point , generator_secp256k1 as BasePoint <EOL> class PubKey ( Point ) : <EOL> """<STR_LIT>""" <EOL> def address ( self ) : <EOL> """<STR_LIT>""" <EOL> return public_pair_to_bitcoin_address ( ( self . x ( ) , self . y ( ) ) ) <EOL> def encrypt ( self , s , mac = <NUM_LIT:16> ) : <EOL> """<STR_LIT>""" <EOL> nonce = randrange ( <NUM_LIT:1> , self . curve ( ) . p ( ) ) <EOL> tmp = BasePoint * nonce <EOL> nonce_point = PubKey ( tmp . curve ( ) , tmp . x ( ) , tmp . y ( ) ) <EOL> tmp = self * nonce <EOL> shared_secret = PubKey ( tmp . curve ( ) , tmp . x ( ) , tmp . y ( ) ) <EOL> key = nonce_point . kdf ( shared_secret ) <EOL> header = nonce_point . sec ( ) <EOL> cipher = AES . new ( key [ : <NUM_LIT:32> ] ) <EOL> s = s + '<STR_LIT:\x00>' * ( <NUM_LIT:16> - len ( s ) % <NUM_LIT:16> ) <EOL> message = cipher . encrypt ( s ) <EOL> checksum_maker = hmac . new ( key [ <NUM_LIT:32> : ] , digestmod = hashlib . sha256 ) <EOL> checksum_maker . update ( message ) <EOL> checksum = checksum_maker . digest ( ) [ : mac ] <EOL> return header + message + checksum <EOL> def kdf ( self , another_p ) : <EOL> """<STR_LIT>""" <EOL> return hashlib . sha512 ( self . sec ( ) + another_p . sec ( ) ) . digest ( ) <EOL> def sec ( self ) : <EOL> """<STR_LIT>""" <EOL> return public_pair_to_sec ( ( self . x ( ) , self . y ( ) ) ) <EOL> def __repr__ ( self ) : <EOL> return self . address ( ) <EOL> class PrivKey : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , exponent ) : <EOL> self . exponent = exponent <EOL> point = BasePoint * exponent <EOL> self . pubkey = PubKey ( point . curve ( ) , point . x ( ) , point . y ( ) ) <EOL> def wif ( self , compressed = True ) : <EOL> """<STR_LIT>""" <EOL> return secret_exponent_to_wif ( self . exponent ) <EOL> def decrypt ( self , raw , mac = <NUM_LIT:16> ) : <EOL> """<STR_LIT>""" <EOL> x , y = sec_to_public_pair ( raw [ : <NUM_LIT> ] ) <EOL> nonce_point = PubKey ( BasePoint . curve ( ) , x , y ) <EOL> message = raw [ <NUM_LIT> : - mac ] <EOL> checksum = raw [ - mac : ] <EOL> tmp = nonce_point * self . exponent <EOL> shared_secret = PubKey ( tmp . curve ( ) , tmp . x ( ) , tmp . y ( ) ) <EOL> key = nonce_point . kdf ( shared_secret ) <EOL> cipher = AES . new ( key [ : <NUM_LIT:32> ] ) <EOL> checksum_maker = hmac . new ( key [ <NUM_LIT:32> : ] , digestmod = hashlib . sha256 ) <EOL> checksum_maker . update ( message ) <EOL> if checksum_maker . digest ( ) [ : mac ] != checksum : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> return cipher . decrypt ( message ) <EOL> def __repr__ ( self ) : <EOL> return self . wif ( ) </s>
