> For the complete documentation index, see [llms.txt](https://infinitode-docs.gitbook.io/documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://infinitode-docs.gitbook.io/documentation/package-documentation/codesafe-package-documentation.md).

# CodeSafe Package Documentation

## Changelog

* **0.0.4 (Latest)**:
  * Major performance optimizations for encryption and decryption (\~10x speedup).
  * Optimized `safe_eval` by pre-filtering builtins and reducing internal overhead.
  * Added support for `bytes` input in `encrypt`.
  * Added support for file-like stream objects in `run` and `decrypt_to_file`.
  * Standardized error handling (replaced `print` with exceptions).
* **0.0.3**: Added a parameter for `allow_attributes` and set `immediate_termination` to be `True` by default for safer function calling.
* **0.0.2**: Removed unnecessary print statements and added error handling for most functions. Related to <https://github.com/Infinitode/CodeSafe/pull/1>. Thanks to [@0XC7R](https://github.com/0XC7R) for contributing!
* **0.0.1**: Initial release.

## Installation

You can install CodeSafe using PyPi, please make sure that you are using Python 3.6 or later before installing CodeSafe:

```bash
pip install codesafe
```

{% hint style="warning" %}
**CodeSafe** is an experimental library, and we're still running some tests on it. Please let us know if you encounter any issues or have an edge use case.
{% endhint %}

{% hint style="info" %}
**CodeSafe** is intended to quickly encrypt/decrypt code files, and run them (only for Python script files / other formats that Python understands) while in their encrypted form, but not as a means for powerful encryption, just for code obfuscation. We have also included a `safe_eval` function, that can safely evaluate expressions within a safe and controlled environment.
{% endhint %}

***

## Example Usage

### Safe Eval

```python
from codesafe import safe_eval

if __name__ == '__main__':
    # Run a normal, safe expression
    expression = "1 + 1"
    disallowed_expression = "os.getcwd()"

    result1 = safe_eval(expression, timeout=10, immediate_termination=True)
    result2 = safe_eval(disallowed_expression, timeout=10, immediate_termination=True)
```

{% hint style="info" %}
Attribute inspection is disabled when using `safe_eval`. You can read more about how to use `safe_eval` from [here](/documentation/package-documentation/codesafe-package-documentation/codesafe-reference/codesafe-functions.md#safe-eval).
{% endhint %}

### Encrypt & Run Code

```python
from codesafe import encrypt_to_file, decrypt_to_file, run

code = """
greetJohnny = "Hello Johnny!"

def greet_someone(greeting):
    print(greeting)

greet_someone(greetJohnny)
"""

# Encrypt the code
encrypted_file_path = "encrypted_code.encrypt"
encrypt_to_file(code, encrypted_file_path)

# Run the encrypted code
run(encrypted_file_path) # Hello Johnny!

# Decrypt code to another file
output_file = "decrypted_code.py"
decrypt_to_file(encrypted_file_path, output_file)
```
