Пример #1
0
def glob(interp, pattern, flags=0):
    if not _valid_fname(pattern):
        interp.space.ec.warn(
            "glob() expects parameter 1 to be a valid path, string given")
        return interp.space.w_Null
    r, files = _glob(pattern, flags)
    if r != 0 and files != []:
        return interp.space.w_False
    res_list = []
    for i in files:
        res_list.append(interp.space.newstr(i))
    return interp.space.new_array_from_list(res_list)
Пример #2
0
def hash_update_file(space, w_res, filename, w_ctx=None):
    """ Pump data into an active hashing context from a file"""
    if not _valid_fname(filename):
        space.ec.warn("hash_file() expects parameter 1 " "to be a valid path, string given")
        return space.w_False
    try:
        f = open(filename)

        while True:
            data = f.read(1024)
            if not data:
                break
            w_res.update(data)
    except IOError, e:
        if not we_are_translated():
            space.ec.warn("hash_file(%s): failed to open stream: " "%s" % (filename, e.strerror))
            return space.w_False
        assert False  # RPython does not raise IOError
Пример #3
0
def hash_file(space, algo, filename, raw_output=False):
    """ Generate a hash value using the contents of a given file"""
    h = _get_hash_algo(algo)
    if not _valid_fname(filename):
        space.ec.warn("hash_file() expects parameter 1 " "to be a valid path, string given")
        return space.w_Null
    try:
        f = open(filename)
        while True:
            data = f.read(1024)
            if not data:
                break
            h.update(data)
    except IOError, e:
        if not we_are_translated():
            space.ec.warn("hash_file(%s): failed to open stream: " "%s" % (filename, e.strerror))
            return space.w_False
        assert False  # RPython does not raise IOError
Пример #4
0
def hash_update_file(space, w_res, filename, w_ctx=None):
    """ Pump data into an active hashing context from a file"""
    if not _valid_fname(filename):
        space.ec.warn("hash_file() expects parameter 1 "
                      "to be a valid path, string given")
        return space.w_False
    try:
        f = open(filename)

        while True:
            data = f.read(1024)
            if not data:
                break
            w_res.update(data)
    except IOError, e:
        if not we_are_translated():
            space.ec.warn("hash_file(%s): failed to open stream: "
                          "%s" % (filename, e.strerror))
            return space.w_False
        assert False  # RPython does not raise IOError
Пример #5
0
def hash_file(space, algo, filename, raw_output=False):
    """ Generate a hash value using the contents of a given file"""
    h = _get_hash_algo(algo)
    if not _valid_fname(filename):
        space.ec.warn("hash_file() expects parameter 1 "
                      "to be a valid path, string given")
        return space.w_Null
    try:
        f = open(filename)
        while True:
            data = f.read(1024)
            if not data:
                break
            h.update(data)
    except IOError, e:
        if not we_are_translated():
            space.ec.warn("hash_file(%s): failed to open stream: "
                          "%s" % (filename, e.strerror))
            return space.w_False
        assert False  # RPython does not raise IOError
Пример #6
0
def hash_hmac_file(space, algo, filename, key, raw_output=False):
    """ Generate a keyed hash value using the
    HMAC method and the contents of a given file"""

    if not _valid_fname(filename):
        space.ec.warn("hash_hmac_file() expects parameter 1 "
                      "to be a valid path, string given")
        return space.w_False

    h = _get_hash_algo(algo)
    bs = h.block_size
    ipad = [chr(0x36)] * bs
    opad = [chr(0x5c)] * bs

    if len(key) > bs:
        h.update(key)
        key = h.digest()
    elif len(key) < bs:
        key = key + chr(0) * (bs - len(key))

    inside = _get_hash_algo(algo)
    outside = _get_hash_algo(algo)

    inside.update(strxor(key, ipad))

    try:
        f = open(filename)

        while True:
            data = f.read(1024)
            if not data:
                break
            inside.update(data)
    except IOError, e:
        if not we_are_translated():
            space.ec.warn("hash_file(%s): failed to open stream: "
                          "%s" % (filename, e.strerror))
            return space.w_False
        assert False  # RPython does not raise IOError
Пример #7
0
def hash_hmac_file(space, algo, filename, key, raw_output=False):
    """ Generate a keyed hash value using the
    HMAC method and the contents of a given file"""

    if not _valid_fname(filename):
        space.ec.warn("hash_hmac_file() expects parameter 1 " "to be a valid path, string given")
        return space.w_False

    h = _get_hash_algo(algo)
    bs = h.block_size
    ipad = [chr(0x36)] * bs
    opad = [chr(0x5C)] * bs

    if len(key) > bs:
        h.update(key)
        key = h.digest()
    elif len(key) < bs:
        key = key + chr(0) * (bs - len(key))

    inside = _get_hash_algo(algo)
    outside = _get_hash_algo(algo)

    inside.update(strxor(key, ipad))

    try:
        f = open(filename)

        while True:
            data = f.read(1024)
            if not data:
                break
            inside.update(data)
    except IOError, e:
        if not we_are_translated():
            space.ec.warn("hash_file(%s): failed to open stream: " "%s" % (filename, e.strerror))
            return space.w_False
        assert False  # RPython does not raise IOError