Metadata-Version: 2.1
Name: sourceprotected
Version: 0.1.0
Summary: Encrypt Python source files and decrypt them on the fly during runtime
Author-email: Viicos <65306057+Viicos@users.noreply.github.com>
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Typing :: Typed
Requires-Python: >=3.8
Requires-Dist: cryptography>=35.0.0
Description-Content-Type: text/markdown

# sourceprotected

Encrypt Python source files and decrypt them on the fly during runtime.

[![Python versions](https://img.shields.io/badge/python-3.8%20%7C%203.9%20%7C%203.10%20%7C%203.11-blue.svg)](https://www.python.org/downloads/)
[![Code style: Black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://black.readthedocs.io/en/stable/)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

## Usage

## Installation

Using `pip`:

```sh
pip install sourceprotected
```

### Encrypting the source files

```sh
sourceprotected path/to/src(.py)
Using key: fHC79...
Using encryption time: 1694...
```

### Decrypt during runtime

With `sourceprotected` installed in your current environment, you can run/import encrypted files/modules as long as the `SOURCEPROTECTED_KEY` environment variable is set:

`encrypted.py`:

```python
# -*- coding: sourceprotected -*-

-----BEGIN SOURCEPROTECTED FILE-----
gAAAAABk-eJaJarzMWy70GTRkRi6kjaD-zJy
-fHX_0G5VHTZjEKxJUvFhVWfWpX_j3LydmJo
pn7YrAj5CSqqPWgxUY_dSk5ELA==
-----END SOURCEPROTECTED FILE-----
```

`main.py`:

```python
import encrypted  # Will decrypt encrypted.py on the fly.
```

Finally:

```sh
SOURCEPROTECTED_KEY=Bk92p... python -B main.py
```

## How does it work?

`sourceprotected` uses a custom [codec](https://docs.python.org/3/library/codecs.html) that will decrypt the file content using the `SOURCEPROTECTED_KEY` environment variable.

This codec is registered on startup by using a [`.pth`](https://docs.python.org/3/library/site.html) file.

Credits goes to https://github.com/asottile-archive/future-fstrings for the implementation idea.
