1.api中添加Base64算法

2.apps中添加home,用于测试算法,后面将首页写到此处
This commit is contained in:
jayhgq 2024-09-09 23:30:04 +08:00
parent b28732d3ef
commit 32e82441c2
10 changed files with 70 additions and 4 deletions

View File

@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.staticfiles',
'apps.auth.apps.AuthConfig',
'apps.api.apps.ApiConfig',
'apps.home.apps.HomeConfig',
]
MIDDLEWARE = [

View File

@ -18,11 +18,13 @@ from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from apps.home import views as home_views
urlpatterns = [
# path('admin/', admin.site.urls),
# path('login', include('apps.login.urls'))
# path('auth', include('apps.auth.urls'))
# path('login', include('apps.login.urls')),
# path('auth', include('apps.auth.urls')),
path('home/', home_views.home, name='home'),
path('api/', include('apps.api.urls')),
path('auth/', include('apps.auth.urls'))
path('auth/', include('apps.auth.urls')),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

View File

@ -49,3 +49,37 @@ class Base64CypherClass:
"""
Base64的加解密算法最简单的加密方式可加密短的文字小图片小文件图片文件大小不宜超过10M
"""
def __init__(self, *args, **kwargs):
import importlib
self.base64 = importlib.import_module('base64')
self.os = importlib.import_module('os')
self.time = importlib.import_module('time')
@staticmethod
def base64_encode_str(self, s):
return self.base64.b64encode(s.encode('utf-8'))
@staticmethod
def base64_decode_str(self, s):
return self.base64.b64decode(s).decode('utf-8')
@staticmethod
def base64_encode_pic(self, pic):
if self.os.path.exists(pic):
with open(pic, 'rb') as f:
read_pic = open(pic, 'rb')
read_data = read_pic.read()
read_pic.close()
return self.base64.b64encode(read_data)
else:
return "图片路径不存在"
@staticmethod
def base64_decode_pic(self, pic_bs64):
if self.os.path.exists(f"upload/temp/pic{int(self.time.time())}.jpg"):
self.os.remove(f"upload/temp/pic{int(self.time.time())}.jpg")
with open(f"upload/temp/pic{int(self.time.time())}.jpg", 'wb') as f:
f.write(self.base64.b64decode(pic_bs64))
return 'upload/temp/pic.jpg'

0
apps/home/__init__.py Normal file
View File

3
apps/home/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
apps/home/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class HomeConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'apps.home'

View File

3
apps/home/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
apps/home/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

14
apps/home/views.py Normal file
View File

@ -0,0 +1,14 @@
from django.shortcuts import render, HttpResponse
from apps.api.common import CaesarCypherClass, Base64CypherClass
# Create your views here.
def home(request):
bs64 = Base64CypherClass()
s = request.GET.get('s')
s_encode = bs64.base64_encode_pic(bs64, s)
if s_encode != "图片路径不存在":
s_decode = bs64.base64_decode_pic(bs64, s_encode)
else:
s_decode = ''
return HttpResponse(f"加密:{s_encode}\n解密:{s_decode}")