CommonService.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: azoauth
  7. @software: PyCharm
  8. @DATE: 2020/1/14 16:45
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: CommonService.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. # -*- coding: utf-8 -*-
  15. import datetime
  16. import time
  17. from pathlib import Path
  18. from random import Random
  19. import base64
  20. import ipdb
  21. import simplejson as json
  22. from django.core import serializers
  23. from django.utils import timezone
  24. from pyipip import IPIPDatabase
  25. # 复用性且公用较高封装代码在这
  26. class CommonService:
  27. # 生成随机数
  28. @staticmethod
  29. def encrypt_data(randomlength=8, number=False):
  30. str = ''
  31. if number == False:
  32. characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \
  33. 'tUuVvWwXxYyZz0123456789'
  34. else:
  35. characterSet = '0123456789'
  36. length = len(characterSet) - 1
  37. random = Random()
  38. for index in range(randomlength):
  39. str += characterSet[random.randint(0, length)]
  40. return str
  41. # 加密
  42. # @staticmethod
  43. def encrypt_pwd(self, userPwd):
  44. for i in range(1, 4):
  45. if i == 1:
  46. userPwd = self.RandomStr(3, False)+userPwd+self.RandomStr(3, False)
  47. userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8')
  48. if i == 2:
  49. userPwd = self.RandomStr(2, False)+str(userPwd)+self.RandomStr(2, False)
  50. userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8')
  51. if i == 3:
  52. userPwd = self.RandomStr(1, False)+str(userPwd)+self.RandomStr(1, False)
  53. userPwd = base64.b64encode(str(userPwd).encode("utf-8")).decode('utf8')
  54. return userPwd
  55. # 解密
  56. @staticmethod
  57. def decode_pwd(password):
  58. for i in range(1, 4):
  59. if i == 1:
  60. # 第一次先解密
  61. password = base64.b64decode(password)
  62. password = password.decode('utf-8')
  63. # 截去第一位,最后一位
  64. password = password[1:-1]
  65. if i == 2:
  66. # 第2次先解密
  67. password = base64.b64decode(password)
  68. password = password.decode('utf-8')
  69. # 去前2位,后2位
  70. password = password[2:-2]
  71. if i == 3:
  72. # 第3次先解密
  73. password = base64.b64decode(password)
  74. password = password.decode('utf-8')
  75. # 去前3位,后3位
  76. password = password[3:-3]
  77. return password
  78. # 生成随机字符串
  79. @staticmethod
  80. def RandomStr(randomlength=8, number=False):
  81. str = ''
  82. if number == False:
  83. characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \
  84. 'tUuVvWwXxYyZz0123456789'
  85. else:
  86. characterSet = '0123456789'
  87. length = len(characterSet) - 1
  88. random = Random()
  89. for index in range(randomlength):
  90. str += characterSet[random.randint(0, length)]
  91. return str
  92. # data = CommonService.encrypt_data(20)
  93. # print(data)