暂无描述

encrypt_views.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. import random
  4. from django_logit import logit
  5. from django_response import response
  6. from logs.models import MchInfoDecryptLogInfo, MchInfoEncryptLogInfo
  7. from mch.models import ActivityInfo, BrandInfo, ModelInfo
  8. from utils.algorithm.b64 import b64_decrypt, b64_encrypt
  9. from utils.algorithm.caesar import caesar_decrypt, caesar_encrypt
  10. from utils.algorithm.rsalg import rsa_decrypt, rsa_encrypt
  11. # CIPHER_ALGORITHM = ('CAESAR', 'B64', 'RSA')
  12. CIPHER_ALGORITHM = ('CAESAR', )
  13. CIPHER_PREFIX = {
  14. 'CAESAR': '0',
  15. 'B64': '1',
  16. 'RSA': '2',
  17. }
  18. @logit(res=True)
  19. def encrypt(request):
  20. plaintext = request.POST.get('plaintext', '')
  21. optor_id = request.POST.get('optor_id', '')
  22. # brand_id#model_id#distributor_id#sn#time
  23. # AAAA#AAAAAA#AAAAA#AAAAAAAAAAAAAA#180224
  24. brand_pk, model_pk, distributor_pk, sn, time = plaintext.split('#')
  25. mieli, created_at = MchInfoEncryptLogInfo.objects.get_or_create(plaintext=plaintext)
  26. if created_at:
  27. alg = random.choice(CIPHER_ALGORITHM)
  28. if alg == 'CAESAR':
  29. ciphertext = caesar_encrypt(plaintext)
  30. elif alg == 'B64':
  31. ciphertext = b64_encrypt(plaintext)
  32. elif alg == 'RSA':
  33. ciphertext = rsa_encrypt(plaintext)
  34. else:
  35. ciphertext = plaintext
  36. mieli.alg = alg
  37. mieli.ciphertext = ciphertext
  38. mieli.brand_pk = brand_pk
  39. mieli.model_pk = model_pk
  40. mieli.distributor_pk = distributor_pk
  41. mieli.sn = sn
  42. mieli.operator_id = optor_id
  43. mieli.save()
  44. return response(200, data={
  45. 'ciphertext': u'{prefix}+{cipherlen}+{ciphertext}'.format(
  46. prefix=CIPHER_PREFIX.get(mieli.alg, ''),
  47. cipherlen=len(mieli.ciphertext),
  48. ciphertext=mieli.ciphertext,
  49. ),
  50. })
  51. @logit(res=True)
  52. def decrypt(request):
  53. ciphertext = request.POST.get('ciphertext', '')
  54. prefix, cipherlen, ciphertext = ciphertext.split('+', 2)
  55. ciphertext = ciphertext[:int(cipherlen)]
  56. if prefix == CIPHER_PREFIX['CAESAR']:
  57. plaintext = caesar_decrypt(ciphertext)
  58. elif prefix == CIPHER_PREFIX['B64']:
  59. plaintext = b64_decrypt(ciphertext)
  60. elif prefix == CIPHER_PREFIX['RSA']:
  61. plaintext = rsa_decrypt(ciphertext)
  62. else:
  63. plaintext = ciphertext
  64. # brand_id#model_id#distributor_id#sn#time
  65. # AAAA#AAAAAA#AAAAA#AAAAAAAAAAAAAA#180224
  66. brand_pk, model_pk, distributor_pk, sn, time = plaintext.split('#')
  67. try:
  68. brand = BrandInfo.objects.get(pk=brand_pk)
  69. except BrandInfo.DoesNotExist:
  70. brand = None
  71. try:
  72. model = ModelInfo.objects.get(pk=model_pk)
  73. except ModelInfo.DoesNotExist:
  74. model = None
  75. mdli, created_at = MchInfoDecryptLogInfo.objects.get_or_create(ciphertext=ciphertext, defaults={
  76. 'brand_pk': brand_pk,
  77. 'model_pk': model_pk,
  78. 'distributor_pk': distributor_pk,
  79. 'sn': sn,
  80. 'decrypt_count': 1,
  81. })
  82. if not created_at:
  83. mdli.decrypt_count += 1
  84. mdli.save()
  85. act = ActivityInfo.objects.filter(status=True).order_by('-pk').first()
  86. has_unexpired_activity = True if act and act.has_unexpired_activity(model.model_uni_name) else False
  87. coupon_info = {
  88. 'coupon_expire_at': act.final_coupon_expire_at(created_at=None),
  89. 'coupon_value': act.coupon_value,
  90. } if has_unexpired_activity else {
  91. 'coupon_expire_at': '',
  92. 'coupon_value': 0,
  93. }
  94. return response(200, data={
  95. 'plaintext': plaintext,
  96. 'logo_url': brand.brand_logo_url if brand else '',
  97. 'model_imgs': model.images if model else [],
  98. 'goodsInfo': {
  99. 'BrandID': brand_pk,
  100. 'Brand': brand.brand_name if brand else '',
  101. 'ModelID': model_pk,
  102. 'Model': (model.model_full_name or model.model_name) if model else '',
  103. 'DistributorID': distributor_pk,
  104. 'SerialNo': sn,
  105. },
  106. 'has_unexpired_activity': has_unexpired_activity,
  107. 'coupon_info': coupon_info,
  108. })