示例#1
0
def get_hash(hash, path, hex=1):
    """
    Return hash of path

    hash should be "MD5" or "SHA1".  The output will be in hexadecimal
    form if hex is true, and in text (base64) otherwise.
    """
    # assert path.isreg()
    fp = path.open("rb")
    if hash == "SHA1":
        hash_obj = sha1()
    elif hash == "MD5":
        hash_obj = md5()
    else:
        assert 0, "Unknown hash %s" % (hash,)

    while 1:
        buf = fp.read(blocksize)
        if not buf:
            break
        hash_obj.update(buf)
    assert not fp.close()
    if hex:
        return hash_obj.hexdigest()
    else:
        return hash_obj.digest()
示例#2
0
def get_hash(hash, path, hex=1):  # pylint: disable=redefined-builtin
    u"""
    Return hash of path

    hash should be "MD5" or "SHA1".  The output will be in hexadecimal
    form if hex is true, and in text (base64) otherwise.
    """
    # assert path.isreg()
    fp = path.open(u"rb")
    if hash == u"SHA1":
        hash_obj = sha1()
    elif hash == u"MD5":
        hash_obj = md5()
    else:
        assert 0, u"Unknown hash %s" % (hash, )

    while 1:
        buf = fp.read(blocksize)
        if not buf:
            break
        hash_obj.update(buf)
    assert not fp.close()
    if hex:
        return hash_obj.hexdigest()
    else:
        return hash_obj.digest()