示例#1
0
 def testI01EncryptPublicElGamalDES3ZIP(self):
     """crypto.cipher: encrypt()/decrypt() ElGamal, DES3 w/ integrity, & ZIP"""
     key_d = read_test_file(['pgpfiles', 'key', 'DSAELG3.pub.gpg'])
     keypkt = list_pkts(key_d)[3]  # ElGamal encrypting key
     d = "My secret message."
     # literal data packet
     litbody = create_LiteralDataBody(data=d,
                                      modified=0,
                                      format='b',
                                      filename='outfile')
     litpkt = create_Packet(PKT_LITERAL, litbody._d)
     # compressed data packet
     compbody = create_CompressedDataBody(COMP_ZIP, litpkt.rawstr())
     comppkt = create_Packet(PKT_COMPRESSED, compbody._d)
     # session key
     key = gen_random(_keysize(SYM_DES3))
     sespkt = encrypt_public_session(keypkt, key, SYM_DES3)
     # encrypted data
     encpkt = encrypt_integrity(SYM_DES3, key, comppkt.rawstr())
     # decryption
     seckey_d = read_test_file(
         ['pgpfiles', 'key', 'DSAELG3.sec.nopass.gpg'])
     seckeypkt = list_pkts(seckey_d)[2]
     clrtxt = decrypt(encpkt, None, sespkt, seckeypkt)
     # got compressed
     comppkt_out = list_pkts(clrtxt)[0]
     # got literal
     litpkt_out = list_pkts(comppkt_out.body.data)[0]
     self.assertEqual(d, litpkt_out.body.data)
     self.assertEqual('outfile', litpkt_out.body.filename)
     self.assertEqual(0, litpkt_out.body.modified)
     self.assertEqual('b', litpkt_out.body.format)
 def testI01EncryptPublicElGamalDES3ZIP(self):
     """crypto.cipher: encrypt()/decrypt() ElGamal, DES3 w/ integrity, & ZIP"""
     key_d = read_test_file(['pgpfiles','key','DSAELG3.pub.gpg'])
     keypkt = list_pkts(key_d)[3] # ElGamal encrypting key
     d = "My secret message."
     # literal data packet
     litbody = create_LiteralDataBody(data=d, modified=0, format='b',
                                          filename='outfile')
     litpkt = create_Packet(PKT_LITERAL, litbody._d)
     # compressed data packet
     compbody = create_CompressedDataBody(COMP_ZIP, litpkt.rawstr())
     comppkt = create_Packet(PKT_COMPRESSED, compbody._d)
     # session key
     key = gen_random(_keysize(SYM_DES3))
     sespkt = encrypt_public_session(keypkt, key, SYM_DES3)
     # encrypted data
     encpkt = encrypt_integrity(SYM_DES3, key, comppkt.rawstr())
     # decryption
     seckey_d = read_test_file(['pgpfiles','key','DSAELG3.sec.nopass.gpg'])
     seckeypkt = list_pkts(seckey_d)[2]
     clrtxt = decrypt(encpkt, None, sespkt, seckeypkt)
     # got compressed
     comppkt_out = list_pkts(clrtxt)[0]
     # got literal
     litpkt_out = list_pkts(comppkt_out.body.data)[0]
     self.assertEqual(d, litpkt_out.body.data)
     self.assertEqual('outfile', litpkt_out.body.filename)
     self.assertEqual(0, litpkt_out.body.modified)
     self.assertEqual('b', litpkt_out.body.format)
示例#3
0
def create_LiteralMsg(literals):
    """Create a literal message out of a sequence of literal data parameters.

    :Parameters:
        - `literals`: list of dictionaries containing literal data
          parameters (see `Literal keys and values`_)

    :Returns: `OpenPGP.message.LiteralMsg.LiteralMsg` instance

    :Exceptions:
        - `PGPError`: literal message was not created, fix source

    .. _Literal keys and values:

    Literal keys and values:

        - `data`: string of literal data
        - `modified`: *optional* integer timestamp
        - `filename`: *optional* string filename
        - `format`: *optional* 'b' for binary or 't' for text (default
          binary)
    """
    from openpgp.sap.pkt.Packet import create_Packet
    from openpgp.sap.pkt.LiteralData import create_LiteralDataBody
    from openpgp.sap.list import find_literal_msg

    import time

    if isinstance(literals, dict): # to accomodate a single dictionary
        literals = [literals] # yikes

    litpkts = []
    i = 0

    for lit in literals: # assume 'data' is present, allow defaults for rest

        if not lit.has_key('modified'):
            lit['modified'] = int(time.time())

        if not lit.has_key('format'):
            lit['format'] = 'b'

        if not lit.has_key('filename'):
            lit['filename'] = "sap_out_%s" % i

        litbody = create_LiteralDataBody(lit)
        litpkts.append(create_Packet(PKT_LITERAL, litbody._d))

    litmsg = find_literal_msg(litpkts)[0]

    if litmsg:
        return litmsg
    else:
        raise Exception("Failed to create literal messge. Fix source.")
示例#4
0
 def testB01LiteralDataCreation(self):
     """LiteralData: create_LiteralDataBody() with known good"""
     _d = read_test_file(['pgpfiles', 'sig', 'sig.DSAELG1.onepass.gpg'])
     litpkt = list_pkts(_d)[1]
     litparams = {
         'data': litpkt.body.data,
         'modified': litpkt.body.modified,
         'format': litpkt.body.format,
         'filename': litpkt.body.filename
     }
     litpktbody = create_LiteralDataBody(litparams)
     self.assertEqual(litpktbody._d, litpkt.body._d)