@@ -7,6 +7,7 @@ import random |
||
7 | 7 |
from django_logit import logit |
8 | 8 |
from django_response import response |
9 | 9 |
|
10 |
+from logs.models import MchInfoEncryptLogInfo |
|
10 | 11 |
from mch.models import BrandInfo, ModelInfo |
11 | 12 |
from utils.algorithm.b64 import b64_decrypt, b64_encrypt |
12 | 13 |
from utils.algorithm.caesar import caesar_decrypt, caesar_encrypt |
@@ -26,23 +27,40 @@ CIPHER_PREFIX = { |
||
26 | 27 |
@logit(res=True) |
27 | 28 |
def encrypt(request): |
28 | 29 |
plaintext = request.POST.get('plaintext', '') |
30 |
+ optor_id = request.POST.get('optor_id', '') |
|
29 | 31 |
|
30 |
- alg = random.choice(CIPHER_ALGORITHM) |
|
32 |
+ # brand_id#model_id#distributor_id#sn#time |
|
33 |
+ # AAAA#AAAAAA#AAAAA#AAAAAAAAAAAAAA#180224 |
|
34 |
+ brand_pk, model_pk, distributor_pk, sn, time = plaintext.split('#') |
|
31 | 35 |
|
32 |
- if alg == 'CAESAR': |
|
33 |
- ciphertext = caesar_encrypt(plaintext) |
|
34 |
- elif alg == 'B64': |
|
35 |
- ciphertext = b64_encrypt(plaintext) |
|
36 |
- elif alg == 'RSA': |
|
37 |
- ciphertext = rsa_encrypt(plaintext) |
|
38 |
- else: |
|
39 |
- ciphertext = plaintext |
|
36 |
+ mieli, created_at = MchInfoEncryptLogInfo.objects.get_or_create(plaintext=plaintext) |
|
37 |
+ |
|
38 |
+ if created_at: |
|
39 |
+ alg = random.choice(CIPHER_ALGORITHM) |
|
40 |
+ |
|
41 |
+ if alg == 'CAESAR': |
|
42 |
+ ciphertext = caesar_encrypt(plaintext) |
|
43 |
+ elif alg == 'B64': |
|
44 |
+ ciphertext = b64_encrypt(plaintext) |
|
45 |
+ elif alg == 'RSA': |
|
46 |
+ ciphertext = rsa_encrypt(plaintext) |
|
47 |
+ else: |
|
48 |
+ ciphertext = plaintext |
|
49 |
+ |
|
50 |
+ mieli.alg = alg |
|
51 |
+ mieli.ciphertext = ciphertext |
|
52 |
+ mieli.brand_pk = brand_pk |
|
53 |
+ mieli.model_pk = model_pk |
|
54 |
+ mieli.distributor_pk = distributor_pk |
|
55 |
+ mieli.sn = sn |
|
56 |
+ mieli.operator_id = optor_id |
|
57 |
+ mieli.save() |
|
40 | 58 |
|
41 | 59 |
return response(200, data={ |
42 | 60 |
'ciphertext': u'{prefix}+{cipherlen}+{ciphertext}'.format( |
43 |
- prefix=CIPHER_PREFIX.get(alg, ''), |
|
44 |
- cipherlen=len(ciphertext), |
|
45 |
- ciphertext=ciphertext, |
|
61 |
+ prefix=CIPHER_PREFIX.get(mieli.alg, ''), |
|
62 |
+ cipherlen=len(mieli.ciphertext), |
|
63 |
+ ciphertext=mieli.ciphertext, |
|
46 | 64 |
), |
47 | 65 |
}) |
48 | 66 |
|
@@ -3,7 +3,12 @@ |
||
3 | 3 |
from django.contrib import admin |
4 | 4 |
from django_admin import ReadOnlyModelAdmin |
5 | 5 |
|
6 |
-from logs.models import MchLogInfo |
|
6 |
+from logs.models import MchInfoEncryptLogInfo, MchLogInfo |
|
7 |
+ |
|
8 |
+ |
|
9 |
+class MchInfoEncryptLogInfoAdmin(ReadOnlyModelAdmin, admin.ModelAdmin): |
|
10 |
+ list_display = ('plaintext', 'alg', 'ciphertext', 'brand_pk', 'model_pk', 'distributor_pk', 'sn', 'operator_id', 'status', 'created_at', 'updated_at') |
|
11 |
+ list_filter = ('alg', 'brand_pk', 'model_pk', 'distributor_pk', 'operator_id', 'status') |
|
7 | 12 |
|
8 | 13 |
|
9 | 14 |
class MchLogInfoAdmin(ReadOnlyModelAdmin, admin.ModelAdmin): |
@@ -13,3 +18,4 @@ class MchLogInfoAdmin(ReadOnlyModelAdmin, admin.ModelAdmin): |
||
13 | 18 |
|
14 | 19 |
|
15 | 20 |
admin.site.register(MchLogInfo, MchLogInfoAdmin) |
21 |
+admin.site.register(MchInfoEncryptLogInfo, MchInfoEncryptLogInfoAdmin) |
@@ -0,0 +1,35 @@ |
||
1 |
+# -*- coding: utf-8 -*- |
|
2 |
+# Generated by Django 1.11.11 on 2018-06-07 10:21 |
|
3 |
+from __future__ import unicode_literals |
|
4 |
+ |
|
5 |
+from django.db import migrations, models |
|
6 |
+ |
|
7 |
+ |
|
8 |
+class Migration(migrations.Migration): |
|
9 |
+ |
|
10 |
+ dependencies = [ |
|
11 |
+ ('logs', '0002_auto_20180530_1122'), |
|
12 |
+ ] |
|
13 |
+ |
|
14 |
+ operations = [ |
|
15 |
+ migrations.CreateModel( |
|
16 |
+ name='MchInfoEncryptLogInfo', |
|
17 |
+ fields=[ |
|
18 |
+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |
|
19 |
+ ('status', models.BooleanField(db_index=True, default=True, help_text='Status', verbose_name='status')), |
|
20 |
+ ('created_at', models.DateTimeField(auto_now_add=True, help_text='Create Time', verbose_name='created_at')), |
|
21 |
+ ('updated_at', models.DateTimeField(auto_now=True, help_text='Update Time', verbose_name='updated_at')), |
|
22 |
+ ('plaintext', models.CharField(blank=True, db_index=True, help_text='\u5f85\u52a0\u5bc6\u5b57\u7b26\u4e32', max_length=64, null=True, unique=True, verbose_name='plaintext')), |
|
23 |
+ ('alg', models.CharField(blank=True, help_text='\u52a0\u5bc6\u7b97\u6cd5', max_length=16, null=True, verbose_name='alg')), |
|
24 |
+ ('ciphertext', models.CharField(blank=True, help_text='\u52a0\u5bc6\u5b57\u7b26\u4e32', max_length=64, null=True, verbose_name='ciphertext')), |
|
25 |
+ ('brand_pk', models.IntegerField(db_index=True, default=0, help_text='\u54c1\u724cPK', verbose_name='brand_pk')), |
|
26 |
+ ('model_pk', models.IntegerField(db_index=True, default=0, help_text='\u578b\u53f7PK', verbose_name='model_pk')), |
|
27 |
+ ('sn', models.CharField(blank=True, db_index=True, help_text='\u5e8f\u5217\u53f7', max_length=32, null=True, verbose_name='sn')), |
|
28 |
+ ('operator_id', models.CharField(blank=True, db_index=True, help_text='\u64cd\u4f5c\u5458\u552f\u4e00\u6807\u8bc6', max_length=32, null=True, verbose_name='operator_id')), |
|
29 |
+ ], |
|
30 |
+ options={ |
|
31 |
+ 'verbose_name': 'mchinfoencryptloginfo', |
|
32 |
+ 'verbose_name_plural': 'mchinfoencryptloginfo', |
|
33 |
+ }, |
|
34 |
+ ), |
|
35 |
+ ] |
@@ -0,0 +1,20 @@ |
||
1 |
+# -*- coding: utf-8 -*- |
|
2 |
+# Generated by Django 1.11.11 on 2018-06-07 10:29 |
|
3 |
+from __future__ import unicode_literals |
|
4 |
+ |
|
5 |
+from django.db import migrations, models |
|
6 |
+ |
|
7 |
+ |
|
8 |
+class Migration(migrations.Migration): |
|
9 |
+ |
|
10 |
+ dependencies = [ |
|
11 |
+ ('logs', '0003_mchinfoencryptloginfo'), |
|
12 |
+ ] |
|
13 |
+ |
|
14 |
+ operations = [ |
|
15 |
+ migrations.AddField( |
|
16 |
+ model_name='mchinfoencryptloginfo', |
|
17 |
+ name='distributor_pk', |
|
18 |
+ field=models.IntegerField(db_index=True, default=0, help_text='\u7ecf\u9500\u5546PK', verbose_name='distributor_pk'), |
|
19 |
+ ), |
|
20 |
+ ] |
@@ -6,6 +6,28 @@ from django_models_ext import BaseModelMixin, upload_file_url, upload_path |
||
6 | 6 |
from shortuuidfield import ShortUUIDField |
7 | 7 |
|
8 | 8 |
|
9 |
+class MchInfoEncryptLogInfo(BaseModelMixin): |
|
10 |
+ plaintext = models.CharField(_(u'plaintext'), max_length=64, blank=True, null=True, help_text=u'待加密字符串', db_index=True, unique=True) |
|
11 |
+ |
|
12 |
+ alg = models.CharField(_(u'alg'), max_length=16, blank=True, null=True, help_text=u'加密算法') |
|
13 |
+ ciphertext = models.CharField(_(u'ciphertext'), max_length=64, blank=True, null=True, help_text=u'加密字符串') |
|
14 |
+ |
|
15 |
+ brand_pk = models.IntegerField(_(u'brand_pk'), default=0, help_text=u'品牌PK', db_index=True) |
|
16 |
+ model_pk = models.IntegerField(_(u'model_pk'), default=0, help_text=u'型号PK', db_index=True) |
|
17 |
+ distributor_pk = models.IntegerField(_(u'distributor_pk'), default=0, help_text=u'经销商PK', db_index=True) |
|
18 |
+ |
|
19 |
+ sn = models.CharField(_(u'sn'), max_length=32, blank=True, null=True, help_text=u'序列号', db_index=True) |
|
20 |
+ |
|
21 |
+ operator_id = models.CharField(_(u'operator_id'), max_length=32, blank=True, null=True, help_text=u'操作员唯一标识', db_index=True) |
|
22 |
+ |
|
23 |
+ class Meta: |
|
24 |
+ verbose_name = _(u'mchinfoencryptloginfo') |
|
25 |
+ verbose_name_plural = _(u'mchinfoencryptloginfo') |
|
26 |
+ |
|
27 |
+ def __unicode__(self): |
|
28 |
+ return unicode(self.pk) |
|
29 |
+ |
|
30 |
+ |
|
9 | 31 |
class MchLogInfo(BaseModelMixin): |
10 | 32 |
log_id = ShortUUIDField(_(u'log_id'), max_length=32, help_text=u'日志唯一标识', db_index=True) |
11 | 33 |
log_file = models.FileField(_(u'log_file'), upload_to=upload_path, blank=True, null=True, help_text=u'日志文件') |
@@ -5,7 +5,7 @@ Pillow==5.0.0 |
||
5 | 5 |
StatusCode==1.0.0 |
6 | 6 |
TimeConvert==1.4.3 |
7 | 7 |
cryptography==1.5.2 |
8 |
-django-admin==1.2.4 |
|
8 |
+django-admin==1.2.6 |
|
9 | 9 |
django-cors-headers==2.2.0 |
10 | 10 |
django-curtail-uuid==1.0.4 |
11 | 11 |
django-detect==1.0.5 |