def test_encrypt_file(self): file_src = os.path.join(os.getcwd(), 'test_plaintext_file.txt') file_dst = os.path.join(os.getcwd(), 'test_encrypted_file.txt') fake_receiver_desc = { 'pgp_key_public': helpers.PGPKEYS['VALID_PGP_KEY1_PRV'], 'pgp_key_fingerprint': u'ECAF2235E78E71CD95365843C7B190543CAA7585', 'username': u'*****@*****.**', } # these are the same lines used in delivery_sched.py pgpobj = GLBPGP() pgpobj.load_key(helpers.PGPKEYS['VALID_PGP_KEY1_PRV']) with open(file_src, 'w+') as f: f.write(self.secret_content) f.seek(0) pgpobj.encrypt_file(fake_receiver_desc['pgp_key_fingerprint'], f, file_dst) with open(file_dst, 'r') as f: self.assertEqual(str(pgpobj.gnupg.decrypt_file(f)), self.secret_content) pgpobj.destroy_environment()
def fsops_pgp_encrypt(fpath, recipient_pgp): """ return path of encrypted file, length of the encrypted file this function is used to encrypt a file for a specific recipient. commonly 'receiver_desc' is expected as second argument; anyhow a simpler dict can be used. required keys are checked on top """ gpoj = GLBPGP() try: gpoj.load_key(recipient_pgp['pgp_key_public']) filepath = os.path.join(GLSettings.submission_path, fpath) with GLSecureFile(filepath) as f: encrypted_file_path = os.path.join(os.path.abspath(GLSettings.submission_path), "pgp_encrypted-%s" % generateRandomKey(16)) _, encrypted_file_size = gpoj.encrypt_file(recipient_pgp['pgp_key_fingerprint'], f, encrypted_file_path) except: raise finally: # the finally statement is always called also if # except contains a return or a raise gpoj.destroy_environment() return encrypted_file_path, encrypted_file_size
def test_encrypt_file(self): # setup the PGP key before GLSetting.pgproot = PGPROOT tempsource = os.path.join(os.getcwd(), "temp_source.txt") with file(tempsource, 'w+') as f1: f1.write("\n\nDecrypt the Cat!\n\nhttp://tobtu.com/decryptocat.php\n\n") f1.seek(0) fake_receiver_desc = { 'pgp_key_public': unicode(VALID_PGP_KEY1), 'pgp_key_status': u'enabled', 'pgp_key_fingerprint': u"CF4A22020873A76D1DCB68D32B25551568E49345", 'username': u'*****@*****.**', } # these are the same lines used in delivery_sched.py pgpobj = GLBPGP() pgpobj.load_key(VALID_PGP_KEY1) encrypted_file_path, encrypted_file_size = pgpobj.encrypt_file(fake_receiver_desc['pgp_key_fingerprint'], tempsource, f1, "/tmp") pgpobj.destroy_environment() with file(encrypted_file_path, "r") as f2: first_line = f2.readline() self.assertSubstring('-----BEGIN PGP MESSAGE-----', first_line) with file(encrypted_file_path, "r") as f2: whole = f2.read() self.assertEqual(encrypted_file_size, len(whole))
def test_encrypt_file(self): # setup the PGP key before GLSettings.pgproot = PGPROOT tempsource = os.path.join(os.getcwd(), "temp_source.txt") with file(tempsource, 'w+') as f1: f1.write("\n\nDecrypt the Cat!\n\nhttp://tobtu.com/decryptocat.php\n\n") f1.seek(0) fake_receiver_desc = { 'pgp_key_public': unicode(VALID_PGP_KEY1), 'pgp_key_status': u'enabled', 'pgp_key_fingerprint': u"CF4A22020873A76D1DCB68D32B25551568E49345", 'username': u'*****@*****.**', } # these are the same lines used in delivery_sched.py pgpobj = GLBPGP() pgpobj.load_key(VALID_PGP_KEY1) encrypted_file_path, encrypted_file_size = pgpobj.encrypt_file(fake_receiver_desc['pgp_key_fingerprint'], tempsource, f1, "/tmp") pgpobj.destroy_environment() with file(encrypted_file_path, "r") as f2: first_line = f2.readline() self.assertSubstring('-----BEGIN PGP MESSAGE-----', first_line) with file(encrypted_file_path, "r") as f2: whole = f2.read() self.assertEqual(encrypted_file_size, len(whole))
def test_encrypt_file(self): file_src = os.path.join(os.getcwd(), 'test_plaintext_file.txt') file_dst = os.path.join(os.getcwd(), 'test_encrypted_file.txt') fake_receiver_desc = { 'pgp_key_public': helpers.PGPKEYS['VALID_PGP_KEY1_PRV'], 'pgp_key_fingerprint': u'ECAF2235E78E71CD95365843C7B190543CAA7585', 'username': u'*****@*****.**', } # these are the same lines used in delivery_sched.py pgpobj = GLBPGP() pgpobj.load_key(helpers.PGPKEYS['VALID_PGP_KEY1_PRV']) with open(file_src, 'w+') as f: f.write(self.secret_content) f.seek(0) encrypted_object, length = pgpobj.encrypt_file(fake_receiver_desc['pgp_key_fingerprint'], f, file_dst) with open(file_dst, 'r') as f: self.assertEqual(str(pgpobj.gnupg.decrypt_file(f)), self.secret_content) pgpobj.destroy_environment()