#!/usr/bin/env python3 # ~ Empresa Libre # ~ Copyright (C) 2016-2019 Mauricio Baeza Servin (public@elmau.net) # ~ # ~ This program is free software: you can redistribute it and/or modify # ~ it under the terms of the GNU General Public License as published by # ~ the Free Software Foundation, either version 3 of the License, or # ~ (at your option) any later version. # ~ # ~ This program is distributed in the hope that it will be useful, # ~ but WITHOUT ANY WARRANTY; without even the implied warranty of # ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # ~ GNU General Public License for more details. # ~ # ~ You should have received a copy of the GNU General Public License # ~ along with this program. If not, see . import base64 import math from cryptography.fernet import Fernet from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes def round_up(value): return int(math.ceil(value)) def _get_key(password): digest = hashes.Hash(hashes.SHA256(), backend=default_backend()) digest.update(password.encode()) key = base64.urlsafe_b64encode(digest.finalize()) return key def encrypt(data, password): f = Fernet(_get_key(password)) return f.encrypt(data.encode()).decode() def decrypt(data, password): f = Fernet(_get_key(password)) return f.decrypt(data.encode()).decode()