"""Generate Wowza secure tokens for v1 and v2.""" import base64 import hashlib def get_wowza_secure_access_token( url_prefix: str, file_path: str, file_name: str, start_time: int, end_time: int, shared_secret: str, ) -> str: """Return wowza secure access token. Args: url_prefix (str): Url prefix, e.g. vod/_definst_/mp3:. file_path (str): Path to the file, e.g. 191773/610/191773610616/ file_name (str): Name of the file, e.g. 191773610616_1_1.mp3 start_time (int): Unix timestamp at which token should become valid end_time (int): Unix timestamp at which token should become invalid shared_secret (str): The Wowza shared secret Returns: str: Secure token value to put in querystring. """ encoding = "utf-8" sha256 = hashlib.sha256() sha256.update(url_prefix.encode(encoding)) sha256.update("{}/".format(file_path).encode(encoding)) sha256.update(file_name.encode(encoding)) sha256.update(("?{}".format(shared_secret)).encode(encoding)) sha256.update("&wowzatokenendtime={}".format(end_time).encode(encoding)) sha256.update("&wowzatokenstarttime={}".format(start_time).encode(encoding)) digest = sha256.digest() result = base64.urlsafe_b64encode(digest).decode("utf-8") return result