forked from orisi/orisi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastproto.py
108 lines (83 loc) · 2.61 KB
/
fastproto.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
import json
import requests
import base64
import time
import datetime
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
import logging
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.WARNING)
FASTCAST_API_URL = 'http://54.77.58.8?format=json'
headers = {'content-type': 'application/json'}
def decode_data(data):
return base64.decodestring(data)
def code_data(data):
base64.encodestring(data)
def sign(message, priv_b64):
priv = base64.decodestring(priv_b64)
key = RSA.importKey(priv)
signer = PKCS1_v1_5.new(key)
digest = SHA256.new()
digest.update(message)
sign = signer.sign(digest)
return base64.encodestring(sign)
def verify(message, signature_b64, pub_b64):
pub = base64.decodestring(pub_b64)
signature = base64.decodestring(signature_b64)
key = RSA.importKey(pub)
signer = PKCS1_v1_5.new(key)
digest = SHA256.new()
digest.update(message)
if signer.verify(digest, signature):
return True
return False
def constructMessage(priv, **kwargs):
"""
Constructing a message, with base64 encoding of body, and signing
"""
req = kwargs
# We do not want to sign base64 encoded string, but actual string
req['signature'] = sign(req['body'], priv)
req['body'] = base64.encodestring(req['body'])
payload = json.dumps(req)
return payload
def generateKey():
new_key = RSA.generate(1024)
public_key = new_key.publickey().exportKey("DER")
private_key = new_key.exportKey("DER")
public_key_base64 = base64.encodestring(public_key)
private_key_base64 = base64.encodestring(private_key)
return (public_key_base64, private_key_base64)
def sendMessage(payload):
"""
Sending a message via api gateway
"""
url = 'http://54.77.58.8?format=json'
r = requests.post(url, data=payload, headers=headers)
print r.text
return r.text
def getMessages():
url = 'http://54.77.58.8?format=json'
r = requests.get(url)
data = json.loads(r.text)
decoded_results = []
for req in data['results']:
try:
decoded_body = decode_data(req['body'])
req['body'] = decoded_body
if not verify(req['body'], req['signature'], req['source']):
continue
decoded_results.append(req)
except:
continue
data['results'] = decoded_results
return data
def broadcastMessage(body, pub, priv):
meta_request = {}
meta_request['source'] = pub
meta_request['channel'] = 0
meta_request['epoch'] = time.mktime(datetime.datetime.utcnow().timetuple())
meta_request['body'] = body
print sendMessage(constructMessage(priv, **meta_request))