Use msgpack instead of pickle in the Heartbeat demo

pull/618/head
David Núñez 2018-12-09 19:40:09 +01:00 committed by Kieran Prasch
parent 356e18079a
commit b89a82fc50
3 changed files with 9 additions and 8 deletions

2
.gitignore vendored
View File

@ -33,6 +33,6 @@ variables*.yml
*ansible/inventory/*
.env
examples/heartbeat_demo/*.json
examples/heartbeat_demo/*.pickle
examples/heartbeat_demo/*.msgpack
examples/heartbeat_demo/doctor-files/
examples/heartbeat_demo/alicia-files/

View File

@ -2,7 +2,7 @@ import json
import os
import sys
import shutil
import pickle
import msgpack
import maya
import traceback
from timeit import default_timer as timer
@ -82,7 +82,7 @@ doctor.join_policy(label, alices_sig_pubkey)
# But first we need some encrypted data!
# Let's read the file produced by the heart monitor and unpack the MessageKits,
# which are the individual ciphertexts.
data = pickle.load(open("heart_data.pickle", "rb"))
data = msgpack.load(open("heart_data.msgpack", "rb"), raw=False)
message_kits = (UmbralMessageKit.from_bytes(k) for k in data['kits'])
# The doctor also needs to create a view of the Data Source from its public keys
@ -103,7 +103,7 @@ for message_kit in message_kits:
)
end = timer()
plaintext = pickle.loads(retrieved_plaintexts[0])
plaintext = msgpack.loads(retrieved_plaintexts[0], raw=False)
# Now we can get the heart rate and the associated timestamp,
# generated by the heart rate monitor.

View File

@ -1,11 +1,11 @@
import random
import time
import pickle
import msgpack
from nucypher.data_sources import DataSource
HEART_DATA_FILENAME = 'heart_data.pickle'
HEART_DATA_FILENAME = 'heart_data.msgpack'
# Alicia defined a label to categorize all her heart-related data ❤️
# All DataSources that produce this type of data will use this label.
@ -37,7 +37,7 @@ def generate_heart_rate_samples(policy_pubkey,
'timestamp': now,
}
plaintext = pickle.dumps(heart_rate_data)
plaintext = msgpack.dumps(heart_rate_data, use_bin_type=True)
message_kit, _signature = data_source.encapsulate_single_message(plaintext)
kit_bytes = message_kit.to_bytes()
@ -49,6 +49,7 @@ def generate_heart_rate_samples(policy_pubkey,
}
if save_as_file:
pickle.dump(data, open(HEART_DATA_FILENAME, "wb"))
with open(HEART_DATA_FILENAME, "wb") as file:
msgpack.dump(data, file, use_bin_type=True)
return data