web3-starter-py/web3-toolkit/libraries/math_utils.py
Dr. Marina Souza, PhD abcc1d8aff
add aes wrapper (#48)
2023-07-06 15:58:42 -07:00

32 lines
659 B
Python

# -*- encoding: utf-8 -*-
# math_utils.py
# This class implements math methods used by the other classes.
import base64
import hashlib
def str_to_bytes(data):
"""Convert string to bytes."""
utype = type(b''.decode('utf8'))
if isinstance(data, utype):
return data.encode('utf8')
return data
def b64encode(data: str) -> str:
"""Encode data to base64."""
return base64.b64encode(data).decode('utf-8')
def b64decode(data: str) -> str:
"""Decode base64 data."""
return base64.b64decode(data)
def hash256(data: str) -> str:
"""Hash data with SHA-256."""
return hashlib.sha256(str_to_bytes(data)).digest()