import struct


def format_oath_code(response: bytes, digits: int = 6) -> str:
    """Formats an OATH code from a hash response."""
    offs = response[-1] & 0xF
    code = struct.unpack_from(">I", response[offs:])[0] & 0x7FFFFFFF
    return ("%%0%dd" % digits) % (code % 10 ** digits)