Esempio n. 1
0
    def compare_files(file1, file2, msg_callback=None, cmd_output=None):
        """
        Compare files by calculating and comparing SHA-256 checksums

        :param file1: First file to compare
        :param file2: Second file to compare
        :param msg_callback: message callback object, can be used to collect additional data via .echo()
        :param cmd_output: Additional data to be used for JSON output
        :return: True if matches, False if doesn't match
        """
        # Calculate the checksums
        hasher = filehash.FileHash(DEFAULT_HASH_ALGORITHM)
        try:
            file1_hash = hasher.hash_file(filename=file1)
            file2_hash = hasher.hash_file(filename=file2)
        except FileNotFoundError as err:
            if msg_callback:
                msg_callback.echo(str(err))
            return False

        # Output additional information if needed
        if msg_callback:
            msg_callback.echo('File1 checksum: ' + file1_hash)
            msg_callback.echo('File2 checksum: ' + file2_hash)

        # Compare the checksums and return the result
        if file1_hash == file2_hash:
            return True
        else:
            if cmd_output is not None:
                cmd_output.append('File1 checksum: ' + file1_hash)
                cmd_output.append('File2 checksum: ' + file2_hash)
            return False
Esempio n. 2
0
    def getFileHash(self, client_socket, command_list):
        '''
        Function to get file hash and send a response to the client
        check decode_command for info of how this will be called.
        Input:
            client_socket - client socket object
            command_list - list object
        Returns:
            None
        '''

        if len(command_list) == 3 or len(command_list) == 2:
            arg = command_list[1].lower()
            if arg.lower() == 'verify':
                filename = command_list[2]
                if os.path.isfile(self.file_storage_path + '/' + filename):
                    arg_file = open(self.file_storage_path + '/' + filename,
                                    'rb')
                    file_stats = os.stat(self.file_storage_path + '/' +
                                         filename)
                    size = file_stats.st_size
                    file_mtime = time.localtime(
                        os.path.getmtime(self.file_storage_path + '/' +
                                         filename))
                    file_mtime = time.strftime('%Y-%m-%d %H:%M:%S', file_mtime)
                    # hasher = hashlib.md5(arg_file.read()).hexdigest()
                    hasher = filehash.FileHash('md5')
                    hasher = hasher.hash_file(self.file_storage_path + '/' +
                                              filename)

                    return_value = 'hash: ' + hasher + ' size: ' + str(
                        size) + ' last modified: ' + file_mtime
                    print(return_value)
                    client_socket.send(return_value.encode('utf-8'))
                    return None

                else:
                    return_value = '0'
                    client_socket.send(return_value.encode('utf-8'))
                    return None

            elif arg.lower() == 'checkall':

                return_value = ''
                for file_name in os.listdir(self.file_storage_path):

                    hasher = filehash.FileHash('md5')
                    hasher = hasher.hash_file(self.file_storage_path + '/' +
                                              file_name)
                    file_mtime = time.localtime(
                        os.path.getmtime(self.file_storage_path + '/' +
                                         file_name))
                    file_mtime = time.strftime('%Y-%m-%d %H:%M:%S', file_mtime)
                    return_value = return_value + file_name + ' hash value: ' + hasher + '  last modified:  ' + file_mtime + '\n'

                client_socket.send(return_value.encode('utf-8'))

                return None

        return_value = "Command error. Usage: FileHash verify <filename>\n FileHash checkall"
        client_socket.send(return_value.encode('utf-8'))
Esempio n. 3
0
    def sendFile(self, client_socket, command_list):
        '''
        Function to send files to client(client download file)

        Input:
            arg - TCP or UDP
            client_socket - client socket object
            filename - filename to the respective file
        Returns:
            None 
        '''

        if len(command_list) == 3:

            if command_list[1].lower() != 'udp' and command_list[1].lower(
            ) != 'tcp':
                print('arg error')
                string_to_send = 'Command error. Usage: FileDownload tcp/udp <filename>'
                client_socket.send(string_to_send.encode('utf-8'))
                return None

            path = self.file_storage_path + '/' + command_list[2]
            arg = command_list[1]

            string_to_send = "Requested file not present in server"
            if not os.path.isfile(path):
                print(string_to_send)
                client_socket.send(string_to_send.encode('utf-8'))
            else:
                filename = command_list[2]
                file_stats = os.stat(path)
                size = file_stats.st_size
                file_mtime = time.localtime(os.path.getmtime(path))
                file_mtime = time.strftime('%Y-%m-%d %H:%M:%S', file_mtime)
                hasher = filehash.FileHash('md5')
                hasher = hasher.hash_file(path)
                print("Sending...")
                file_info_text = (
                    "Filename: %s, Timestamp: %s, MD5hash: %s, Filesize(Bytes): %s"
                    % (filename, str(file_mtime), str(hasher), str(size)))
                print(file_info_text)
                # string_to_send = "Requested file has been found. Filesize: " + str(size)
                string_to_send = "Requested file has been found.\n" + file_info_text

                print(string_to_send)
                client_socket.send(string_to_send.encode('utf-8'))

                time.sleep(0.02)

                if arg == 'tcp' or arg == 'TCP':
                    file = open(path, 'rb')
                    reading = file.read(config.BUFFER_SIZE)
                    # print (reading)
                    while (reading):
                        client_socket.send(reading)
                        reading = file.read(config.BUFFER_SIZE)

                        # print (reading)
                    file.close()

                elif arg == 'udp' or arg == 'UDP':

                    file = open(path, 'rb')
                    client_udp_socket = socket.socket(socket.AF_INET,
                                                      socket.SOCK_DGRAM)

                    client_ip = client_socket.getpeername()[0]
                    port_number = client_socket.getpeername()

                    if client_ip == '':
                        client_ip = '127.0.0.1'

                    reading = file.read(config.BUFFER_SIZE)

                    while (reading):
                        if (client_udp_socket.sendto(
                                reading, (client_ip, self.udp_port))):
                            reading = file.read(config.BUFFER_SIZE)
                            time.sleep(0.02)
                    client_udp_socket.close()
                    file.close()
        else:
            string_to_send = 'Command error. Usage: FileDownload tcp/udp <filename>'
            client_socket.send(string_to_send.encode('utf-8'))
            return None
Esempio n. 4
0
    def verify_checksum(filename,
                        algorithm,
                        msg_callback=None,
                        cmd_output=None,
                        checksum_value=None,
                        checksumfile=None):
        """
        Calculates a filename hash and compares against the provided checksum or checksums file

        :param filename: Filename used to calculate the hash
        :param algorithm: Algorithm to use for hashing
        :param msg_callback: message callback object, can be used to collect additional data via .echo()
        :param cmd_output: Additional data to be used for JSON output
        :param checksum_value: Checksum value
        :param checksumfile: Filename of the file containing checksums, follows the format from shasum
        :return: True if matches, False if doesn't match
        """
        # Check algorithm for valid values
        if algorithm not in filehash.SUPPORTED_ALGORITHMS:
            raise ValueError('Unsupported algorithm value')

        # Make sure either checksum or checksumfile arguments are set
        if checksum_value is None and checksumfile is None:
            raise ValueError(
                'Either checksum_value or checksumfile arguments must be set')

        # Calculate the hash
        try:
            calculated_hash = filehash.FileHash(algorithm).hash_file(
                filename=filename)
        except FileNotFoundError as err:
            if msg_callback:
                msg_callback.echo(str(err))
            return False

        # Output additional information if needed
        if msg_callback:
            msg_callback.echo('Algorithm: ' + algorithm)
            msg_callback.echo('File hash: ' + calculated_hash)

        # Compare the checksums and return the result
        if checksum_value:
            if calculated_hash == checksum_value.lower().strip():
                return True
            else:
                if cmd_output is not None:
                    cmd_output.append('Algorithm: ' + algorithm)
                    cmd_output.append('File checksum: ' + calculated_hash)
                    cmd_output.append('Checksum to check against: ' +
                                      checksum_value)
                return False
        else:
            try:
                checksums_content = str(Path(checksumfile).read_bytes())
            except (FileNotFoundError, TypeError) as err:
                if msg_callback:
                    msg_callback.echo(str(err))
                return False

            # Process verification results
            if calculated_hash in checksums_content:
                return True
            else:
                if cmd_output is not None:
                    cmd_output.append('Algorithm: ' + algorithm)
                    cmd_output.append('File checksum: ' + calculated_hash)
                    cmd_output.append('No match found in checksum file')
                return False
import itertools
import multiprocessing

import matplotlib
import matplotlib.pyplot as plt

import filehash
import subprocess

cpu_amount = multiprocessing.cpu_count()

import numpy as np

from PIL import Image, ImageFile

hasher = filehash.FileHash('sha512')

# import decimal
# prec = 50
# decimal.getcontext().prec = prec

# from decimal import Decimal as Dec

ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) + "/"

if __name__ == "__main__":
    # n = 4
    # for page_num in range(100*n, 100*(n+1)):
    # 1237
    # n = 1000
    n1 = 4200
Esempio n. 6
0
    os.chdir(this_file.parents[1])
    cache_file = this_file.parent / "cache.json"

    if cache_file.exists():
        with open(cache_file, 'r') as f:
            cache = json.load(f)
    else:
        cache = dict()

    root = Path(os.getcwd())

    svgs = [str(x) for x in root.glob("**/svg_source/*.svg")]

    changed = list()

    sha512hasher = filehash.FileHash()

    for file in svgs:
        _hash = sha512hasher.hash_file(file)
        if file in cache.keys():
            if cache[file] != _hash:
                cache[file] = _hash
                changed.append(file)

        else:
            cache[file] = _hash
            changed.append(file)

    for change in changed:
        _path = Path(change).parents[1]
        command = [
import multiprocessing

import matplotlib
import matplotlib.pyplot as plt

import hashlib
import filehash
import subprocess

cpu_amount = multiprocessing.cpu_count()

import numpy as np

from PIL import Image, ImageFile

hasher512 = filehash.FileHash('sha512')
hasher256 = filehash.FileHash('sha256')

# import decimal
# prec = 50
# decimal.getcontext().prec = prec

# from decimal import Decimal as Dec

ROOT_PATH = os.path.dirname(os.path.abspath(__file__))+"/"

def get_image_size_from_path(file_path):
    ImPar=ImageFile.Parser()
    with open(file_path, "rb") as f:
        ImPar=ImageFile.Parser()
        chunk = f.read(2048)
Esempio n. 8
0
    def Cache(self, command_list):
        '''
        Cache command
        Input :
            command_list - list object - contains command separated at spaces
        Return :
            None
        '''

        if len(command_list) == 1:
            print(
                'Command error. Usage :\nCache verify <filename>\nCache show')
            return None

        if command_list[1].lower() == 'show':

            files = os.scandir(self.cache_directory_path)
            # print (files)
            string_to_display = ''
            for entry in files:
                stats_entry = entry.stat()
                size = stats_entry.st_size
                string = entry.name + " | " + str(size)
                string_to_display = string_to_display + string + '\n'

            if string_to_display == '':
                string_to_display = 'No files to display'

            print(string_to_display)

        elif command_list[1].lower() == 'verify':
            download_flag = 1
            file_name = command_list[2]
            file_name = file_name.replace(' ', '\\ ')
            path = self.cache_directory_path + '/' + command_list[2]
            if os.path.exists(path):
                hasher = filehash.FileHash('md5')
                hasher = hasher.hash_file(path)
                command = 'FileHash verify ' + file_name

                hash_value, size = self.decode_command(command)

                if hash_value == 0 and size == 0:
                    download_flag = 0
                    return 0

                elif hasher == hash_value:
                    print('File exists.')
                    download_flag = 0
                else:
                    print('File changed on server downloading again.')
                    download_flag = 1

            if download_flag:

                command = 'FileHash verify ' + file_name
                hash_value, size = self.decode_command(command)

                if hash_value == 0 and size == 0:
                    return 0

                download_flag = helper_functions.clear_cache(
                    self.cache_directory_path, size, self.cache_size)

                if download_flag:
                    command = 'FileDownload tcp ' + file_name
                    print('Downloading file\n')
                    self.client_socket.send(command.encode('utf-8'))
                    command_list = helper_functions.string_split(command)
                    self.FileDownload(command_list, cache=1)
        else:
            print(
                'Command error. Usage :\nCache verify <filename>\nCache show')
            return None