Functional Weave
Code in Rust

crypto.sha1@1.0.0

impl/python.py

726 bytes · the Python implementation · view raw

import builtins
import hashlib
from typing import List, Sequence


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

    Only for HMAC-SHA1 in HOTP/TOTP; SHA-1 is broken for collisions. The byte
    values are checked here so a bad value fails with the same words as in
    TypeScript and Rust.
    """
    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.sha1(builtins.bytes(bytes)).digest())