def do_main(source, dest, password, encrypt, fLOG=None):
    """
    Encrypt or decrypt of a file

    @param      source      source of files to encrypt or decrypt
    @param      dest        destination
    @param      password    password
    @param      encrypt     boolean, True to encrypt
    @param      fLOG        logging function
    """
    if not os.path.exists(source):
        raise FileNotFoundError(source)
    try:
        from pyquickhelper.filehelper import encrypt_stream, decrypt_stream
    except ImportError:
        folder = os.path.normpath(os.path.join(
            os.path.abspath(os.path.dirname(__file__)), "..", ".."))
        sys.path.append(folder)
        from pyquickhelper.filehelper import encrypt_stream, decrypt_stream

    if isinstance(password, str):
        password = bytes(password, encoding="ascii")

    if encrypt:
        encrypt_stream(key=password,
                       filename=source,
                       out_filename=dest,
                       chunksize=os.stat(source).st_size * 2 + 1)
    else:
        decrypt_stream(key=password,
                       filename=source,
                       out_filename=dest,
                       chunksize=os.stat(source).st_size * 2 + 1)
示例#2
0
    def test_encryption_file_size(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        try:
            import Cryptodome as skip__
        except ImportError:
            warnings.warn("pycryptodomex is not installed")
            return

        temp = get_temp_folder(__file__, "temp_encryption1")

        infile = os.path.abspath(__file__).replace(".pyc", ".py")
        outfile = os.path.join(temp, "out_crypted.enc")
        r = encrypt_stream(b"key0" * 4, infile, outfile, chunksize=16)
        assert r is None

        outfile2 = os.path.join(temp, "out_decrypted.enc")
        r = decrypt_stream(b"key0" * 4, outfile, outfile2, chunksize=16)
        assert r is None

        with open(infile, "rb") as f:
            inc = f.read()
        with open(outfile2, "rb") as f:
            ouc = f.read()
        self.assertEqual(inc, ouc)

        outfile3 = os.path.join(temp, "out_decrypted2.enc")
        r = decrypt_stream(b"key1" * 4, outfile, outfile3, chunksize=16)
        assert r is None
        with open(outfile3, "rb") as f:
            ouc3 = f.read()
        self.assertNotEqual(inc, ouc3)
示例#3
0
    def test_encryption_stream_fernet(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        infile = StreamIO(bytes([0, 1, 2, 3, 4]))
        outst = StreamIO()

        r = encrypt_stream("key0" * 8, infile, outst, algo="fernet")
        assert r is None

        enc = StreamIO(outst.getvalue())
        enc2 = StreamIO(outst.getvalue())
        outdec = StreamIO()
        r2 = decrypt_stream("key0" * 8, enc, outdec, algo="fernet")
        assert r2 is None

        self.assertEqual(infile.getvalue(), outdec.getvalue())

        outdec2 = StreamIO()
        try:
            r3 = decrypt_stream("key1" * 8, enc2, outdec2, algo="fernet")
        except Exception:
            return
        assert r3 is None
        self.assertNotEqual(infile.getvalue(), outdec2.getvalue())
示例#4
0
    def test_encryption_stream_fernet_chunck_size(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        infile = StreamIO(bytes(list(i % 255 for i in range(0, 10000))))
        outst = StreamIO()

        r = encrypt_stream("key0" * 8, infile, outst,
                           algo="fernet", chunksize=256)
        assert r is None

        enc = StreamIO(outst.getvalue())
        enc2 = StreamIO(outst.getvalue())
        outdec = StreamIO()
        r2 = decrypt_stream("key0" * 8, enc, outdec,
                            algo="fernet", chunksize=256)
        assert r2 is None

        self.assertEqual(infile.getvalue(), outdec.getvalue())

        outdec2 = StreamIO()
        try:
            r3 = decrypt_stream("key1" * 8, enc2, outdec2,
                                algo="fernet", chunksize=256)
        except Exception:
            return
        assert r3 is None
        self.assertNotEqual(infile.getvalue(), outdec2.getvalue())
示例#5
0
    def test_encryption_file_size(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        try:
            import Cryptodome as skip__
        except ImportError:
            warnings.warn("pycryptodomex is not installed")
            return

        temp = get_temp_folder(__file__, "temp_encryption1")

        infile = os.path.abspath(__file__).replace(".pyc", ".py")
        outfile = os.path.join(temp, "out_crypted.enc")
        r = encrypt_stream(b"key0" * 4, infile, outfile, chunksize=16)
        assert r is None

        outfile2 = os.path.join(temp, "out_decrypted.enc")
        r = decrypt_stream(b"key0" * 4, outfile, outfile2, chunksize=16)
        assert r is None

        with open(infile, "rb") as f:
            inc = f.read()
        with open(outfile2, "rb") as f:
            ouc = f.read()
        self.assertEqual(inc, ouc)

        outfile3 = os.path.join(temp, "out_decrypted2.enc")
        r = decrypt_stream(b"key1" * 4, outfile, outfile3, chunksize=16)
        assert r is None
        with open(outfile3, "rb") as f:
            ouc3 = f.read()
        self.assertNotEqual(inc, ouc3)
示例#6
0
    def test_encryption_stream(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        try:
            import Cryptodome as skip___
        except ImportError:
            warnings.warn("pycryptodomex is not installed")
            return

        infile = StreamIO(bytes([0, 1, 2, 3, 4]))
        outst = StreamIO()

        r = encrypt_stream(b"key0" * 4, infile, outst)
        assert r is None

        enc = StreamIO(outst.getvalue())
        enc2 = StreamIO(outst.getvalue())
        outdec = StreamIO()
        r2 = decrypt_stream(b"key0" * 4, enc, outdec)
        assert r2 is None

        self.assertEqual(infile.getvalue(), outdec.getvalue())

        outdec2 = StreamIO()
        r3 = decrypt_stream(b"key1" * 4, enc2, outdec2)
        assert r3 is None
        self.assertNotEqual(infile.getvalue(), outdec2.getvalue())
示例#7
0
def do_main(source, dest, password, encrypt, fLOG=None):
    """
    Encrypt or decrypt of a file

    @param      source      source of files to encrypt or decrypt
    @param      dest        destination
    @param      password    password
    @param      encrypt     boolean, True to encrypt
    @param      fLOG        logging function
    """
    if not os.path.exists(source):
        raise FileNotFoundError(source)
    try:
        from pyquickhelper.filehelper import encrypt_stream, decrypt_stream
    except ImportError:
        folder = os.path.normpath(
            os.path.join(os.path.abspath(os.path.dirname(__file__)), "..",
                         ".."))
        sys.path.append(folder)
        from pyquickhelper.filehelper import encrypt_stream, decrypt_stream

    if sys.version_info[0] >= 3 and isinstance(password, str):
        password = bytes(password, encoding="ascii")

    if encrypt:
        encrypt_stream(key=password,
                       filename=source,
                       out_filename=dest,
                       chunksize=os.stat(source).st_size * 2 + 1)
    else:
        decrypt_stream(key=password,
                       filename=source,
                       out_filename=dest,
                       chunksize=os.stat(source).st_size * 2 + 1)
示例#8
0
    def test_encryption_stream_fernet(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        infile = StreamIO(bytes([0, 1, 2, 3, 4]))
        outst = StreamIO()

        r = encrypt_stream("key0" * 8, infile, outst, algo="fernet")
        assert r is None

        enc = StreamIO(outst.getvalue())
        enc2 = StreamIO(outst.getvalue())
        outdec = StreamIO()
        r2 = decrypt_stream("key0" * 8, enc, outdec, algo="fernet")
        assert r2 is None

        self.assertEqual(infile.getvalue(), outdec.getvalue())

        outdec2 = StreamIO()
        try:
            r3 = decrypt_stream("key1" * 8, enc2, outdec2, algo="fernet")
        except Exception:
            return
        assert r3 is None
        self.assertNotEqual(infile.getvalue(), outdec2.getvalue())
示例#9
0
    def test_encryption_stream(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        try:
            import Cryptodome as skip___
        except ImportError:
            warnings.warn("pycryptodomex is not installed")
            return

        infile = StreamIO(bytes([0, 1, 2, 3, 4]))
        outst = StreamIO()

        r = encrypt_stream(b"key0" * 4, infile, outst)
        assert r is None

        enc = StreamIO(outst.getvalue())
        enc2 = StreamIO(outst.getvalue())
        outdec = StreamIO()
        r2 = decrypt_stream(b"key0" * 4, enc, outdec)
        assert r2 is None

        self.assertEqual(infile.getvalue(), outdec.getvalue())

        outdec2 = StreamIO()
        r3 = decrypt_stream(b"key1" * 4, enc2, outdec2)
        assert r3 is None
        self.assertNotEqual(infile.getvalue(), outdec2.getvalue())
示例#10
0
    def test_import_exam(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if is_travis_or_appveyor():
            # no password
            return

        with warnings.catch_warnings():
            warnings.simplefilter('ignore', DeprecationWarning)
            import keyring
        temp = get_temp_folder(__file__, "temp_import_exam")
        pwd = keyring.get_password("exam", "ensae_teaching_cs,key")
        pwd += "*" * (16 - len(pwd))
        pwd = pwd.encode("ascii")
        this = os.path.abspath(os.path.dirname(module_file))
        dst = os.path.join(this, "encrypted", "cryptcode_exam_2016.crypted")
        assert os.path.exists(dst)
        decr = os.path.join(temp, "onemod.py")
        decrypt_stream(pwd, dst, decr)
        assert os.path.exists(decr)
        fLOG("importing")
        sys.path.append(temp)
        import onemod as temp_module
        del sys.path[-1]
        answer = temp_module.build(120)
        exp = """***      H
                H ***    H
                ***      H
                ***      H
                H ***    H
                ***      H
                ***      H
                H ***    H
                ***      H
                ***      H
                H ***    H
                ***      H""".replace("                ", "")
        self.assertEqual(answer, exp)
    def test_import_exam(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if is_travis_or_appveyor():
            # no password
            return

        with warnings.catch_warnings():
            warnings.simplefilter('ignore', DeprecationWarning)
            import keyring
        temp = get_temp_folder(__file__, "temp_import_exam")
        pwd = keyring.get_password("exam", "ensae_teaching_cs,key")
        pwd += "*" * (16 - len(pwd))
        pwd = pwd.encode("ascii")
        this = os.path.abspath(os.path.dirname(module_file))
        dst = os.path.join(this, "encrypted", "cryptcode_exam_2016.crypted")
        assert os.path.exists(dst)
        decr = os.path.join(temp, "onemod.py")
        decrypt_stream(pwd, dst, decr)
        assert os.path.exists(decr)
        fLOG("importing")
        sys.path.append(temp)
        import onemod as temp_module
        del sys.path[-1]
        answer = temp_module.build(120)
        exp = """***      H
                H ***    H
                ***      H
                ***      H
                H ***    H
                ***      H
                ***      H
                H ***    H
                ***      H
                ***      H
                H ***    H
                ***      H""".replace("                ", "")
        self.assertEqual(answer, exp)
示例#12
0
    def test_encryption_stream_fernet_chunck_size(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        infile = StreamIO(bytes(list(i % 255 for i in range(0, 10000))))
        outst = StreamIO()

        r = encrypt_stream("key0" * 8,
                           infile,
                           outst,
                           algo="fernet",
                           chunksize=256)
        assert r is None

        enc = StreamIO(outst.getvalue())
        enc2 = StreamIO(outst.getvalue())
        outdec = StreamIO()
        r2 = decrypt_stream("key0" * 8,
                            enc,
                            outdec,
                            algo="fernet",
                            chunksize=256)
        assert r2 is None

        self.assertEqual(infile.getvalue(), outdec.getvalue())

        outdec2 = StreamIO()
        try:
            r3 = decrypt_stream("key1" * 8,
                                enc2,
                                outdec2,
                                algo="fernet",
                                chunksize=256)
        except Exception:
            return
        assert r3 is None
        self.assertNotEqual(infile.getvalue(), outdec2.getvalue())
示例#13
0
    def test_encryption_bytes(self):
        fLOG(__file__,
             self._testMethodName,
             OutputPrint=__name__ == "__main__")

        try:
            import Cryptodome as skip__
        except ImportError:
            warnings.warn("pycryptodomex is not installed")
            return

        infile = bytes([0, 1, 2, 3, 4])
        r = encrypt_stream(b"key0" * 4, infile)
        assert r is not None

        r2 = decrypt_stream(b"key0" * 4, r)
        assert r2 is not None

        self.assertEqual(infile, r2)

        r3 = decrypt_stream(b"key1" * 4, r)
        assert r3 is not None
        self.assertNotEqual(infile, r3)
示例#14
0
    def test_encryption_bytes(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        try:
            import Cryptodome as skip__
        except ImportError:
            warnings.warn("pycryptodomex is not installed")
            return

        infile = bytes([0, 1, 2, 3, 4])
        r = encrypt_stream(b"key0" * 4, infile)
        assert r is not None

        r2 = decrypt_stream(b"key0" * 4, r)
        assert r2 is not None

        self.assertEqual(infile, r2)

        r3 = decrypt_stream(b"key1" * 4, r)
        assert r3 is not None
        self.assertNotEqual(infile, r3)
示例#15
0
def decrypt_dataframe(infile, password=None, sep="\t", encoding="utf8", **kwargs):
    """
    Read an encrypted dataframe.

    @param      infile      filename
    @param      password    password
    @param      sep         separator
    @param      encoding    encoding
    @param      kwargs      others options for `read_csv <>`_
    @return                 dataframe
    """
    password = get_password_from_keyring_or_env(password)
    data = decrypt_stream(password, infile)
    st = io.BytesIO(data)
    df = pandas.read_csv(st, sep=sep, encoding="utf8", **kwargs)
    return df