本文整理汇总了Python中anon_crypto.AnonCrypto类的典型用法代码示例。如果您正苦于以下问题:Python AnonCrypto类的具体用法?Python AnonCrypto怎么用?Python AnonCrypto使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AnonCrypto类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: recv_interest_voucher
def recv_interest_voucher(self, data):
msg, key = marshal.loads(data)
(nonce, leader_ip, leader_gui_port, leader_com_port) = marshal.loads(msg)
self.DEBUG(
"Start round voucher from %s:%s, communicating at port %s" % (leader_ip, leader_gui_port, leader_com_port)
)
# get the path to the file you want to share
self.emit(SIGNAL("getSharedFilename()"))
verified = self.verify(leader_ip, leader_gui_port, data)
""" generate temporary keys for this round so leader can aggregate """
self.gen_temp_keys()
temp1_str = AnonCrypto.pub_key_to_str(self.pubgenkey1)
temp2_str = AnonCrypto.pub_key_to_str(self.pubgenkey2)
""" default to random file of 128 bytes if you don't have anything to share """
if verified:
if os.path.exists(self.shared_filename):
self.DEBUG("You are sharing file %s" % (self.shared_filename))
else:
self.DEBUG("Not a valid file path, continuing without sharing...")
# respond with your interest
self.DEBUG("Verified leader, participating as %s:%s at port %s" % (self.ip, self.gui_port, self.com_port))
response = marshal.dumps((nonce, self.ip, self.gui_port, self.com_port, temp1_str, temp2_str))
cipher = AnonCrypto.sign_with_key(self.privKey, response)
AnonNet.send_to_addr(leader_ip, int(leader_gui_port), marshal.dumps(("interested", cipher)))
else:
self.DEBUG("Unkown leader, opting out...")
示例2: unpickle_pub_keys
def unpickle_pub_keys(self, msgs):
""" Leader uses this method to unpack keys from other nodes """
addrs = []
key_dict = {}
key_dict[self.id] = (
self.key_from_file(1),
AnonCrypto.sign(self.id, self.key1, self.key_from_file(2)))
for data in msgs:
(rem_id, rem_round, rem_ip, rem_port,
rem_key1, rem_key2) = marshal.loads(data)
self.debug("Unpickled msg from node %d" % (rem_id))
if rem_round != self.round_id:
raise RuntimeError, "Mismatched round numbers! (mine: %d, other: %d)" % (
self.round_id, rem_round)
k1 = AnonCrypto.pub_key_from_str(rem_key1)
self.pub_keys[rem_id] = (k1, k1)
k2 = AnonCrypto.pub_key_from_str(AnonCrypto.verify(self.pub_keys, rem_key2))
self.pub_keys[rem_id] = (k1, k2)
addrs.append((rem_ip, rem_port))
key_dict[rem_id] = (rem_key1, rem_key2)
return (marshal.dumps((self.round_id, key_dict)), addrs)
示例3: phase0b_msg
def phase0b_msg(self):
""" Message the leader sends to all other nodes. """
newdict = {}
for i in xrange(0, self.n_nodes):
k1, k2 = self.pub_keys[i]
newdict[i] = (AnonCrypto.pub_key_to_str(k1), AnonCrypto.pub_key_to_str(k2))
return marshal.dumps((self.round_id, newdict))
示例4: initialize_keys
def initialize_keys(self):
self.advance_phase()
for index, participant in enumerate(self.participants_vector):
msg, key = marshal.loads(participant[0])
(nonce, interest_ip, interest_gui_port, interest_com_port, pubkey1_str, pubkey2_str) = marshal.loads(msg)
k1 = AnonCrypto.pub_key_from_str(pubkey1_str)
k2 = AnonCrypto.pub_key_from_str(pubkey2_str)
self.pub_keys[index] = (k1, k2)
self.info('Unpickled public keys')
示例5: generate_keys
def generate_keys(self):
info("Generating keypair, please wait...")
self.key1 = AnonCrypto.random_key(self.key_len)
self.key2 = AnonCrypto.random_key(self.key_len)
self.save_pub_key(self.key1, 1)
self.save_pub_key(self.key2, 2)
self.pub_keys[self.id] = (
M2Crypto.RSA.load_pub_key(self.key_filename(1)),
M2Crypto.RSA.load_pub_key(self.key_filename(2)))
示例6: run_phase4
def run_phase4(self):
self.advance_phase()
if self.am_leader():
self.debug("Leader broadcasting ciphers to all nodes")
self.broadcast_to_all_nodes(marshal.dumps(self.final_ciphers))
self.debug("Cipher set len %d" % (len(self.final_ciphers)))
else:
""" Get C' ciphertexts from leader. """
self.final_ciphers = marshal.loads(self.recv_from_leader())
"""
self.final_ciphers holds an array of
pickled (round_id, cipher_prime) tuples
"""
my_cipher_str = marshal.dumps((self.round_id, self.cipher_prime))
go = False
if my_cipher_str in self.final_ciphers:
self.info("Found my ciphertext in set")
go = True
else:
self.critical("ABORT! My ciphertext is not in set!")
self.debug(self.final_ciphers)
go = False
raise RuntimeError, "Protocol violation: My ciphertext is missing!"
hashval = AnonCrypto.hash_list(self.final_ciphers)
go_msg = marshal.dumps((
self.id,
self.round_id,
go,
hashval))
go_data = ''
if self.am_leader():
""" Collect go msgs """
data = self.recv_from_all(False)
""" Add leader's signed GO message to set """
data.append(AnonCrypto.sign(self.id, self.key1, go_msg))
go_data = marshal.dumps((data))
self.broadcast_to_all_nodes(go_data)
else:
""" Send go msg to leader """
self.send_to_leader(go_msg)
go_data = self.recv_from_leader()
self.check_go_data(hashval, go_data)
self.info("All nodes report GO")
return
示例7: gen_temp_keys
def gen_temp_keys(self):
key1 = AnonCrypto.random_key(KEY_LENGTH)
key2 = AnonCrypto.random_key(KEY_LENGTH)
key1.save_key("config/temp1.priv", None)
key1.save_pub_key("config/temp1.pub")
key2.save_key("config/temp2.priv", None)
key2.save_pub_key("config/temp2.pub")
self.privgenkey1 = M2Crypto.RSA.load_key("config/temp1.priv")
self.pubgenkey1 = M2Crypto.RSA.load_pub_key("config/temp1.pub")
self.privgenkey2 = M2Crypto.RSA.load_key("config/temp2.priv")
self.pubgenkey2 = M2Crypto.RSA.load_pub_key("config/temp2.pub")
示例8: __main__
def __main__(argv):
min_key_len = 1024
if(len(argv) != 14):
raise ValueError, "Usage: %s id key_len round_id n_nodes my_ip my_port leader_ip leader_port dnstr_ip dnstr_port upstr_ip upstr_port msg_len" % (argv[0])
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
debug("Starting node")
id = int(argv[1])
key_len = int(argv[2])
round_id = int(argv[3])
n_nodes = int(argv[4])
my_addr = (argv[5], int(argv[6]))
leader_addr = (argv[7], int(argv[8]))
up_addr = (argv[9], int(argv[10]))
dn_addr = (argv[11], int(argv[12]))
msg_len = int(argv[13])
msg_file = AnonCrypto.random_file(msg_len)
node = bulk_node.bulk_node(id, key_len, round_id, n_nodes,
my_addr, leader_addr, up_addr, dn_addr, msg_file)
cProfile.runctx('mynode.run_protocol()', {'mynode': node}, {})
#node.run_protocol()
fnames = node.output_filenames()
for i in xrange(0, len(fnames)):
copyfile(fnames[i], "data/node%04d-%04d.out" % (id, i))
return
示例9: __main__
def __main__(argv):
min_key_len = 1024
if(len(argv) != 16):
raise ValueError, "Usage: %s id key_len round_id n_nodes my_ip my_port leader_ip leader_port dnstr_ip dnstr_port upstr_ip upstr_port msg_len max_len mode" % (argv[0])
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
id = int(argv[1])
key_len = int(argv[2])
round_id = int(argv[3])
n_nodes = int(argv[4])
my_addr = (argv[5], int(argv[6]))
leader_addr = (argv[7], int(argv[8]))
up_addr = (argv[9], int(argv[10]))
dn_addr = (argv[11], int(argv[12]))
msg_len = int(argv[13])
max_len = int(argv[14])
mode = int (argv[15])
msg_file = AnonCrypto.random_file(msg_len)
node = shuffle_node.shuffle_node(id, key_len, round_id, n_nodes,
my_addr, leader_addr, up_addr, dn_addr, msg_file, max_len)
#cProfile.runctx('node.run_protocol()', {}, {'node': node})
node.run_bad_protocol(mode)
fnames = node.output_filenames()
print "--------------------------------------------------------------------"
# for i in xrange(0, len(fnames)):
# copyfile(fnames[i], "data/node%04d-%04d.out" % (id, i))
return
示例10: accept_phase
def accept_phase(self, ip, port, nonce):
# package and encrypt data
response = marshal.dumps((nonce, self.ip, self.gui_port))
cipher = AnonCrypto.sign_with_key(self.privKey, response)
# respond with ((ip, port), encrypted_data)
AnonNet.send_to_addr(ip, int(port), marshal.dumps(("accept", cipher)))
示例11: prepare_round
def prepare_round(self):
# can't start round without 3 or more peers
if len(self.participants) < 3:
self.DEBUG("Not enough peers to start round!")
return
prepare_voucher = marshal.dumps(
(int(PREPARE_WAIT), int(1), copy.copy(self.participants), self.ip, self.gui_port, self.com_port)
)
cipher = AnonCrypto.sign_with_key(self.privKey, prepare_voucher)
for index, participant in enumerate(self.participants):
down_index = (index - 1) % len(self.participants)
up_index = (index + 1) % len(self.participants)
if (self.ip, self.gui_port, self.com_port) != (participant[1], participant[2], participant[3]):
AnonNet.send_to_addr(
participant[1],
participant[2],
marshal.dumps(("prepare:%s:%s:%s" % (index, down_index, up_index), cipher)),
)
self.DEBUG(
"Sending prepare to peer %s:%s at port %s" % (participant[1], participant[2], participant[3])
)
# after informing the participants, create your node
dn_idx = -1 % len(self.participants)
up_idx = 1
self.start_node(dn_idx, up_idx, self.participants, 0)
# start round after PREPARE_WAIT minutes
DelayTimer(PREPARE_WAIT, self.run_protocol).start()
示例12: create_cipher_string
def create_cipher_string(self):
self.cipher_prime = self.datum_string()
""" Encrypt with all secondary keys from N ... 1 """
self.debug("Orig len: %d" % len(self.cipher_prime))
for i in xrange(self.n_nodes - 1, -1, -1):
k1, k2 = self.pub_keys[i]
self.cipher_prime = AnonCrypto.encrypt_with_rsa(k2, self.cipher_prime)
self.debug("Cipher-prim len: %d" % len(self.cipher_prime))
self.cipher = self.cipher_prime
""" Encrypt with all primary keys from N ... 1 """
for i in xrange(self.n_nodes-1, -1, -1):
k1, k2 = self.pub_keys[i]
self.cipher = AnonCrypto.encrypt_with_rsa(k1, self.cipher)
self.debug("Cipher len: %d" % len(self.cipher))
示例13: broadcast_to_all_nodes
def broadcast_to_all_nodes(self, msg, signed = True):
if not self.am_leader():
raise RuntimeError, 'Only leader can broadcast'
if signed: outmsg = AnonCrypto.sign(self.id, self.key1, msg)
else: outmsg = msg
AnonNet.broadcast_using(self.sockets, AnonNet.send_to_socket, outmsg)
示例14: run_phase4
def run_phase4(self):
self.advance_phase()
self.info("Starting phase 4")
self.data_filenames = self.unpack_msg_tar(self.message_tar)
for i in xrange(0, len(self.data_filenames)):
if AnonCrypto.hash_file(self.data_filenames[i]) != self.msg_data[i][3]:
# raise RuntimeError, "Mismatched hash in slot %d" % i
self.critical("Mismatched hashes")
示例15: unpickle_keyset
def unpickle_keyset(self, keys):
""" Non-leader nodes use this to decode leader's key msg """
(rem_round_id, keydict) = marshal.loads(keys)
if rem_round_id != self.round_id:
raise RuntimeError, "Mismatched round ids"
for i in keydict:
s1,s2 = keydict[i]
k1 = AnonCrypto.pub_key_from_str(s1)
k1.check_key()
self.pub_keys[i] = (k1, k1)
k2 = AnonCrypto.pub_key_from_str(AnonCrypto.verify(self.pub_keys, s2))
k2.check_key()
self.pub_keys[i] = (k1, k2)
self.info('Unpickled public keys')