import binascii
import base64
import uuid


def kdm_extract_key_info(data):
    """ Extract fields from decoded key cipher data.

        For more details, see SMPTE 430-1, section 6.1.2

        Args:
            data (bytes): Decoded key cipher data.

        Returns:
            Returns a Dictionary containing all key info fields.
    """
    fields = {}

    fields['StructureID'] = binascii.hexlify(data[:16]).decode()
    fields["CertificateThumbprint"] = base64.b64encode(data[16:36]).decode()
    fields["CompositionPlaylistId"] = str(uuid.UUID(bytes=data[36:52]))
    fields["KeyType"] = data[52:56].decode('ascii')
    fields["KeyId"] = str(uuid.UUID(bytes=data[56:72]))
    fields["NotValidBefore"] = data[72:97].decode('ascii')
    fields["NotValidAfter"] = data[97:122].decode('ascii')
    fields["ContentKey"] = binascii.hexlify((data[122:138])).decode()

    return fields