model/utils.py
def hash_content(content: dict, difficulty: int, nonce_limit: int) -> dict:
    """Returns the new dictionary with it's hash containing 
    N leading zeros, where N is the given difficulty"""

    content["nonce"] = 0
    timestamp = datetime.datetime.now(datetime.timezone.utc)
    content["timestamp"] = str(timestamp)
    hash_value = ""

    while not hash_value[:difficulty] == "0" * difficulty:
        content["nonce"] += 1

        if content["nonce"] > nonce_limit:
            timestamp = datetime.datetime.now(
                datetime.timezone.utc)

            content["timestamp"] = str(timestamp)
            content["nonce"] = 0

        encoded_content = json.dumps(content, sort_keys=True).encode()
        hash_value = hashlib.sha256(encoded_content).hexdigest()

    content["hash_value"] = hash_value

    return content
Returns the new dictionary with it's hash containing 
    N leading zeros, where N is the given difficulty
Returns the new dictionary with it's hash containing
N leading zeros, where N is the given difficulty
Returns the new dictionary with it ' s hash containing N leading zeros where N is the given difficulty
def hash_content(content: dict, difficulty: int, nonce_limit: int) -> dict:
    content["nonce"] = 0
    timestamp = datetime.datetime.now(datetime.timezone.utc)
    content["timestamp"] = str(timestamp)
    hash_value = ""
    while not hash_value[:difficulty] == "0" * difficulty:
        content["nonce"] += 1
        if content["nonce"] > nonce_limit:
            timestamp = datetime.datetime.now(
                datetime.timezone.utc)
            content["timestamp"] = str(timestamp)
            content["nonce"] = 0
        encoded_content = json.dumps(content, sort_keys=True).encode()
        hash_value = hashlib.sha256(encoded_content).hexdigest()
    content["hash_value"] = hash_value
    return content
def hash_content ( content : dict , difficulty : int , nonce_limit : int ) -> dict : content [ "nonce" ] = 0 timestamp = datetime . datetime . now ( datetime . timezone . utc ) content [ "timestamp" ] = str ( timestamp ) hash_value = "" while not hash_value [ : difficulty ] == "0" * difficulty : content [ "nonce" ] += 1 if content [ "nonce" ] > nonce_limit : timestamp = datetime . datetime . now ( datetime . timezone . utc ) content [ "timestamp" ] = str ( timestamp ) content [ "nonce" ] = 0 encoded_content = json . dumps ( content , sort_keys = True ) . encode ( ) hash_value = hashlib . sha256 ( encoded_content ) . hexdigest ( ) content [ "hash_value" ] = hash_value return content
Returns the new dictionary with it's hash containing
N leading zeros, where N is the given difficulty
Returns the new dictionary with it ' s hash containing N leading zeros where N is the given difficulty
"""Returns the new dictionary with it's hash containing 
    N leading zeros, where N is the given difficulty"""
content
dict
difficulty
int
nonce_limit
int