def getKey(username):
        """
        Returns the symmetric key to the caller

        param username: User's who's folder has to be dealt with and read into

        return: Symmetric key
        """

        sym_key = None
        if username != "admin":
            # Read private key of user
            folder_name = username + "_files/"
            filepath = folder_name + PRIV_KEY_FILE
            priv_key = serialization.load_pem_private_key(
                readFile(filepath), password=None, backend=default_backend())

            # Read and decrypt the encrypted symmetric key
            filepath = folder_name + SHARED_KEY_FILE
            sym_key = priv_key.decrypt(
                readFile(filepath),
                padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
                             algorithm=hashes.SHA256(),
                             label=None))
        else:  # if it's the admin, it's already stored in plaintext
            sym_key = readFile(ADMIN_FOLDER + "/" + SHARED_KEY_FILE)

        return sym_key
Esempio n. 2
0
def _getCurrUsersPass_():
    """
    Gets the current users and passwords and returns a dictionary with the users and passwords. If file doesn't exist, it creates it with admin's details

    return: dict with users array (accesed by 'users' key) and passwords array (accessed by 'passwords' key)
    """

    # if file doesn't exist, create file with admin details
    if not os.path.exists(USERS_FILE):
        ret = {'users': ["admin"], 'passwords': [ADMIN_PASS]}
        saveFile(USERS_FILE,
                 "admin" + USER_PASS_DELIM + ADMIN_PASS + USER_DELIM)
        return ret

    # get users file contents
    curr_users_contents = readFile(USERS_FILE)
    users_passwords = curr_users_contents.split(USER_DELIM)
    ret = {'users': [], 'passwords': []}  # init users and pass dict

    # seperate users from passwords
    for userPass in users_passwords:
        if not userPass:  # if string is empty we're done
            break
        tmpArr = userPass.split(USER_PASS_DELIM)
        ret['users'].append(tmpArr[0])
        ret['passwords'].append(tmpArr[1])

    return ret
    def __init_key_fernet__(self):
        """
        Initialises a new key if it doesn't exist

        return: None
        """

        # read key if key exists else generate a new one
        if os.path.exists(ADMIN_FOLDER + "/" + SHARED_KEY_FILE):
            self.key = readFile(ADMIN_FOLDER + "/" + SHARED_KEY_FILE)
            self.fernet = Fernet(self.key)  # create fernet obj
        else:
            self.__generate_new_key__()
Esempio n. 4
0
    def upload_file(self, filename, fernet):
        """
        Saves and uploads an encrypted version of the file named filename, if the file exists locally and no duplicate is on the drive

        param filename: Name of the file to be uploaded
        param fernet: The fernet object created in KeyManagementSystem class

        return: Filename of encrypted file, if file uploaded successfully, else None
        """

        # add path to filename if needed
        prevFilename = filename
        filename = self.normaliseFilename(filename)

        # return None if file doesn't exist
        if not os.path.exists(filename):
            print("Error 404: File specified not found")
            return None

        # get the drive's main folder ID
        folderID = self._get_file_ID_(DRIVE_FOLDER, DRIVE_ROOT_ID)

        # Create and share folder on drive if it doesnt exist, and get folderID
        if folderID == None:
            folderID = self.__create_share_folder__()
            print("Created shared folder on drive")

        # Check if file with identical name is already on the drive
        encryptedFilename = prevFilename + ENCR_EXTENSION
        encryptedFileID = self._get_file_ID_(encryptedFilename, folderID)
        if encryptedFileID != None: # implies there's a file identically named
            print("This file is already up on the drive!")
            return None

        # Save encrypted file locally
        plain_text = readFile(filename) # getting file contents
        cipher_text = GoogleDriveAccess._encrypt_(plain_text, fernet)
        normEncryptedFilename = encryptedFilename
        normEncryptedFilename = self.normaliseFilename(normEncryptedFilename)
        saveFile(normEncryptedFilename, cipher_text) # Saving file locally

        # Upload encrypted file to google drive folder
        fileToUpload = self.drive.CreateFile({"title": encryptedFilename, "parents": [{"id": folderID}]})
        fileToUpload.SetContentFile(normEncryptedFilename) # set file contents
        fileToUpload.Upload()

        # Delete local version of encrypted file here
        os.remove(normEncryptedFilename)

        return encryptedFilename
Esempio n. 5
0
def _getNewUsers_():
    """
    Reads contents of the new user files and gives back an array with new usernames. Create empty file if file doesn't exist

    return: Array with new usernames
    """

    # Create emtpy file if it doesn't exist
    if not os.path.exists(N_USERS_FILE):
        saveFile(N_USERS_FILE, "")
        return []

    # get new user's usernames
    new_users_contents = readFile(N_USERS_FILE)
    new_users = new_users_contents.split(USER_DELIM)

    return new_users[:-1]  # ignore last empty element