import re


def readChecksumFromFile(checksumFilepath, expectedLength):
    """Read checksum digest from checksum file
    The content of the checksum file must be e.g. in the following format:

        some text da39a3ee5e6b4b0d3255bfef95601890afd80709

    There can also be CR, LF or both at the end of the line.

    :param checksumFilepath: Location of the checksum file
    :param expectedLength: Expected length of the checksum digest (e.g. 32, 40..)
    :returns: Checksum digest if present in file, None otherwise
    """
    checksumRegex = re.compile("^(?:.*\s+)?([0-9a-f]{%d})\s*$" % expectedLength)

    with open(checksumFilepath, "r") as checksumFile:
        checksumContent = checksumFile.read()
    checksum = checksumRegex.search(checksumContent)

    return checksum.group(1) if checksum else None