Exemple #1
0
def decrypt_write_file(localindex, fdir, fhash, secret):
    """
    @param localindex: dict
    @type localindex:
    @param fdir: str or unicode
    @type fdir:
    @param fhash: str or unicode
    @type fhash:
    @param secret: str or unicode
    @type secret:
    """
    cpath = os.path.join(fdir, fhash[2:])
    enc_file_parts = open(cpath).read().split("\n")
    enc_file_parts = tuple([x for x in enc_file_parts if x])
    data = decrypt_file_smp(secret,
                            enc_files=enc_file_parts,
                            progress_callback=update_item_progress)
    os.remove(cpath)

    for fp in enc_file_parts:
        os.remove(fp)

    files_left = get_files_dir(fdir)

    if len(files_left) == 0:
        os.rmdir(fdir)

    if len(files_left) == 1:
        dstorp = os.path.join(fdir, ".DS_Store")

        if os.path.exists(dstorp):
            os.remove(dstorp)
            files_left = get_files_dir(fdir)

            if len(files_left) == 0:
                os.rmdir(fdir)

    file_blob = {"data": data}
    paths = []

    for dirhash in localindex["dirnames"]:
        for cfile in localindex["dirnames"][dirhash]["filenames"]:
            if strcmp(fhash, cfile["hash"]):
                fpath = os.path.join(
                    localindex["dirnames"][dirhash]["dirname"], cfile["name"])

                if not os.path.exists(fpath):
                    ft = localindex["filestats"][fpath]
                    file_blob["st_atime"] = int(ft["st_atime"])
                    file_blob["st_mtime"] = int(ft["st_mtime"])
                    file_blob["st_mode"] = int(ft["st_mode"])
                    file_blob["st_uid"] = int(ft["st_uid"])
                    file_blob["st_gid"] = int(ft["st_gid"])
                    write_fdict_to_file(file_blob, fpath)
                    paths.append(fpath)

    return paths
Exemple #2
0
 def test_encrypt_file_smp_single_file(self):
     """
     test_encrypt_file
     """
     self.do_wait_for_tasks = False
     fname = "testdata/200MB.zip"
     secret = '\xeb>M\x04\xc22\x96!\xce\xed\xbb.\xe1u\xc7\xe4\x07h<.\x87\xc9H\x89\x8aj\xb4\xb2b5}\x95'
     enc_file = encrypt_file_smp(secret, fname, print_progress, single_file=True)
     dec_file = decrypt_file_smp(secret, enc_file=enc_file, progress_callback=print_progress)
     self.assertEqual(make_sha1_hash_file(fpi=dec_file), make_sha1_hash_file(fpi=open(fname)))
Exemple #3
0
def decrypt_write_file(localindex, fdir, fhash, secret):
    """
    @param localindex: dict
    @type localindex:
    @param fdir: str or unicode
    @type fdir:
    @param fhash: str or unicode
    @type fhash:
    @param secret: str or unicode
    @type secret:
    """
    cpath = os.path.join(fdir, fhash[2:])
    enc_file_parts = open(cpath).read().split("\n")
    enc_file_parts = tuple([x for x in enc_file_parts if x])
    data = decrypt_file_smp(secret, enc_files=enc_file_parts, progress_callback=update_item_progress)
    os.remove(cpath)

    for fp in enc_file_parts:
        os.remove(fp)

    files_left = get_files_dir(fdir)

    if len(files_left) == 0:
        os.rmdir(fdir)

    if len(files_left) == 1:
        dstorp = os.path.join(fdir, ".DS_Store")

        if os.path.exists(dstorp):
            os.remove(dstorp)
            files_left = get_files_dir(fdir)

            if len(files_left) == 0:
                os.rmdir(fdir)

    file_blob = {"data": data}
    paths = []

    for dirhash in localindex["dirnames"]:
        for cfile in localindex["dirnames"][dirhash]["filenames"]:
            if strcmp(fhash, cfile["hash"]):
                fpath = os.path.join(localindex["dirnames"][dirhash]["dirname"], cfile["name"])

                if not os.path.exists(fpath):
                    ft = localindex["filestats"][fpath]
                    file_blob["st_atime"] = int(ft["st_atime"])
                    file_blob["st_mtime"] = int(ft["st_mtime"])
                    file_blob["st_mode"] = int(ft["st_mode"])
                    file_blob["st_uid"] = int(ft["st_uid"])
                    file_blob["st_gid"] = int(ft["st_gid"])
                    write_fdict_to_file(file_blob, fpath)
                    paths.append(fpath)

    return paths
Exemple #4
0
    def test_encrypt_file_smp(self):
        """
        test_encrypt_file
        """
        self.do_wait_for_tasks = False

        #self.make_testfile("1000MB.zip", 1000)
        fname = "testdata/200MB.zip"
        secret = '\xeb>M\x04\xc22\x96!\xce\xed\xbb.\xe1u\xc7\xe4\x07h<.\x87\xc9H\x89\x8aj\xb4\xb2b5}\x95'
        enc_files = encrypt_file_smp(secret, fname, print_progress)
        dec_file = decrypt_file_smp(secret, enc_files=enc_files, progress_callback=print_progress, delete_enc_files=True)
        h1 = make_sha1_hash_file(fpi=dec_file)
        h2 = make_sha1_hash_file(fpi=open(fname))
        self.assertEqual(h1, h2)
Exemple #5
0
def decrypt_file(enc_path, secret, get_chunk_paths=False):
    """
    @type enc_path: str or unicode
    @type secret: str or unicode
    @return: @rtype:
    """
    enc_file_chunks = []
    with open(enc_path) as enc_fp:
        enc_file_chunk_path = enc_fp.readline()

        while enc_file_chunk_path:
            enc_file_chunks.append(enc_file_chunk_path.strip())
            enc_file_chunk_path = enc_fp.readline()

    dec_file = decrypt_file_smp(secret, enc_files=tuple(enc_file_chunks), progress_callback=update_item_progress)

    if get_chunk_paths:
        return dec_file, enc_file_chunks
    else:
        return dec_file
Exemple #6
0
def decrypt_file(enc_path, secret, get_chunk_paths=False):
    """
    @type enc_path: str or unicode
    @type secret: str or unicode
    @return: @rtype:
    """
    enc_file_chunks = []
    with open(enc_path) as enc_fp:
        enc_file_chunk_path = enc_fp.readline()

        while enc_file_chunk_path:
            enc_file_chunks.append(enc_file_chunk_path.strip())
            enc_file_chunk_path = enc_fp.readline()

    dec_file = decrypt_file_smp(secret,
                                enc_files=tuple(enc_file_chunks),
                                progress_callback=update_item_progress)

    if get_chunk_paths:
        return dec_file, enc_file_chunks
    else:
        return dec_file