#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved. @AUTHOR: ASJRD018 @NAME: azoauth @software: PyCharm @DATE: 2020/1/14 16:45 @Version: python3.6 @MODIFY DECORD:ansjer dev @file: CommonService.py @Contact: chanjunkai@163.com """ # -*- coding: utf-8 -*- import datetime import time from pathlib import Path from random import Random import base64 import ipdb import simplejson as json from django.core import serializers from django.utils import timezone from pyipip import IPIPDatabase # 复用性且公用较高封装代码在这 class CommonService: # 生成随机数 @staticmethod def encrypt_data(randomlength=8, number=False): str = '' if number == False: characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \ 'tUuVvWwXxYyZz0123456789' else: characterSet = '0123456789' length = len(characterSet) - 1 random = Random() for index in range(randomlength): str += characterSet[random.randint(0, length)] return str # 加密 # @staticmethod def encrypt_pwd(self, userPwd): for i in range(1, 4): if i == 1: userPwd = self.RandomStr(3, False)+userPwd+self.RandomStr(3, False) userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8') if i == 2: userPwd = self.RandomStr(2, False)+str(userPwd)+self.RandomStr(2, False) userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8') if i == 3: userPwd = self.RandomStr(1, False)+str(userPwd)+self.RandomStr(1, False) userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8') return userPwd # 解密 @staticmethod def decode_pwd(password): for i in range(1, 4): if i == 1: # 第一次先解密 password = base64.b64decode(password) password = password.decode('utf-8') # 截去第一位,最后一位 password = password[1:-1] if i == 2: # 第2次先解密 password = base64.b64decode(password) password = password.decode('utf-8') # 去前2位,后2位 password = password[2:-2] if i == 3: # 第3次先解密 password = base64.b64decode(password) password = password.decode('utf-8') # 去前3位,后3位 password = password[3:-3] return password # 生成随机字符串 @staticmethod def RandomStr(randomlength=8, number=False): str = '' if number == False: characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \ 'tUuVvWwXxYyZz0123456789' else: characterSet = '0123456789' length = len(characterSet) - 1 random = Random() for index in range(randomlength): str += characterSet[random.randint(0, length)] return str # data = CommonService.encrypt_data(20) # print(data)