-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
69 lines (53 loc) · 1.58 KB
/
server.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
import falcon
import logging
from GhettoYubiHSM import GhettoYubiHSM
FORMAT = '%(asctime)-15s %(levelname)s %(funcName)s(%(lineno)d): %(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
LOG = logging.getLogger(__name__)
class EncryptResource:
"""
Resource to encrypt plaintext and return the ciphertext.
"""
def on_post(self, req, resp):
"""
Handle HTTP POST requests.
Args:
req: the request object.
resp: the response object.
"""
pt = req.stream.read()
LOG.debug("plaintext: %s" % pt)
ct = self.encrypt(pt)
LOG.debug("ciphertext: %s" % ct)
resp.body = ct
def encrypt(self, pt):
"""
This method will "encrypt" the provided plaintext value.
"""
hsm = GhettoYubiHSM()
return hsm.encrypt(pt)
class DecryptResource:
"""
Resource to decrypt ciphertext and return the plaintext.
"""
def on_post(self, req, resp):
"""
Handle HTTP POST requests.
Args:
req: the request object.
resp: the response object.
"""
ct = req.stream.read()
LOG.debug("ciphertext: %s" % ct)
pt = self.decrypt(ct)
LOG.debug("plaintext: %s" % pt)
resp.body = pt
def decrypt(self, ct):
"""
This method will "decrypt" the provided ciphertext value.
"""
hsm = GhettoYubiHSM()
return hsm.decrypt(ct)
app = falcon.API()
app.add_route('/decrypt', DecryptResource())
app.add_route('/encrypt', EncryptResource())