change API to support upcoming --verify flag

This commit is contained in:
Brian Warner 2015-03-24 00:03:10 -07:00
parent af1e3c51ec
commit ed1809d521
5 changed files with 27 additions and 28 deletions

View File

@ -138,7 +138,7 @@ class Common:
assert len(key) == SecretBox.KEY_SIZE
box = SecretBox(key)
nonce = utils.random(SecretBox.NONCE_SIZE)
return box.encrypt(self.data, nonce)
return box.encrypt(data, nonce)
def _post_data(self, data):
post_data = json.dumps({"message": hexlify(data).decode("ascii")})
@ -167,9 +167,8 @@ class Common:
return HKDF(self.key, length, CTXinfo=purpose)
class Initiator(Common):
def __init__(self, appid, data, relay):
def __init__(self, appid, relay):
self.appid = appid
self.data = data
self.relay = relay
assert self.relay.endswith("/")
self.started = time.time()
@ -186,12 +185,12 @@ class Initiator(Common):
self._post_pake()
return self.code
def get_data(self):
def get_data(self, outbound_data):
key = self._get_pake([])
self.key = key
try:
outbound_key = self.derive_key(b"sender")
outbound_encrypted = self._encrypt_data(outbound_key, self.data)
outbound_encrypted = self._encrypt_data(outbound_key, outbound_data)
other_msgs = self._post_data(outbound_encrypted)
inbound_encrypted = self._get_data(other_msgs)
@ -207,9 +206,8 @@ class Initiator(Common):
class Receiver(Common):
def __init__(self, appid, data, relay):
def __init__(self, appid, relay):
self.appid = appid
self.data = data
self.relay = relay
assert self.relay.endswith("/")
self.started = time.time()
@ -239,7 +237,7 @@ class Receiver(Common):
idA=self.appid+":Initiator",
idB=self.appid+":Receiver")
def get_data(self):
def get_data(self, outbound_data):
assert self.code is not None
assert self.channel_id is not None
other_msgs = self._post_pake()
@ -248,7 +246,7 @@ class Receiver(Common):
try:
outbound_key = self.derive_key(b"receiver")
outbound_encrypted = self._encrypt_data(outbound_key, self.data)
outbound_encrypted = self._encrypt_data(outbound_key, outbound_data)
other_msgs = self._post_data(outbound_encrypted)
inbound_encrypted = self._get_data(other_msgs)

View File

@ -10,21 +10,21 @@ def receive_file(args):
# we're receiving
transit_receiver = TransitReceiver(transit_relay=args.transit_helper)
mydata = json.dumps({
"transit": {
"direct_connection_hints": transit_receiver.get_direct_hints(),
"relay_connection_hints": transit_receiver.get_relay_hints(),
},
}).encode("utf-8")
r = Receiver(APPID, mydata, args.relay_url)
r = Receiver(APPID, args.relay_url)
code = args.code
if not code:
code = r.input_code("Enter receive-file wormhole code: ",
args.code_length)
r.set_code(code)
mydata = json.dumps({
"transit": {
"direct_connection_hints": transit_receiver.get_direct_hints(),
"relay_connection_hints": transit_receiver.get_relay_hints(),
},
}).encode("utf-8")
try:
data = json.loads(r.get_data().decode("utf-8"))
data = json.loads(r.get_data(mydata).decode("utf-8"))
except WrongPasswordError as e:
print("ERROR: " + e.explain(), file=sys.stderr)
return 1

View File

@ -6,15 +6,15 @@ APPID = "lothar.com/wormhole/text-xfer"
def receive_text(args):
# we're receiving
data = json.dumps({"message": "ok"}).encode("utf-8")
r = Receiver(APPID, data, args.relay_url)
r = Receiver(APPID, args.relay_url)
code = args.code
if not code:
code = r.input_code("Enter receive-text wormhole code: ",
args.code_length)
r.set_code(code)
data = json.dumps({"message": "ok"}).encode("utf-8")
try:
them_bytes = r.get_data()
them_bytes = r.get_data(data)
except WrongPasswordError as e:
print("ERROR: " + e.explain(), file=sys.stderr)
return 1

View File

@ -12,6 +12,12 @@ def send_file(args):
assert os.path.isfile(filename)
transit_sender = TransitSender(transit_relay=args.transit_helper)
i = Initiator(APPID, args.relay_url)
code = i.get_code(args.code_length)
print("On the other computer, please run: wormhole receive-file")
print("Wormhole code is '%s'" % code)
print()
filesize = os.stat(filename).st_size
data = json.dumps({
"file": {
@ -24,13 +30,8 @@ def send_file(args):
},
}).encode("utf-8")
i = Initiator(APPID, data, args.relay_url)
code = i.get_code(args.code_length)
print("On the other computer, please run: wormhole receive-file")
print("Wormhole code is '%s'" % code)
print()
try:
them_bytes = i.get_data()
them_bytes = i.get_data(data)
except WrongPasswordError as e:
print("ERROR: " + e.explain(), file=sys.stderr)
return 1

View File

@ -9,13 +9,13 @@ def send_text(args):
message = args.text
data = json.dumps({"message": message,
}).encode("utf-8")
i = Initiator(APPID, data, args.relay_url)
i = Initiator(APPID, args.relay_url)
code = i.get_code(args.code_length)
print("On the other computer, please run: wormhole receive-text")
print("Wormhole code is: %s" % code)
print("")
try:
them_bytes = i.get_data()
them_bytes = i.get_data(data)
except WrongPasswordError as e:
print("ERROR: " + e.explain(), file=sys.stderr)
return 1