From 5cdbb0aaa7bf51a799ebd6e67c53937d4af18981 Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Sun, 3 Feb 2019 23:32:48 -0600 Subject: [PATCH] Agregar funciones para encriptar --- source/app/controllers/util.py | 3 -- source/app/controllers/utils.py | 50 +++++++++++++++++++++++++++++++++ source/app/models/main.py | 6 +++- source/app/settings.py | 2 +- 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 source/app/controllers/utils.py diff --git a/source/app/controllers/util.py b/source/app/controllers/util.py index 32c9768..3202ac7 100644 --- a/source/app/controllers/util.py +++ b/source/app/controllers/util.py @@ -23,7 +23,6 @@ import hashlib import io import json import locale -import math import mimetypes import os import re @@ -3766,6 +3765,4 @@ def parse_xml2(xml_str): return etree.fromstring(xml_str.encode('utf-8')) -def round_up(value): - return int(math.ceil(value)) diff --git a/source/app/controllers/utils.py b/source/app/controllers/utils.py new file mode 100644 index 0000000..8f46c9f --- /dev/null +++ b/source/app/controllers/utils.py @@ -0,0 +1,50 @@ +#!/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 +import os + +from cryptography.fernet import Fernet +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC + + +def round_up(value): + return int(math.ceil(value)) + + +def _get_key(password): + salt = os.urandom(16) + kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt, + iterations=100000, backend=default_backend()) + key = base64.urlsafe_b64encode(kdf.derive(password.encode())) + 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() + + diff --git a/source/app/models/main.py b/source/app/models/main.py index afcbcda..f8eb164 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -29,8 +29,12 @@ if __name__ == '__main__': parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, parent_dir) +# ~ v2 +from controllers import utils +# ~ v1 from controllers import util + from settings import log, DEBUG, VERSION, PATH_CP, COMPANIES, PRE, CURRENT_CFDI, \ INIT_VALUES, DEFAULT_PASSWORD, DECIMALES, IMPUESTOS, DEFAULT_SAT_PRODUCTO, \ CANCEL_SIGNATURE, PUBLIC, DEFAULT_SERIE_TICKET, CURRENT_CFDI_NOMINA, \ @@ -4336,7 +4340,7 @@ class Facturas(BaseModel): importe = round(cantidad * precio_final, DECIMALES) if use_packing: - product['empaques'] = util.round_up( + product['empaques'] = utils.round_up( cantidad / float(p.cantidad_empaque)) product['cantidad'] = cantidad diff --git a/source/app/settings.py b/source/app/settings.py index 5449698..74da420 100644 --- a/source/app/settings.py +++ b/source/app/settings.py @@ -48,7 +48,7 @@ except ImportError: DEBUG = DEBUG VERSION = '1.28.0' -EMAIL_SUPPORT = ('soporte@empresalibre.net',) +EMAIL_SUPPORT = ('soporte@empresalibre.mx',) TITLE_APP = '{} v{}'.format(TITLE_APP, VERSION) BASE_DIR = os.path.abspath(os.path.dirname(__file__))