Skip to content

Commit

Permalink
Add library functions (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmazan authored Sep 1, 2023
1 parent 1b0aff0 commit 7b8a8c8
Show file tree
Hide file tree
Showing 27 changed files with 760 additions and 216 deletions.
53 changes: 29 additions & 24 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
name: main

on: [push]
on: [ push ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7']
python-version: [ 3.7, 3.8, 3.9, "3.10" ]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install poetry
run: pipx install poetry

- uses: Gr1N/setup-poetry@v8
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: "poetry"

- name: Check dependencies
run: make doctor
- name: Install poetry dependencies
run: poetry install

- uses: actions/cache@v2
with:
path: .venv
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }}
- name: Check dependencies
run: make doctor

- name: Install dependencies
run: make install
- uses: actions/cache@v2
with:
path: .venv
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }}

- name: Check code
run: make check
- name: Install dependencies
run: make install

- name: Test code
run: make test
- name: Check code
run: make check

- name: Upload coverage
uses: codecov/codecov-action@v1
with:
fail_ci_if_error: true
- name: Test code
run: make test

# - name: Upload coverage
# uses: codecov/codecov-action@v1
# with:
# fail_ci_if_error: true
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 22.12.0
hooks:
- id: black
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.7.11
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ $(DEPENDENCIES): poetry.lock
@ rm -rf ~/Library/Preferences/pypoetry
@ poetry config virtualenvs.in-project true
poetry install
@ touch $@

ifndef CI
poetry.lock: pyproject.toml
poetry lock --no-update
@ touch $@
endif

.cache:
Expand Down
70 changes: 56 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
# Overview
![Horizontal Lockupblack](https://user-images.githubusercontent.com/293079/169453889-ae211c6c-7634-4ccd-8ca9-8970c2621b6f.png#gh-light-mode-only)
![Horizontal Lockup copywhite](https://user-images.githubusercontent.com/293079/169453892-895f637b-4633-4a14-b997-960c9e17579b.png#gh-dark-mode-only)

Sample project generated from Jace's Python Template.
# Anvil Encryption

This project was generated with [cookiecutter](https://github.com/audreyr/cookiecutter) using [jacebrowning/template-python](https://github.com/jacebrowning/template-python).

[![Unix Build Status](https://img.shields.io/github/actions/workflow/status/anvilco/python-anvil-encryption/main.yml?branch=main&label=linux)](https://github.com/anvilco/python-anvil-encryption/actions)
[![Windows Build Status](https://img.shields.io/appveyor/ci/anvilco/python-anvil-encryption.svg?label=windows)](https://ci.appveyor.com/project/anvilco/python-anvil-encryption)
[![Coverage Status](https://img.shields.io/codecov/c/gh/anvilco/python-anvil-encryption)](https://codecov.io/gh/anvilco/python-anvil-encryption)
[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/anvilco/python-anvil-encryption.svg)](https://scrutinizer-ci.com/g/anvilco/python-anvil-encryption)
[![PyPI License](https://img.shields.io/pypi/l/python-anvil-encryption.svg)](https://pypi.org/project/python-anvil-encryption)
[![PyPI Version](https://img.shields.io/pypi/v/python-anvil-encryption.svg)](https://pypi.org/project/python-anvil-encryption)
[![PyPI Downloads](https://img.shields.io/pypi/dm/python-anvil-encryption.svg?color=orange)](https://pypistats.org/packages/python-anvil-encryption)

This library is a small wrapper around the [`pypa/cryptography` library](https://cryptography.io/en/latest/).
It offers convenience methods for encrypting and decrypting arbitrary payloads in both AES and RSA.

For use encrypting / decrypting payloads in Anvil, you can generate an RSA keypair from
your [organization's settings page](https://www.useanvil.com/docs/api/getting-started#encryption).

[Anvil](www.useanvil.com/developers) provides easy APIs for all things paperwork.

1. [PDF filling API](https://www.useanvil.com/products/pdf-filling-api/) - fill out a PDF template with a web request
and structured JSON data.
2. [PDF generation API](https://www.useanvil.com/products/pdf-generation-api/) - send markdown or HTML and Anvil will
render it to a PDF.
3. [Etch E-sign with API](https://www.useanvil.com/products/etch/) - customizable, embeddable, e-signature platform with
an API to control the signing process end-to-end.
4. [Anvil Workflows (w/ API)](https://www.useanvil.com/products/workflows/) - Webforms + PDF + E-sign with a powerful
no-code builder. Easily collect structured data, generate PDFs, and request signatures.

Learn more about Anvil on our [Anvil developer page](https://www.useanvil.com/developers).

## Setup

Expand All @@ -34,10 +47,39 @@ $ poetry add python-anvil-encryption

## Usage

After installation, the package can be imported:
This usage example is also in a runnable form in the `examples/` directory.

```text
$ python
>>> import python-anvil-encryption
>>> python-anvil-encryption.__version__
```python
import os
from python_anvil_encryption import encryption

CURRENT_PATH = os.path.dirname(os.path.realpath(__file__))

# Keys could be read from a file (or preferably from environment variables)
public_key = None
private_key = None
with open(os.path.join(CURRENT_PATH, "keys/public.pub"), "rb") as pub_file:
public_key = pub_file.read()

with open(os.path.join(CURRENT_PATH, "./keys/private.pem"), "rb") as priv_file:
private_key = priv_file.read()

# RSA
message = b"Super secret message"
encrypted_message = encryption.encrypt_rsa(public_key, message)
decrypted_message = encryption.decrypt_rsa(private_key, encrypted_message)
assert decrypted_message == message
print(f"Are equal? {decrypted_message == message}")

# AES
aes_key = encryption.generate_aes_key()
aes_encrypted_message = encryption.encrypt_aes(aes_key, message)
# The aes key in the first parameter is required to be in a hex
# byte string format.
decrypted_message = encryption.decrypt_aes(
aes_key.hex().encode(),
aes_encrypted_message
)
assert decrypted_message == message
print(f"Are equal? {decrypted_message == message}")
```
5 changes: 0 additions & 5 deletions docs/about/changelog.md

This file was deleted.

1 change: 1 addition & 0 deletions docs/about/changelog.md
93 changes: 0 additions & 93 deletions docs/about/contributing.md

This file was deleted.

1 change: 1 addition & 0 deletions docs/about/contributing.md
22 changes: 0 additions & 22 deletions docs/about/license.md

This file was deleted.

1 change: 1 addition & 0 deletions docs/about/license.md
1 change: 1 addition & 0 deletions docs/index.md
3 changes: 3 additions & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mkdocs==1.3.1 ; python_full_version >= "3.7.2" and python_version < "3.12"
pygments==2.16.1 ; python_full_version >= "3.7.2" and python_version < "3.12"
jinja2==3.1.2 ; python_full_version >= "3.7.2" and python_version < "3.12"
38 changes: 38 additions & 0 deletions examples/decrypt_payload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
from python_anvil_encryption import encryption

CURRENT_PATH = os.path.dirname(os.path.realpath(__file__))


def main():
# Read your public and private keys
public_key = None
private_key = None
with open(os.path.join(CURRENT_PATH, "keys/public.pub"), "rb") as pub_file:
public_key = pub_file.read()

with open(os.path.join(CURRENT_PATH, "./keys/private.pem"), "rb") as priv_file:
private_key = priv_file.read()

# RSA
message = b"Super secret message"
encrypted_message = encryption.encrypt_rsa(public_key, message)
decrypted_message = encryption.decrypt_rsa(private_key, encrypted_message)
assert decrypted_message == message
print(f"Are equal? {decrypted_message == message}")

# AES
aes_key = encryption.generate_aes_key()
aes_encrypted_message = encryption.encrypt_aes(aes_key, message)
# The aes key in the first parameter is required to be in a hex
# byte string format.
decrypted_message = encryption.decrypt_aes(
aes_key.hex().encode(), aes_encrypted_message
)
assert decrypted_message == message
print(f"Are equal? {decrypted_message == message}")


if __name__ == "__main__":
main()
print(CURRENT_PATH)
28 changes: 28 additions & 0 deletions examples/keys/private.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BA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