|
# -*- coding: utf-8 -*-
from __future__ import division
import json
import random
from django_response import response
from logit import logit
from mch.models import BrandInfo, ModelImageInfo, ModelInfo
from utils.algorithm.b64 import b64_decrypt, b64_encrypt
from utils.algorithm.rsalg import rsa_decrypt, rsa_encrypt
# CIPHER_ALGORITHM = ('B64', 'RSA')
CIPHER_ALGORITHM = ('B64', )
CIPHER_PREFIX = {
'B64': 'alg1',
'RSA': 'alg2',
}
@logit(res=True)
def encrypt(request):
plaintext = request.POST.get('plaintext', '')
alg = random.choice(CIPHER_ALGORITHM)
if alg == 'B64':
ciphertext = b64_encrypt(plaintext)
elif alg == 'RSA':
ciphertext = rsa_encrypt(plaintext)
else:
ciphertext = plaintext
return response(200, data={
'ciphertext': u'%s+%s' % (CIPHER_PREFIX.get(alg, ''), ciphertext),
})
@logit(res=True)
def decrypt(request):
ciphertext = request.POST.get('ciphertext', '')
alg, ciphertext = ciphertext.split('+', 1)
if alg == CIPHER_PREFIX['B64']:
plaintext = b64_decrypt(ciphertext)
elif alg == CIPHER_PREFIX['RSA']:
plaintext = rsa_decrypt(ciphertext)
else:
plaintext = ciphertext
infos = json.loads(plaintext)
Brand = infos.get('Brand', '')
Model = infos.get('Model', '')
try:
logo_url = BrandInfo.objects.get(brand_name=Brand).brand_logo_url
except BrandInfo.DoesNotExist:
logo_url = ''
try:
model_imgs = ModelInfo.objects.get(model_name=Model).images
except ModelInfo.DoesNotExist:
model_imgs = []
return response(200, data={
'plaintext': plaintext,
'logo_url': logo_url,
'model_imgs': model_imgs,
})
|