Refactored Identity method names

This commit is contained in:
Mark Qvist 2021-05-16 16:15:57 +02:00
parent fe773c32e2
commit d03b7d7a52
8 changed files with 42 additions and 42 deletions

View File

@ -133,7 +133,7 @@ def client(destination_hexhash, configpath, timeout=None):
# We set the destination to the request_destination
# that was just created, and the only data we add
# is a random hash.
echo_request = RNS.Packet(request_destination, RNS.Identity.getRandomHash())
echo_request = RNS.Packet(request_destination, RNS.Identity.get_random_hash())
# Send the packet! If the packet is successfully
# sent, it will return a PacketReceipt instance.

View File

@ -332,7 +332,7 @@ class Destination:
:param path_response: Internal flag used by :ref:`RNS.Transport<Transport>`. Ignore.
"""
destination_hash = self.hash
random_hash = RNS.Identity.getRandomHash()
random_hash = RNS.Identity.get_random_hash()
if app_data == None and self.default_app_data != None:
if isinstance(self.default_app_data, bytes):
@ -342,7 +342,7 @@ class Destination:
if isinstance(returned_app_data, bytes):
app_data = returned_app_data
signed_data = self.hash+self.identity.getPublicKey()+random_hash
signed_data = self.hash+self.identity.get_public_key()+random_hash
if app_data != None:
signed_data += app_data
@ -351,7 +351,7 @@ class Destination:
# TODO: Check if this could be optimised by only
# carrying the hash in the destination field, not
# also redundantly inside the signed blob as here
announce_data = self.hash+self.identity.getPublicKey()+random_hash+signature
announce_data = self.hash+self.identity.get_public_key()+random_hash+signature
if app_data != None:
announce_data += app_data

View File

@ -42,7 +42,7 @@ class Identity:
if destination_hash in Identity.known_destinations:
identity_data = Identity.known_destinations[destination_hash]
identity = Identity(public_only=True)
identity.loadPublicKey(identity_data[2])
identity.load_public_key(identity_data[2])
identity.app_data = identity_data[3]
RNS.log("Found "+RNS.prettyhexrep(destination_hash)+" in known destinations", RNS.LOG_EXTREME)
return identity
@ -62,7 +62,7 @@ class Identity:
return None
@staticmethod
def saveKnownDestinations():
def save_known_destinations():
RNS.log("Saving known destinations to storage...", RNS.LOG_VERBOSE)
file = open(RNS.Reticulum.storagepath+"/known_destinations","wb")
umsgpack.dump(Identity.known_destinations, file)
@ -70,7 +70,7 @@ class Identity:
RNS.log("Done saving known destinations to storage", RNS.LOG_VERBOSE)
@staticmethod
def loadKnownDestinations():
def load_known_destinations():
if os.path.isfile(RNS.Reticulum.storagepath+"/known_destinations"):
try:
file = open(RNS.Reticulum.storagepath+"/known_destinations","rb")
@ -83,22 +83,22 @@ class Identity:
RNS.log("Destinations file does not exist, so no known destinations loaded", RNS.LOG_VERBOSE)
@staticmethod
def fullHash(data):
def full_hash(data):
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
digest.update(data)
return digest.finalize()
@staticmethod
def truncatedHash(data):
return Identity.fullHash(data)[:(Identity.TRUNCATED_HASHLENGTH//8)]
def truncated_hash(data):
return Identity.full_hash(data)[:(Identity.TRUNCATED_HASHLENGTH//8)]
@staticmethod
def getRandomHash():
return Identity.truncatedHash(os.urandom(10))
def get_random_hash():
return Identity.truncated_hash(os.urandom(10))
@staticmethod
def validateAnnounce(packet):
def validate_announce(packet):
if packet.packet_type == RNS.Packet.ANNOUNCE:
RNS.log("Validating announce from "+RNS.prettyhexrep(packet.destination_hash), RNS.LOG_DEBUG)
destination_hash = packet.destination_hash
@ -115,7 +115,7 @@ class Identity:
app_data = None
announced_identity = Identity(public_only=True)
announced_identity.loadPublicKey(public_key)
announced_identity.load_public_key(public_key)
if announced_identity.pub != None and announced_identity.validate(signature, signed_data):
RNS.Identity.remember(packet.getHash(), destination_hash, public_key, app_data)
@ -129,7 +129,7 @@ class Identity:
@staticmethod
def exit_handler():
Identity.saveKnownDestinations()
Identity.save_known_destinations()
@staticmethod
@ -151,9 +151,9 @@ class Identity:
self.hexhash = None
if not public_only:
self.createKeys()
self.create_keys()
def createKeys(self):
def create_keys(self):
self.prv = rsa.generate_private_key(
public_exponent=65537,
key_size=Identity.KEYSIZE,
@ -170,17 +170,17 @@ class Identity:
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
self.updateHashes()
self.update_hashes()
RNS.log("Identity keys created for "+RNS.prettyhexrep(self.hash), RNS.LOG_VERBOSE)
def getPrivateKey(self):
def get_private_key(self):
return self.prv_bytes
def getPublicKey(self):
def get_public_key(self):
return self.pub_bytes
def loadPrivateKey(self, prv_bytes):
def load_private_key(self, prv_bytes):
try:
self.prv_bytes = prv_bytes
self.prv = serialization.load_der_private_key(
@ -193,7 +193,7 @@ class Identity:
encoding=serialization.Encoding.DER,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
self.updateHashes()
self.update_hashes()
return True
@ -202,16 +202,16 @@ class Identity:
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)
return False
def loadPublicKey(self, key):
def load_public_key(self, key):
try:
self.pub_bytes = key
self.pub = load_der_public_key(self.pub_bytes, backend=default_backend())
self.updateHashes()
self.update_hashes()
except Exception as e:
RNS.log("Error while loading public key, the contained exception was: "+str(e), RNS.LOG_ERROR)
def updateHashes(self):
self.hash = Identity.truncatedHash(self.pub_bytes)
def update_hashes(self):
self.hash = Identity.truncated_hash(self.pub_bytes)
self.hexhash = self.hash.hex()
def save(self, path):
@ -228,7 +228,7 @@ class Identity:
try:
with open(path, "rb") as key_file:
prv_bytes = key_file.read()
return self.loadPrivateKey(prv_bytes)
return self.load_private_key(prv_bytes)
return False
except Exception as e:
RNS.log("Error while loading identity from "+str(path), RNS.LOG_ERROR)

View File

@ -12,4 +12,4 @@ class Interface:
def get_hash(self):
# TODO: Maybe expand this to something more unique
return RNS.Identity.fullHash(str(self).encode("utf-8"))
return RNS.Identity.full_hash(str(self).encode("utf-8"))

View File

@ -243,10 +243,10 @@ class Packet:
self.packet_hash = self.getHash()
def getHash(self):
return RNS.Identity.fullHash(self.getHashablePart())
return RNS.Identity.full_hash(self.getHashablePart())
def getTruncatedHash(self):
return RNS.Identity.truncatedHash(self.getHashablePart())
return RNS.Identity.truncated_hash(self.getHashablePart())
def getHashablePart(self):
hashable_part = bytes([self.raw[0] & 0b00001111])

View File

@ -195,7 +195,7 @@ class Resource:
RNS.log("Compression saved "+str(saved_bytes)+" bytes, sending compressed", RNS.LOG_DEBUG)
self.data = b""
self.data += RNS.Identity.getRandomHash()[:Resource.RANDOM_HASH_SIZE]
self.data += RNS.Identity.get_random_hash()[:Resource.RANDOM_HASH_SIZE]
self.data += self.compressed_data
self.compressed = True
@ -203,7 +203,7 @@ class Resource:
else:
self.data = b""
self.data += RNS.Identity.getRandomHash()[:Resource.RANDOM_HASH_SIZE]
self.data += RNS.Identity.get_random_hash()[:Resource.RANDOM_HASH_SIZE]
self.data += self.uncompressed_data
self.uncompressed_data = self.data
@ -231,9 +231,9 @@ class Resource:
hashmap_computation_began = time.time()
RNS.log("Starting resource hashmap computation with "+str(hashmap_entries)+" entries...", RNS.LOG_DEBUG)
self.random_hash = RNS.Identity.getRandomHash()[:Resource.RANDOM_HASH_SIZE]
self.hash = RNS.Identity.fullHash(data+self.random_hash)
self.expected_proof = RNS.Identity.fullHash(data+self.hash)
self.random_hash = RNS.Identity.get_random_hash()[:Resource.RANDOM_HASH_SIZE]
self.hash = RNS.Identity.full_hash(data+self.random_hash)
self.expected_proof = RNS.Identity.full_hash(data+self.hash)
if original_hash == None:
self.original_hash = self.hash
@ -298,7 +298,7 @@ class Resource:
# uncompressed transfers on streams with long blocks
# of identical bytes. Doing so would be very silly
# anyways but maybe it should be handled gracefully.
return RNS.Identity.fullHash(data+self.random_hash)[:Resource.MAPHASH_LEN]
return RNS.Identity.full_hash(data+self.random_hash)[:Resource.MAPHASH_LEN]
def advertise(self):
thread = threading.Thread(target=self.__advertise_job)
@ -437,7 +437,7 @@ class Resource:
else:
self.data = data
calculated_hash = RNS.Identity.fullHash(self.data+self.random_hash)
calculated_hash = RNS.Identity.full_hash(self.data+self.random_hash)
if calculated_hash == self.hash:
self.file = open(self.storagepath, "ab")
@ -474,7 +474,7 @@ class Resource:
def prove(self):
if not self.status == Resource.FAILED:
try:
proof = RNS.Identity.fullHash(self.data+self.hash)
proof = RNS.Identity.full_hash(self.data+self.hash)
proof_data = self.hash+proof
proof_packet = RNS.Packet(self.link, proof_data, packet_type=RNS.Packet.PROOF, context=RNS.Packet.RESOURCE_PRF)
proof_packet.send()

View File

@ -120,7 +120,7 @@ class Reticulum:
exit(1)
self.__apply_config()
RNS.Identity.loadKnownDestinations()
RNS.Identity.load_known_destinations()
RNS.Transport.start(self)

View File

@ -619,7 +619,7 @@ class Transport:
# of queued announce rebroadcasts once handed to the next node.
if packet.packet_type == RNS.Packet.ANNOUNCE:
local_destination = next((d for d in Transport.destinations if d.hash == packet.destination_hash), None)
if local_destination == None and RNS.Identity.validateAnnounce(packet):
if local_destination == None and RNS.Identity.validate_announce(packet):
if packet.transport_id != None:
received_from = packet.transport_id
@ -1016,14 +1016,14 @@ class Transport:
@staticmethod
def requestPath(destination_hash):
path_request_data = destination_hash + RNS.Identity.getRandomHash()
path_request_data = destination_hash + RNS.Identity.get_random_hash()
path_request_dst = RNS.Destination(None, RNS.Destination.OUT, RNS.Destination.PLAIN, Transport.APP_NAME, "path", "request")
packet = RNS.Packet(path_request_dst, path_request_data, packet_type = RNS.Packet.DATA, transport_type = RNS.Transport.BROADCAST, header_type = RNS.Packet.HEADER_1)
packet.send()
@staticmethod
def requestPathOnInterface(destination_hash, interface):
path_request_data = destination_hash + RNS.Identity.getRandomHash()
path_request_data = destination_hash + RNS.Identity.get_random_hash()
path_request_dst = RNS.Destination(None, RNS.Destination.OUT, RNS.Destination.PLAIN, Transport.APP_NAME, "path", "request")
packet = RNS.Packet(path_request_dst, path_request_data, packet_type = RNS.Packet.DATA, transport_type = RNS.Transport.BROADCAST, header_type = RNS.Packet.HEADER_1, attached_interface = interface)
packet.send()