-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
63 lines (50 loc) · 1.67 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import traceback
import toml
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from ff3 import FF3Cipher
from pydantic import BaseModel
import random
class Text(BaseModel):
text: str
config_path = "./config.toml"
cfg_read = toml.loads(open(config_path, 'r', encoding='utf-8').read())
key = cfg_read['secret']['key']
tweak = cfg_read['secret']['tweak']
alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
c = FF3Cipher.withCustomAlphabet(key, tweak, alphabet)
def random_string(length: int) -> str:
return ''.join(random.choices(alphabet, k=length))
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post('/encrypt/')
async def encrypt(text: Text):
try:
j_t = random_string(2) + ':'+text.text+':' + random_string(2)
texts = []
# split text for each 28 characters
for i in range(0, len(j_t), 28):
texts.append(j_t[i:i+28])
if len(texts[-1]) < 4:
texts[-1] = texts[-1].ljust(4, '0')
encrypted_texts = []
for t in texts:
encrypted_texts.append(c.encrypt(t))
return {"encrypted": ''.join(encrypted_texts)}
except Exception:
fm = traceback.format_exc()
print(fm)
return JSONResponse(status_code=500, content={
"detail": "Internal Server Error",
"message": fm
})
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, port=cfg_read['secret'].get('port', 5000), log_level="info")