Module RelayModel.KeyGeneration
Expand source code
import hashlib
import uuid
def generate_prefix(layer_id):
"""Generates a prefix for a authentication key. It contains the given RelayLayerId to check the origin of a key.
Args:
layer_id (RelayLayerId): The RelayLayerId of the RelayLayer that generates the key
Returns:
str: The key prefix as a string
"""
sha1 = hashlib.sha1()
sha1.update(str(layer_id).encode())
return sha1.hexdigest()
def generate_key(layer_id):
"""Generates a key for a given RelayLayerId
Args:
layer_id (RelayLayerId): Holds the RelayLayerId of the RelayLayer that generates the key
Returns:
str: The generated key as a string in the format "prefix#uniqueId"
"""
prefix = generate_prefix(layer_id)
base = uuid.uuid4()
key = prefix + "#" + str(base)
return key
def check_key_origin(key: str, layer_id):
"""Checks if the given key contains the given RelayLayerId.
Args:
key (str): Holds the key that should be checked.
layer_id (RelayLayerId): Holds the RelayLayerId of the RelayLayer that should be checked.
Returns:
bool: True if prefix of key matches given layer id, False otherwise.
"""
prefix = generate_prefix(layer_id)
if "#" in key:
key_prefix = key.split("#")[0]
if key_prefix == prefix:
return True
return False
Functions
def check_key_origin(key: str, layer_id)-
Checks if the given key contains the given RelayLayerId.
Args
key:str- Holds the key that should be checked.
layer_id:RelayLayerId- Holds the RelayLayerId of the RelayLayer that should be checked.
Returns
bool- True if prefix of key matches given layer id, False otherwise.
Expand source code
def check_key_origin(key: str, layer_id): """Checks if the given key contains the given RelayLayerId. Args: key (str): Holds the key that should be checked. layer_id (RelayLayerId): Holds the RelayLayerId of the RelayLayer that should be checked. Returns: bool: True if prefix of key matches given layer id, False otherwise. """ prefix = generate_prefix(layer_id) if "#" in key: key_prefix = key.split("#")[0] if key_prefix == prefix: return True return False def generate_key(layer_id)-
Generates a key for a given RelayLayerId
Args
layer_id:RelayLayerId- Holds the RelayLayerId of the RelayLayer that generates the key
Returns
str- The generated key as a string in the format "prefix#uniqueId"
Expand source code
def generate_key(layer_id): """Generates a key for a given RelayLayerId Args: layer_id (RelayLayerId): Holds the RelayLayerId of the RelayLayer that generates the key Returns: str: The generated key as a string in the format "prefix#uniqueId" """ prefix = generate_prefix(layer_id) base = uuid.uuid4() key = prefix + "#" + str(base) return key def generate_prefix(layer_id)-
Generates a prefix for a authentication key. It contains the given RelayLayerId to check the origin of a key.
Args
layer_id:RelayLayerId- The RelayLayerId of the RelayLayer that generates the key
Returns
str- The key prefix as a string
Expand source code
def generate_prefix(layer_id): """Generates a prefix for a authentication key. It contains the given RelayLayerId to check the origin of a key. Args: layer_id (RelayLayerId): The RelayLayerId of the RelayLayer that generates the key Returns: str: The key prefix as a string """ sha1 = hashlib.sha1() sha1.update(str(layer_id).encode()) return sha1.hexdigest()