Functional Weave
Code in TypeScript

crypto.sha256@1.0.0

impl/python.py

750 bytes · the Python implementation · view raw

import builtins
import hashlib
from typing import List, Sequence


def sha256(bytes: Sequence[int]) -> List[int]:
    """The SHA-256 digest of a byte string (FIPS 180-4), from hashlib.

    A Python ``bytes`` value is accepted as is; a list must hold integers
    0-255, checked here so a bad value fails with the same words as in
    TypeScript and Rust rather than hashlib's own.
    """
    if isinstance(bytes, (str, dict)) or not hasattr(bytes, "__len__"):
        raise TypeError("bytes must be a list of integers from 0 to 255")
    for b in bytes:
        if type(b) is not int or b < 0 or b > 255:
            raise ValueError("bytes must be a list of integers from 0 to 255")
    return list(hashlib.sha256(builtins.bytes(bytes)).digest())