# fernet-encrypt [![PyPI](https://img.shields.io/pypi/v/fernet-encrypt?label=CLI)](https://pypi.org/project/fernet-encrypt/) [![Docker](https://img.shields.io/docker/v/tysonholub/fernet-webapp/latest?label=API)](https://hub.docker.com/r/tysonholub/fernet-webapp/tags) [![PyPiStats](https://img.shields.io/pypi/dm/fernet-encrypt.svg)](https://pypistats.org/packages/fernet-encrypt) ![PyPi](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13-%2344CC11)

A [Fernet](https://cryptography.io/en/latest/fernet/) encryption CLI tool with a free, open source, remote [API](https://gitlab.com/tysonholub/fernet-webapp)

## Install

Use [pipx](https://pypa.github.io/pipx/) to install globally in an isolated python environment.

```bash
pipx install fernet-encrypt
```

## Usage

```
 Usage: fernet-encrypt [OPTIONS] COMMAND [ARGS]...

╭─ Options ───────────────────────────────────────────────────────────────────────────────────╮
│ --install-completion          Install completion for the current shell.                     │
│ --show-completion             Show completion for the current shell, to copy it or          │
│                               customize the installation.                                   │
│ --help                        Show this message and exit.                                   │
╰─────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ──────────────────────────────────────────────────────────────────────────────────╮
│ login              Sign up or Login to https://fernet-encrypt.tysonholub.com                │
│ get-keys           Sync remote keys to fernet-encrypt local key storage                     │
│ create-local-key   Create a new local storage Fernet key                                    │
│ encrypt            Encrypt stdin using local key storage or remote API keys                 │
│ decrypt            Decrypt stdin using local key storage or remote API keys                 │
╰─────────────────────────────────────────────────────────────────────────────────────────────╯
```

### login

Interactively login or create a new account

```
$ fernet-encrypt login
```

Or, pass a `--refresh-token` to generate a new access_token without logging in (useful for automation). A refresh_token can be pulled from the .fernet-encrypt file generated by logging in (in package install location)

```
$ fernet-encrypt login --refresh-token=your-token
```

### get-keys

Sync remote encryption keys to your local package

```
$ fernet-encrypt get-keys
```

Or, use `--stdout` to print the JSON response without syncing to your local package

```
$ fernet-encrypt get-keys --stdout
```

### create-fernet-key

Create a new local key for encryption

```
$ fernet-encrypt create-local-key
```

### encrypt

Encrypt stdin data using local key storage.

```
$ echo "some-data" | fernet-encrypt encrypt
```

Or, use `--remote` to use remote API keys to encrypt. You should pass a `--name` to more easily identify your keys in the web portal

```
$ echo "some-data" | fernet-encrypt encrypt --name "some-name" --remote
```

### decrypt

Decrypt stdin data using local key storage.

```
$ cat encrypted.out | fernet-encrypt decrypt
```

Or, use `--remote` to use remote API keys to decrypt.

```
$ cat encrypted.out | fernet-encrypt decrypt --remote
```

### full circle

You can redirect the output to a file or pipe it into another command

```
$ echo "some-data" | fernet-encrypt encrypt --name "some-name" --remote | base64 -w0 > b64.out
$ base64 -d b64.out | fernet-encrypt decrypt --remote
some-data
```

In the web portal you should see a filename `some-name`. If you upload the encryption data, you will download a file named `some-name` containing `some-data`

```
$ base64 -d b64.out > encrypted.out
```

## Dev Prerequisites

-   python 3.12
-   [pipx](https://pypa.github.io/pipx/), an optional tool for prerequisite installs
-   [poetry](https://github.com/python-poetry/poetry) (install globally with `pipx install poetry`)
-   [flake8](https://github.com/PyCQA/flake8) (install globally with `pipx install flake8`)
    -   [flake8-bugbear](https://github.com/PyCQA/flake8-bugbear) extension (install with `pipx inject flake8 flake8-bugbear`)
    -   [flake8-naming](https://github.com/PyCQA/pep8-naming) extension (install with `pipx inject flake8 pep8-naming`)
-   [black](https://github.com/psf/black) (install globally with `pipx install black`)
-   [pre-commit](https://github.com/pre-commit/pre-commit) (install globally with `pipx install pre-commit`)
-   [just](https://github.com/casey/just), a Justfile command runner

### Windows

Justfile support for Windows requires [cygwin](https://www.cygwin.com/). Once installed your `PATH` will need to be updated to resolve `cygpath.exe` (probably `C:\cygwin64\bin`). Justfile will forward any targets with shebangs starting with `/` to cygwin for execution.

Consider using a bash terminal through [WSL](https://ubuntu.com/desktop/wsl) instead.

## Updating python version:

-   Update python version in `Dev Prerequisites` above
-   Update \[tool.poetry.dependencies\] section of `pyproject.toml`
-   Update pyupgrade hook in `.pre-commit-config.yaml`
-   Update python version in `.gitlab-ci.yml`

## Justfile Targets

-   `install`: installs poetry dependencies and pre-commit git hooks
-   `update_boilerplate`: fetches and applies updates from the boilerplate remote
-   `test`: runs pytest with test coverage report

## Boilerplate

To support pulling updates from the [pyplate](git@gitlab.com:tysonholub/pyplate.git) python boilerplate, add the `boilerplate` git remote:

```bash
git remote add boilerplate git@gitlab.com:tysonholub/pyplate.git
```

Then moving forward, run `just update_boilerplate` to pull latest changes from the `boilerplate` remote. **NOTE**: you must keep the boilerplate remote history intact to successfully merge updates from the boilerplate remote.
