pumpwood_communication.hash
Create hash from a dictionary.
1"""Create hash from a dictionary.""" 2import os 3import hashlib 4from typing import List 5from pumpwood_communication.serializers import pumpJsonDump 6 7 8def create_hash_from_dict(index_dict: dict, salt: str = "", 9 get_env: bool = True, keys: List[str] = None) -> str: 10 """Create a hash from a dictionary limiting keys used at `keys` arguments. 11 12 Create hash from a dictionary, it adds `HASH_SALT` as salt at the 13 beginng of the string if `HASH_SALT` enviroment variable is present. 14 15 Args: 16 index_dict: Dictionary with the index that will be used to generate 17 the hash value. 18 salt: Salt text that will be concatenated to string before generating 19 the hash. 20 get_env: If uses `HASH_SALT` enviroment variable as salt to create 21 the hash. 22 keys: List of the keys of index_dict that will be used to generate 23 hash. 24 25 Returns: 26 Return a string with hash create using parameters passed at this 27 funcion. 28 """ 29 # If get_env set as True and salt not set try to get from env variable 30 if salt == "" and get_env: 31 salt = os.getenv("HASH_SALT", "") 32 33 temp_dict = index_dict 34 # Retrict keys to be used in hashing 35 if keys is not None: 36 temp_dict = dict([(k, index_dict[k]) for k in keys]) 37 38 string_dict = pumpJsonDump(temp_dict) 39 hash_object = hashlib.sha1( # NOQA 40 salt.encode() + str(string_dict).encode()) 41 pbHash = hash_object.hexdigest() 42 return pbHash 43 44 45def create_hash_from_str(index: str, salt: str = "", 46 get_env: bool = True) -> str: 47 """Create a hash for the index. 48 49 Args: 50 index: String used to create the hash. 51 salt: String to be used as salt to generate the hash. 52 get_env: If enviroment variable `HASH_SALT` should be used 53 to create a salt string, 54 55 Returns: 56 Hash string from parameters passed. 57 """ 58 # If get_env set as True and salt not set try to get from env variable 59 if salt == "" and get_env: 60 salt = os.getenv("HASH_SALT", "") 61 62 hash_object = hashlib.sha1( # NOQA 63 salt.encode() + index.encode()) 64 pbHash = hash_object.hexdigest() 65 return pbHash
def
create_hash_from_dict( index_dict: dict, salt: str = '', get_env: bool = True, keys: List[str] = None) -> str:
9def create_hash_from_dict(index_dict: dict, salt: str = "", 10 get_env: bool = True, keys: List[str] = None) -> str: 11 """Create a hash from a dictionary limiting keys used at `keys` arguments. 12 13 Create hash from a dictionary, it adds `HASH_SALT` as salt at the 14 beginng of the string if `HASH_SALT` enviroment variable is present. 15 16 Args: 17 index_dict: Dictionary with the index that will be used to generate 18 the hash value. 19 salt: Salt text that will be concatenated to string before generating 20 the hash. 21 get_env: If uses `HASH_SALT` enviroment variable as salt to create 22 the hash. 23 keys: List of the keys of index_dict that will be used to generate 24 hash. 25 26 Returns: 27 Return a string with hash create using parameters passed at this 28 funcion. 29 """ 30 # If get_env set as True and salt not set try to get from env variable 31 if salt == "" and get_env: 32 salt = os.getenv("HASH_SALT", "") 33 34 temp_dict = index_dict 35 # Retrict keys to be used in hashing 36 if keys is not None: 37 temp_dict = dict([(k, index_dict[k]) for k in keys]) 38 39 string_dict = pumpJsonDump(temp_dict) 40 hash_object = hashlib.sha1( # NOQA 41 salt.encode() + str(string_dict).encode()) 42 pbHash = hash_object.hexdigest() 43 return pbHash
Create a hash from a dictionary limiting keys used at keys
arguments.
Create hash from a dictionary, it adds HASH_SALT
as salt at the
beginng of the string if HASH_SALT
enviroment variable is present.
Arguments:
- index_dict: Dictionary with the index that will be used to generate the hash value.
- salt: Salt text that will be concatenated to string before generating the hash.
- get_env: If uses
HASH_SALT
enviroment variable as salt to create the hash. - keys: List of the keys of index_dict that will be used to generate hash.
Returns:
Return a string with hash create using parameters passed at this funcion.
def
create_hash_from_str(index: str, salt: str = '', get_env: bool = True) -> str:
46def create_hash_from_str(index: str, salt: str = "", 47 get_env: bool = True) -> str: 48 """Create a hash for the index. 49 50 Args: 51 index: String used to create the hash. 52 salt: String to be used as salt to generate the hash. 53 get_env: If enviroment variable `HASH_SALT` should be used 54 to create a salt string, 55 56 Returns: 57 Hash string from parameters passed. 58 """ 59 # If get_env set as True and salt not set try to get from env variable 60 if salt == "" and get_env: 61 salt = os.getenv("HASH_SALT", "") 62 63 hash_object = hashlib.sha1( # NOQA 64 salt.encode() + index.encode()) 65 pbHash = hash_object.hexdigest() 66 return pbHash
Create a hash for the index.
Arguments:
- index: String used to create the hash.
- salt: String to be used as salt to generate the hash.
- get_env: If enviroment variable
HASH_SALT
should be used to create a salt string,
Returns:
Hash string from parameters passed.