Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow newer pycryptodome #9

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
54 changes: 54 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
sudo: required
language: python
addons:
apt_packages:
- pandoc
python:
- '2.7'
- '3.3'
- '3.4'
- '3.5'
- "3.6"

install:
- pip install pypandoc
- pip install .

script:
- python -m unittest discover tests/

branches:
only: master

after_success:
- git config --global user.email "[email protected]"
- git config --global user.name "Travis"
- git tag -a v$(python setup.py --version) -m "Travis build $TRAVIS_BUILD_NUMBER pushed
a tag."
- git push origin --tags
- git fetch origin

deploy:
- provider: pypi
skip_cleanup: true
distributions: sdist
user: "trustpilot"
password:
secure: lVpNewz7Xn9e8hy3s2TIIHsMEIWQ3AqPmQZYiM1hXin/2qOXfe65ekEyHACfDu8P/iD+4v3qgI7xOaXH5dcFFOu8/C8GQ7V6GKSOZ6h08W0YQN7/iv3GPAXAMIPYgn0DaA0JUwy838xAUG764zK/Xejlbj6/4Zkw6mbWUBu95Q65IeSdXWpk82L4GrVSbEAtuDgtDAyy1KpNG60Ijqo7A8IbaSeJzsozRiKhwQnRzKzaQGV3UJnA+MBmTRGpS6Tr0Mhr5W5RQcksvoffbOyJB2MDQPieo/7SNdQAJ/PKtEtthTBK1/iQsVnPsKOV35rY/h1wwz5YhZtNiaaGKSFmvhyzwmfhjl94rLDxl6yGuS4q0ioocezjFuanSZrMYSg6L2gCq29oqJfJd5bBEWmfJ73E26yarxRrfKjhjpB3qZtw3gNo1EgyAW8UKxUmDcdPygd8X8EQX3ytrrSMcK/dpOWccV5hTXawsbBUMsxzx76EtP8WZ48P9c4LXtTs1WX0NC7HtjXiMFsK+ZD4navkwFUdLNEyAnoGm4DJ/NAQp/70mgBCqUEt5tvmu5Yt2K9mGJ9yTPBqgffR2iiBTkX/v9v0yhv8Nb6gJhasD90/kqnAfbdLERJ1xqEsrIGzIZ1F0J5fUhuVcJiuatkjny/1J7x43AIFDn4KZZ18Ws4Zwzo=
on:
tags: false
branch: master
repo: trustpilot/python-authenticated-encryption
condition: $TRAVIS_PYTHON_VERSION = "2.7"
- provider: releases
skip_cleanup: true
api-key:
secure: WkiR5PDXisJF0WkMehpXtJ6G4ogk60m0N3eovcryTGdCMnfoFgzJPO52RwMUAWtEUep9mUbXKqmx2N/aGMXjHLCYCDTwO511r5rRRHiHLewCrlG3VbuFhtot+s0+Oy6xqoKZUH8o9FH405S2O2uTit9Jw+BUvgHsh5OC1bhk484UUz7wm71jo8Q9CDCiQ556DtBXDiEQ2ytFu79aFiE+nFxJqNPq6Kr55A0xFlJJuqdSh5+0jFIkWvT8GCa+YAuFeoksVjwfKvzs/5I/rSqtcCy85pu1lmUnRsvwU0TNOZXBvJfMdRsbG5UsD2WAYq1MhZHM24XKkHOwgfv2448BW0vKxU0yS0V/xPNWRjrnFhL8Mx69wXga8X0MGNJD4EGq6iFPpx35yS5TMWKQB3Fu+9j4iqfkj3GKFB3LnqaMCZSuVJ026uYlK5gQSyly+J8xt0Aj0SD+KDy4Jsnu6aE0nH64VoQaXbz0iutpLp27PDXvUCIHEg/Z4oFz9XGalI1Q/2mgLH+TcXNgECKBeorLtI7C759/usMTJi2WGQiOUynS1UxtveoKSGbjHmsip9yjBp0wkW/jUER5JMF0v1yNyPWQfQ11JRR+sOgvQvm4rLC5/fPrKQ60ZNZb2rIebF6LH2Nc78NbMo9BkdtpzhUqWF2crTLb/byOHvtZRj3nrm4=
file_glob: true
file: dist/*
on:
tags: false
branch: master
repo: trustpilot/python-authenticated-encryption
condition: $TRAVIS_PYTHON_VERSION = "2.7"

19 changes: 0 additions & 19 deletions .vscode/launch.json

This file was deleted.

1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include README.md
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Trustpilot authenticated encryption for python

[![Build Status](http://travis-ci.org/trustpilot/python-authenticated-encryption.svg?branch=master)](https://travis-ci.org/trustpilot/python-authenticated-encryption) [![Latest Version](https://img.shields.io/pypi/v/trustpilot_authenticated_encryption.svg)](https://pypi.python.org/pypi/trustpilot_authenticated_encryption) [![Python Support](https://img.shields.io/pypi/pyversions/trustpilot_authenticated_encryption.svg)](https://pypi.python.org/pypi/trustpilot_authenticated_encryption)

Library for authenticated encryption used with Trustpilot.

## Install
- `pip install git+https://github.com/trustpilot/[email protected]`
- Omit @{version} at the end to install the latest source version

```
pip install trustpilot_authenticated_encryption
```

## Usage
To encrypt a message
Expand Down
24 changes: 18 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
from setuptools import setup
from os import path


def read(fname):
return open(path.join(path.dirname(__file__), fname)).read()


try:
from pypandoc import convert
read_md = lambda f: convert(f, 'rst')
except ImportError:
print("warning: pypandoc module not found, could not convert Markdown to RST")
read_md = lambda f: open(f, 'r').read()

with open('README.md') as readme_file:
readme = readme_file.read()

requirements = [
'pycryptodome'
'pycryptodome>=3.6.6'
]

setup(
name='trustpilot_authenticated_encryption',
version='1.0.0',
version='1.1.0',
description="Library for authenticated encryption used with Trustpilot",
long_description=readme,
long_description=read_md('README.md'),
url='https://github.com/trustpilot/python-authenticated-encryption',
packages=['trustpilot_authenticated_encryption'],
package_dir={'trustpilot_authenticated_encryption': 'trustpilot_authenticated_encryption'},
include_package_data=True,
install_requires=requirements,
author='Trustpilot',
license='MIT'
license='MIT',
test_suite='tests'
)
12 changes: 12 additions & 0 deletions tests/test_encryption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import unittest
from trustpilot_authenticated_encryption.encryption import encrypt

class TestEncryption(unittest.TestCase):

def test_encrypt_messagelength(self):
message = 'this is the secret message'
encrypt_key = "g9hH6MkVnlKlGa5IG+5R/uKgyrCJxOsh5fXlwK0mjH0="
hash_key = "oGmd/bHHkd+N6P6lZQxyfikjU7c5P/mhWO/noCsERyY="
encrypted = encrypt(message.encode("utf-8"), encrypt_key, hash_key)

self.assertEqual(len(encrypted.decode("ascii")), 108)
9 changes: 0 additions & 9 deletions tests/test_trustpilot_authenticated_encryption.py

This file was deleted.

32 changes: 0 additions & 32 deletions trustpilot_authenticated_encryption/__init__.py
Original file line number Diff line number Diff line change
@@ -1,32 +0,0 @@
import hmac
import hashlib
import base64
from Crypto.Cipher import AES
from Crypto.Util import Padding

BLOCK_SIZE = 16


def encrypt(msg, encrypt_key, hash_key):
"""
Encrypt and hash a message.

Args:
param msg: Bytes to encrypt
param encrypt_key: Base64 encoded encryption key
param hash_key: Base64 encoded hash key

Returns:
Base 64 encoded message
"""
encrypt_key = base64.b64decode(encrypt_key)
hash_key = base64.b64decode(hash_key)

padded_msg = Padding.pad(msg, BLOCK_SIZE, style="pkcs7")

cipher = AES.new(encrypt_key, AES.MODE_CBC)
encrypted_msg = cipher.encrypt(padded_msg)

msg_hash = hmac.new(hash_key, cipher.iv + encrypted_msg, digestmod=hashlib.sha256).digest()

return base64.b64encode(cipher.iv + encrypted_msg + msg_hash)
32 changes: 32 additions & 0 deletions trustpilot_authenticated_encryption/encryption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import hmac
import hashlib
import base64
from Crypto.Cipher import AES
from Crypto.Util import Padding

BLOCK_SIZE = 16


def encrypt(msg, encrypt_key, hash_key):
"""
Encrypt and hash a message.

Args:
param msg: Bytes to encrypt
param encrypt_key: Base64 encoded encryption key
param hash_key: Base64 encoded hash key

Returns:
Base 64 encoded message
"""
encrypt_key = base64.b64decode(encrypt_key)
hash_key = base64.b64decode(hash_key)

padded_msg = Padding.pad(msg, BLOCK_SIZE, style="pkcs7")

cipher = AES.new(encrypt_key, AES.MODE_CBC)
encrypted_msg = cipher.encrypt(padded_msg)

msg_hash = hmac.new(hash_key, cipher.iv + encrypted_msg, digestmod=hashlib.sha256).digest()

return base64.b64encode(cipher.iv + encrypted_msg + msg_hash)