import hashlib


def CryptDeriveKey(password, key_len=0x20):
    """
    Emulate MS CryptDeriveKey
    https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptderivekey

    :param bytes password: Password to base the key off of.
    :param int key_len: Length of key to be used.

    :return: Sequence of bytes representing the key to use.
    :rtype: bytes
    """

    base_data = hashlib.sha1(password).digest()
    b1, b2 = bytearray(b'\x00' * 0x40), bytearray(b'\x00' * 0x40)
    for i in range(0, 0x40):
        b1[i] = 0x36
        b2[i] = 0x5c
        if i < len(base_data):
            b1[i] ^= base_data[i]
            b2[i] ^= base_data[i]
    key_hash = hashlib.sha1(b1).digest() + hashlib.sha1(b2).digest()
    return key_hash[:key_len]