<s> import nose <EOL> import os . path <EOL> import OpenPGP <EOL> import OpenPGP . cryptography <EOL> import Crypto . Util <EOL> import Crypto . PublicKey . RSA <EOL> class TestMessageVerification : <EOL> def oneMessage ( self , pkey , path ) : <EOL> pkeyM = OpenPGP . Message . parse ( open ( os . path . dirname ( __file__ ) + '<STR_LIT>' + pkey , '<STR_LIT:rb>' ) . read ( ) ) <EOL> m = OpenPGP . Message . parse ( open ( os . path . dirname ( __file__ ) + '<STR_LIT>' + path , '<STR_LIT:rb>' ) . read ( ) ) <EOL> verify = OpenPGP . cryptography . Wrapper ( pkeyM ) <EOL> nose . tools . assert_equal ( verify . verify ( m ) , m . signatures ( ) ) <EOL> def testUncompressedOpsRSA ( self ) : <EOL> self . oneMessage ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def testCompressedSig ( self ) : <EOL> self . oneMessage ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def testCompressedSigZLIB ( self ) : <EOL> self . oneMessage ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def testCompressedSigBzip2 ( self ) : <EOL> self . oneMessage ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def testSigningMessagesRSA ( self ) : <EOL> wkey = OpenPGP . Message . parse ( open ( os . path . dirname ( __file__ ) + '<STR_LIT>' , '<STR_LIT:rb>' ) . read ( ) ) <EOL> data = OpenPGP . LiteralDataPacket ( '<STR_LIT>' , '<STR_LIT:u>' , '<STR_LIT>' ) <EOL> sign = OpenPGP . cryptography . Wrapper ( wkey ) <EOL> m = sign . sign ( data ) . to_bytes ( ) <EOL> reparsedM = OpenPGP . Message . parse ( m ) <EOL> nose . tools . assert_equal ( sign . verify ( reparsedM ) , reparsedM . signatures ( ) ) <EOL> def testSigningMessagesDSA ( self ) : <EOL> wkey = OpenPGP . Message . parse ( open ( os . path . dirname ( __file__ ) + '<STR_LIT>' , '<STR_LIT:rb>' ) . read ( ) ) <EOL> data = OpenPGP . LiteralDataPacket ( '<STR_LIT>' , '<STR_LIT:u>' , '<STR_LIT>' ) <EOL> dsa = OpenPGP . cryptography . Wrapper ( wkey ) . private_key ( '<STR_LIT>' ) <EOL> m = OpenPGP . cryptography . Wrapper ( data ) . sign ( dsa , '<STR_LIT>' , '<STR_LIT>' ) . to_bytes ( ) <EOL> reparsedM = OpenPGP . Message . parse ( m ) <EOL> nose . tools . assert_equal ( OpenPGP . cryptography . Wrapper ( wkey ) . verify ( reparsedM ) , reparsedM . signatures ( ) ) <EOL> def testUncompressedOpsDSA ( self ) : <EOL> self . oneMessage ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def testUncompressedOpsDSAsha384 ( self ) : <EOL> self . oneMessage ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestKeyVerification : <EOL> def oneKeyRSA ( self , path ) : <EOL> m = OpenPGP . Message . parse ( open ( os . path . dirname ( __file__ ) + '<STR_LIT>' + path , '<STR_LIT:rb>' ) . read ( ) ) <EOL> verify = OpenPGP . cryptography . Wrapper ( m ) <EOL> nose . tools . assert_equal ( verify . verify ( m ) , m . signatures ( ) ) <EOL> def testSigningKeysRSA ( self ) : <EOL> k = Crypto . PublicKey . RSA . generate ( <NUM_LIT> ) <EOL> nkey = OpenPGP . SecretKeyPacket ( ( <EOL> Crypto . Util . number . long_to_bytes ( k . n ) , <EOL> Crypto . Util . number . long_to_bytes ( k . e ) , <EOL> Crypto . Util . number . long_to_bytes ( k . d ) , <EOL> Crypto . Util . number . long_to_bytes ( k . p ) , <EOL> Crypto . Util . number . long_to_bytes ( k . q ) , <EOL> Crypto . Util . number . long_to_bytes ( k . u ) <EOL> ) ) <EOL> uid = OpenPGP . UserIDPacket ( '<STR_LIT>' ) <EOL> wkey = OpenPGP . cryptography . Wrapper ( nkey ) <EOL> m = wkey . sign_key_userid ( [ nkey , uid ] ) . to_bytes ( ) <EOL> reparsedM = OpenPGP . Message . parse ( m ) <EOL> nose . tools . assert_equal ( wkey . verify ( reparsedM ) , reparsedM . signatures ( ) ) <EOL> def testHelloKey ( self ) : <EOL> self . oneKeyRSA ( "<STR_LIT>" ) <EOL> class TestDecryption : <EOL> def oneSymmetric ( self , pss , cnt , path ) : <EOL> m = OpenPGP . Message . parse ( open ( os . path . dirname ( __file__ ) + '<STR_LIT>' + path , '<STR_LIT:rb>' ) . read ( ) ) <EOL> m2 = OpenPGP . cryptography . Wrapper ( m ) . decrypt_symmetric ( pss ) <EOL> while ( isinstance ( m2 [ <NUM_LIT:0> ] , OpenPGP . CompressedDataPacket ) ) : <EOL> m2 = m2 [ <NUM_LIT:0> ] . data <EOL> for p in m2 : <EOL> if ( isinstance ( p , OpenPGP . LiteralDataPacket ) ) : <EOL> nose . tools . assert_equal ( p . data , cnt ) <EOL> def testDecryptAES ( self ) : <EOL> self . oneSymmetric ( "<STR_LIT:hello>" , b"<STR_LIT>" , "<STR_LIT>" ) <EOL> def testDecryptNoMDC ( self ) : <EOL> self . oneSymmetric ( "<STR_LIT:hello>" , b"<STR_LIT>" , "<STR_LIT>" ) <EOL> def testDecrypt3DES ( self ) : <EOL> self . oneSymmetric ( "<STR_LIT:hello>" , b"<STR_LIT>" , "<STR_LIT>" ) <EOL> def testDecryptBlowfish ( self ) : <EOL> self . oneSymmetric ( "<STR_LIT:hello>" , b"<STR_LIT>" , "<STR_LIT>" ) <EOL> def testDecryptCAST5 ( self ) : <EOL> self . oneSymmetric ( "<STR_LIT:hello>" , b"<STR_LIT>" , "<STR_LIT>" ) <EOL> def testDecryptSessionKey ( self ) : <EOL> self . oneSymmetric ( "<STR_LIT:hello>" , b"<STR_LIT>" , "<STR_LIT>" ) <EOL> def testDecryptSecretKey ( self ) : <EOL> key = OpenPGP . Message . parse ( open ( os . path . dirname ( __file__ ) + '<STR_LIT>' , '<STR_LIT:rb>' ) . read ( ) ) <EOL> skey = OpenPGP . cryptography . Wrapper ( key [ <NUM_LIT:0> ] ) . decrypt_secret_key ( "<STR_LIT:hello>" ) <EOL> nose . tools . assert_equal ( not ( not skey ) , True ) <EOL> def testDecryptAsymmetric ( self ) : <EOL> m = OpenPGP . Message . parse ( open ( os . path . dirname ( __file__ ) + '<STR_LIT>' , '<STR_LIT:rb>' ) . read ( ) ) <EOL> key = OpenPGP . Message . parse ( open ( os . path . dirname ( __file__ ) + '<STR_LIT>' , '<STR_LIT:rb>' ) . read ( ) ) <EOL> m2 = OpenPGP . cryptography . Wrapper ( key ) . decrypt ( m ) <EOL> while ( isinstance ( m2 [ <NUM_LIT:0> ] , OpenPGP . CompressedDataPacket ) ) : <EOL> m2 = m2 [ <NUM_LIT:0> ] . data <EOL> for p in m2 : <EOL> if ( isinstance ( p , OpenPGP . LiteralDataPacket ) ) : <EOL> nose . tools . assert_equal ( p . data , b"<STR_LIT>" ) <EOL> class TestEncryption : <EOL> def testEncryptSymmetric ( self ) : <EOL> data = OpenPGP . LiteralDataPacket ( '<STR_LIT>' , '<STR_LIT:u>' , '<STR_LIT>' ) <EOL> encrypted = OpenPGP . cryptography . Wrapper ( OpenPGP . Message ( [ data ] ) ) . encrypt ( '<STR_LIT>' ) <EOL> decrypted = OpenPGP . cryptography . Wrapper ( encrypted ) . decrypt_symmetric ( '<STR_LIT>' ) <EOL> nose . tools . assert_equal ( decrypted [ <NUM_LIT:0> ] . data , b'<STR_LIT>' ) <EOL> def testEncryptAsymmetric ( self ) : <EOL> key = OpenPGP . Message . parse ( open ( os . path . dirname ( __file__ ) + '<STR_LIT>' , '<STR_LIT:rb>' ) . read ( ) ) <EOL> data = OpenPGP . LiteralDataPacket ( '<STR_LIT>' , '<STR_LIT:u>' , '<STR_LIT>' ) <EOL> encrypted = OpenPGP . Crypto . Wrapper ( OpenPGP . Message ( [ data ] ) ) . encrypt ( key ) <EOL> decryptor = OpenPGP . Crypto . Wrapper ( key ) <EOL> decrypted = decryptor . decrypt ( encrypted ) <EOL> nose . tools . assert_equal ( decrypted [ <NUM_LIT:0> ] . data , b'<STR_LIT>' ) </s>
