From 85983492394c325a1ce8c3412027809ba5d95f65 Mon Sep 17 00:00:00 2001 From: caolinan <1325380061@qq.com> Date: Thu, 24 May 2018 11:32:23 +0800 Subject: [PATCH] fix bug: setup:readme.md util:encrypt --- setup.py | 2 +- ulordapi/config.py | 14 +++++++------- ulordapi/user.py | 10 ++++++++-- ulordapi/utils.py | 37 +++++++++++++++++++++++++++++-------- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/setup.py b/setup.py index ff76e6e..941afff 100644 --- a/setup.py +++ b/setup.py @@ -47,7 +47,7 @@ base_dir = os.path.abspath(os.path.dirname(__file__)) # Get the long description from the README file -with open(os.path.join(base_dir, 'README.md'), 'rb') as f: +with open(os.path.join(base_dir, 'read.md'), 'rb') as f: long_description = f.read().decode('utf-8') diff --git a/ulordapi/config.py b/ulordapi/config.py index 2c6d3c5..68f9212 100644 --- a/ulordapi/config.py +++ b/ulordapi/config.py @@ -1,11 +1,11 @@ # coding=utf-8 +# Copyright (c) 2016-2018 The Ulord Core Developers # @File : config.py # @Author: PuJi # @Date : 2018/5/16 0016 - +# @Description : Second import.Just after utils import os, json, logging, io, time -from ulordapi.utils import fileHelper from ulordapi import utils @@ -87,8 +87,9 @@ def edit(self, data): baseconfig = Config( - version="0.0.1", - config_file=os.path.join(ROOTPATH, 'config') + version="0.0.1", + Debug=True, + config_file=os.path.join(ROOTPATH, 'config') ) @@ -159,14 +160,13 @@ def edit(self, data): # encryption # utilspath = os.path.join(os.getcwd(), 'utils'), - pubkeypath=os.path.join(os.path.join(os.getcwd(), 'utils'), 'public.pem'), - privkeypath=os.path.join(os.path.join(os.getcwd(), 'utils'), 'private.pem'), + pubkeypath=os.path.join(os.getcwd(), 'public.pem'), + privkeypath=os.path.join(os.getcwd(), 'private.pem'), ) dbconfig = Config( IsCreated=False, - Debug=True, SECRET_KEY="ulord platform is good", SQLALCHEMY_DATABASE_URI='sqlite:///sqlite.db', SQLALCHEMY_COMMIT_ON_TEARDOWN=True, diff --git a/ulordapi/user.py b/ulordapi/user.py index 756fa78..621750e 100644 --- a/ulordapi/user.py +++ b/ulordapi/user.py @@ -205,8 +205,14 @@ def __init__(self, appkey, secret): Developer.__init__(self, appkey, secret) self.log = logging.getLogger("Developer2:") self.log.info("Developer2 init") - self.pripath = os.path.join(os.getcwd(), 'private.pem') - self.pubpath = os.path.join(os.getcwd(), 'public.pem') + if webconfig.get('privkeypath'): + self.pripath = webconfig.get('privkeypath') + else: + self.pripath = os.path.join(os.getcwd(), 'private.pem') + if webconfig.get('pubkeypath'): + self.pubpath = webconfig.get('pubkeypath') + else: + self.pubpath = os.path.join(os.getcwd(), 'public.pem') self.rsahelper = utils.RSAHelper(self.pubpath, self.pripath) def get_purearg(self, arg): diff --git a/ulordapi/utils.py b/ulordapi/utils.py index 79db0ea..1ea5744 100644 --- a/ulordapi/utils.py +++ b/ulordapi/utils.py @@ -3,6 +3,7 @@ # @File : utils.py # @Author: Ulord_PuJi # @Date : 2018/5/18 0018 +# @Description: project import first.Alone. import re, os, hashlib, base64, logging, json, codecs, collections @@ -11,6 +12,9 @@ from Crypto import Random +RANDOM_GENERATOR=Random.new().read + + def isCellphone(number): """ check if a cellphone @@ -97,6 +101,7 @@ def __init__(self, public_path, private_path): else: self.generate() self.load() + # update public pem path and private pem path def generate(self): """ @@ -130,20 +135,36 @@ def _encry(self, comment): :type comment: str :return: encrypted information """ - cipher = PKCS1_v1_5.new(self.pubkey) - return (cipher.encrypt(comment)) + pass def encry(self, pubkey, comment): - cipher = PKCS1_v1_5.new(pubkey) - return (cipher.encrypt(comment)) + # cipher = PKCS1_v1_5.new(pubkey) + # return (cipher.decrypt(base64.b64decode(comment), RANDOM_GENERATOR)) + pass def _decrypt(self, message): - pass + """ + decrypt message + + :param message: need to be decrypted + :type message: str + :return: decrypted message + """ + cipher = PKCS1_v1_5.new(self.privkey) + return (cipher.decrypt(base64.b64decode(message), RANDOM_GENERATOR)) def decrypt(self, prikey, message): - # cipher = PKCS1_v1_5.new(prikey) - # return (cipher.decrypt(base64.b64decode(message), RANDOM_GENERATOR)) - pass + """ + using private key to decrypt message + + :param prikey: private key + :type prikey: str + :param message: need to be decrypted + :type message: str + :return: decrypted message + """ + cipher = PKCS1_v1_5.new(prikey) + return (cipher.decrypt(base64.b64decode(message), RANDOM_GENERATOR)) class FileHelper():