<s> import time <EOL> import os <EOL> import sys <EOL> from Crypto . PublicKey import RSA <EOL> from Crypto . Cipher import AES , ARC2 , ARC4 , Blowfish , CAST , DES3 , DES , XOR <EOL> from Crypto . Hash import MD2 , MD4 , MD5 , SHA256 , SHA <EOL> try : <EOL> from Crypto . Hash import RIPEMD <EOL> except ImportError : <EOL> RIPEMD = None <EOL> class Benchmark : <EOL> def __init__ ( self ) : <EOL> self . __random_data = None <EOL> def random_keys ( self , bytes ) : <EOL> """<STR_LIT>""" <EOL> return self . random_blocks ( bytes , <NUM_LIT:10> ** <NUM_LIT:5> ) <EOL> def random_blocks ( self , bytes_per_block , blocks ) : <EOL> bytes = bytes_per_block * blocks <EOL> data = self . random_data ( bytes ) <EOL> retval = [ ] <EOL> for i in xrange ( blocks ) : <EOL> p = i * bytes_per_block <EOL> retval . append ( data [ p : p + bytes_per_block ] ) <EOL> return retval <EOL> def random_data ( self , bytes ) : <EOL> if self . __random_data is None : <EOL> self . __random_data = self . _random_bytes ( bytes ) <EOL> return self . __random_data <EOL> elif bytes == len ( self . __random_data ) : <EOL> return self . __random_data <EOL> elif bytes < len ( self . __random_data ) : <EOL> return self . __random_data [ : bytes ] <EOL> else : <EOL> self . __random_data += self . _random_bytes ( bytes - len ( self . __random_data ) ) <EOL> return self . __random_data <EOL> def _random_bytes ( self , b ) : <EOL> return os . urandom ( b ) <EOL> def announce_start ( self , test_name ) : <EOL> sys . stdout . write ( "<STR_LIT>" % ( test_name , ) ) <EOL> sys . stdout . flush ( ) <EOL> def announce_result ( self , value , units ) : <EOL> sys . stdout . write ( "<STR_LIT>" % ( value , units ) ) <EOL> sys . stdout . flush ( ) <EOL> def test_pubkey_setup ( self , pubkey_name , module , key_bytes ) : <EOL> self . announce_start ( "<STR_LIT>" % ( pubkey_name , ) ) <EOL> keys = self . random_keys ( key_bytes ) [ : <NUM_LIT:5> ] <EOL> t0 = time . time ( ) <EOL> for k in keys : <EOL> module . generate ( key_bytes * <NUM_LIT:8> ) <EOL> t = time . time ( ) <EOL> pubkey_setups_per_second = len ( keys ) / ( t - t0 ) <EOL> self . announce_result ( pubkey_setups_per_second , "<STR_LIT>" ) <EOL> def test_key_setup ( self , cipher_name , module , key_bytes , mode ) : <EOL> self . announce_start ( "<STR_LIT>" % ( cipher_name , ) ) <EOL> keys = self . random_keys ( key_bytes ) <EOL> if mode is None : <EOL> t0 = time . time ( ) <EOL> for k in keys : <EOL> module . new ( k ) <EOL> t = time . time ( ) <EOL> else : <EOL> t0 = time . time ( ) <EOL> for k in keys : <EOL> module . new ( k , module . MODE_ECB ) <EOL> t = time . time ( ) <EOL> key_setups_per_second = len ( keys ) / ( t - t0 ) <EOL> self . announce_result ( key_setups_per_second / <NUM_LIT:1000> , "<STR_LIT>" ) <EOL> def test_encryption ( self , cipher_name , module , key_bytes , mode ) : <EOL> self . announce_start ( "<STR_LIT>" % ( cipher_name , ) ) <EOL> rand = self . random_data ( key_bytes + module . block_size ) <EOL> key , iv = rand [ : key_bytes ] , rand [ key_bytes : ] <EOL> blocks = self . random_blocks ( <NUM_LIT> , <NUM_LIT:1000> ) <EOL> if mode is None : <EOL> cipher = module . new ( key ) <EOL> else : <EOL> cipher = module . new ( key , mode , iv ) <EOL> t0 = time . time ( ) <EOL> for b in blocks : <EOL> cipher . encrypt ( b ) <EOL> t = time . time ( ) <EOL> encryption_speed = ( len ( blocks ) * len ( blocks [ <NUM_LIT:0> ] ) ) / ( t - t0 ) <EOL> self . announce_result ( encryption_speed / <NUM_LIT:10> ** <NUM_LIT:6> , "<STR_LIT>" ) <EOL> def test_hash_small ( self , hash_name , module ) : <EOL> self . announce_start ( "<STR_LIT>" % ( hash_name , module . digest_size ) ) <EOL> blocks = self . random_blocks ( module . digest_size , <NUM_LIT> ) <EOL> t0 = time . time ( ) <EOL> for b in blocks : <EOL> module . new ( b ) . digest ( ) <EOL> t = time . time ( ) <EOL> hashes_per_second = len ( blocks ) / ( t - t0 ) <EOL> self . announce_result ( hashes_per_second / <NUM_LIT:1000> , "<STR_LIT>" ) <EOL> def test_hash_large ( self , hash_name , module ) : <EOL> self . announce_start ( "<STR_LIT>" % ( hash_name , ) ) <EOL> blocks = self . random_blocks ( <NUM_LIT> , <NUM_LIT> ) <EOL> t0 = time . time ( ) <EOL> h = module . new ( ) <EOL> for b in blocks : <EOL> h . update ( b ) <EOL> h . digest ( ) <EOL> t = time . time ( ) <EOL> hash_speed = len ( blocks ) * len ( blocks [ <NUM_LIT:0> ] ) / ( t - t0 ) <EOL> self . announce_result ( hash_speed / <NUM_LIT:10> ** <NUM_LIT:6> , "<STR_LIT>" ) <EOL> def run ( self ) : <EOL> pubkey_specs = [ <EOL> ( "<STR_LIT>" , RSA , <NUM_LIT> / <NUM_LIT:8> ) , <EOL> ( "<STR_LIT>" , RSA , <NUM_LIT> / <NUM_LIT:8> ) , <EOL> ( "<STR_LIT>" , RSA , <NUM_LIT> / <NUM_LIT:8> ) , <EOL> ] <EOL> block_specs = [ <EOL> ( "<STR_LIT>" , DES , <NUM_LIT:8> ) , <EOL> ( "<STR_LIT>" , DES3 , <NUM_LIT> ) , <EOL> ( "<STR_LIT>" , AES , <NUM_LIT:16> ) , <EOL> ( "<STR_LIT>" , AES , <NUM_LIT> ) , <EOL> ( "<STR_LIT>" , AES , <NUM_LIT:32> ) , <EOL> ( "<STR_LIT>" , Blowfish , <NUM_LIT:32> ) , <EOL> ( "<STR_LIT>" , CAST , <NUM_LIT:5> ) , <EOL> ( "<STR_LIT>" , CAST , <NUM_LIT:10> ) , <EOL> ( "<STR_LIT>" , CAST , <NUM_LIT:16> ) , <EOL> ] <EOL> stream_specs = [ <EOL> ( "<STR_LIT>" , ARC2 , <NUM_LIT:16> ) , <EOL> ( "<STR_LIT>" , ARC4 , <NUM_LIT:16> ) , <EOL> ( "<STR_LIT>" , XOR , <NUM_LIT:3> ) , <EOL> ( "<STR_LIT>" , XOR , <NUM_LIT:32> ) , <EOL> ] <EOL> hash_specs = [ <EOL> ( "<STR_LIT>" , MD2 ) , <EOL> ( "<STR_LIT>" , MD4 ) , <EOL> ( "<STR_LIT>" , MD5 ) , <EOL> ( "<STR_LIT>" , SHA ) , <EOL> ( "<STR_LIT>" , SHA256 ) , <EOL> ] <EOL> if RIPEMD is not None : <EOL> hash_specs += [ ( "<STR_LIT>" , RIPEMD ) ] <EOL> for pubkey_name , module , key_bytes in pubkey_specs : <EOL> self . test_pubkey_setup ( pubkey_name , module , key_bytes ) <EOL> for cipher_name , module , key_bytes in block_specs : <EOL> self . test_key_setup ( cipher_name , module , key_bytes , module . MODE_CBC ) <EOL> self . test_encryption ( "<STR_LIT>" % ( cipher_name , ) , module , key_bytes , module . MODE_CBC ) <EOL> self . test_encryption ( "<STR_LIT>" % ( cipher_name , ) , module , key_bytes , module . MODE_CFB ) <EOL> self . test_encryption ( "<STR_LIT>" % ( cipher_name , ) , module , key_bytes , module . MODE_OFB ) <EOL> self . test_encryption ( "<STR_LIT>" % ( cipher_name , ) , module , key_bytes , module . MODE_ECB ) <EOL> self . test_encryption ( "<STR_LIT>" % ( cipher_name , ) , module , key_bytes , module . MODE_OPENPGP ) <EOL> for cipher_name , module , key_bytes in stream_specs : <EOL> self . test_key_setup ( cipher_name , module , key_bytes , None ) <EOL> self . test_encryption ( cipher_name , module , key_bytes , None ) <EOL> for hash_name , module in hash_specs : <EOL> self . test_hash_small ( hash_name , module ) <EOL> self . test_hash_large ( hash_name , module ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> Benchmark ( ) . run ( ) </s>
