-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
229 lines (184 loc) · 6.72 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import fastapi
from fastapi import HTTPException
import uvicorn
from pynacl import *
from keys import generate_public_key, generate_private_key
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from cryptodom import AES_encrypt, AES_decrypt
from rsacipher import *
from prime_euclid import *
from vs import *
app = fastapi.FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
#Base model for post endpoints of Nacl library
class Code(BaseModel):
key: str
text: str
class Naclhash(BaseModel):
message: str
detail: str
class NaclVerify(BaseModel):
password: str
hash: str
#Base model for post endpoints of Cryptodom library
class AES(BaseModel):
key: str
text: str
mode: str
#Base model for AES decryption of Cryptodom Library
class DES(BaseModel):
key: str
info: dict
mode: str
#Base model for post endpoints of HRSA (RSA)
class Hrsa(BaseModel):
text: str
key: int
n : int
#endpoint for getting encrypted message back using caesar cipher
@app.get('/caesarencrypt/{message}/{shift}/')
async def caesarencrypt(message: str, shift: int):
try:
return caesar_encrypt(message, shift)
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
#endpoint for getting encrypted message decrypted using caesar cipher
@app.get('/caesardecrypt/{cipher}/{shift}/')
async def caesardecrypt(cipher: str, shift: int):
try:
return caesar_decrypt(cipher, shift)
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
#endpoint for getting encrypted message back using vigenere cipher
@app.get('/vigenereencrypt/{message}/{key}/')
async def vigenereEncrypt(message: str, key: str):
try:
return vigenere_encrypt(message, key)
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
#endpoint for getting encrypted message decrypted back using vigenere cipher
@app.get('/vigeneredecrypt/{message}/{key}/')
async def vigeneredecrypt(message: str, key: str):
try:
return vigenere_decrypt(message, key)
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
# endpoint for getting hashed message back
@app.post('/nacl/hashing/')
async def hashing(naclhash: Naclhash):
result = nacl_hashing(naclhash.message, naclhash.detail)
return result
#endpoint for getting password hash
@app.post('/nacl/passwordhash/')
async def password_hashing(naclhash: Naclhash):
try:
result = get_password_hash(naclhash.message, naclhash.detail)
return result
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
#endpoint for verifying password with hash
@app.post('/nacl/verifypasswordhash/')
async def password_hashing(naclhash: NaclVerify):
try:
result = verify_password_hash(naclhash.password, naclhash.hash)
return result
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
#endpoint for nacl symmetric key encryption
@app.post('/nacl/encrypt/')
async def nacl_encrypt(code: Code):
try:
result = nacl_secret_key_encrypt_text(code.key, code.text)
return result
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
#endpoint for nacl symmetric key decryption
@app.post('/nacl/decrypt/')
async def nacl_decrypt(code: Code):
try:
result = nacl_secret_key_decrypt_text(code.key, base64.b64decode(code.text))
return result
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
#endpoint for aes encryption using cryptodom
@app.post('/cryptodom/aes/encrypt/')
async def aes_encrypt(aes: AES):
try:
result = AES_encrypt(aes.key, aes.text, aes.mode)
return result
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
#endpoint for aes decryption using cryptodom
@app.post('/cryptodom/aes/decrypt/')
async def aes_decrypt(des: DES):
try:
result = AES_decrypt(des.key, des.info, des.mode)
return result
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
#endpoint for getting a random prime number using a unique id provided in the url
@app.get('/hrsaprime/{ID}')
async def hrsaprime(ID: str):
try:
result = hrsa_prime(ID)
return result
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
#endpoint for getting a n bits prime number
@app.get('/nprime/{n}')
async def nprime(n: int):
try:
result = generate_prime(n)
return result
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
# endpoint exposes a function which
# computes and returns a list of k primes starting from a prime
# which is "start" length of bits and ending with a prime which has
# a length of "end" bits
@app.get('/kprime/{k}/{start}/{end}/')
async def nprime(k: int, start: int, end: int):
try:
result = generate_kprimes(k,start,end)
return result
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
# endpoint exposes the public key function of rsa
@app.get('/hrsa/publickey/{p1}/{p2}/')
async def hrsa_publickey(p1: int, p2: int):
try:
result = generate_public_key(p1,p2)
return result
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
# endpoint exposes the private key function of rsa
@app.get('/hrsa/privatekey/{e}/{p1}/{p2}/')
async def hrsa_privatekey(e: int, p1: int, p2: int):
try:
result = generate_private_key(e,p1,p2)
return result
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
# endpoint exposes the hrsacipher encryption function
@app.post('/hrsa/encryption/')
async def hrsa_encryption(hrsa: Hrsa):
try:
result = hrsa_encrypted(hrsa.text, hrsa.key, hrsa.n)
return result
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
# endpoint exposes the hrsacipher decryption function
@app.post('/hrsa/decryption/')
async def hrsa_decryption(hrsa: Hrsa):
try:
result = hrsa_decrypted(hrsa.text, hrsa.key, hrsa.n)
return result
except:
HTTPException(status_code=404, detail="Algorithm could not compute the desired result")
# uvicorn.run(app)