{"id":15294052,"url":"https://github.com/querdos/flask-encryptor","last_synced_at":"2026-03-08T02:02:41.992Z","repository":{"id":57430284,"uuid":"124235819","full_name":"Querdos/Flask-Encryptor","owner":"Querdos","description":"Flask extension helping encrypting users personal files","archived":false,"fork":false,"pushed_at":"2018-03-07T15:15:33.000Z","size":16,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-17T00:35:42.811Z","etag":null,"topics":["cryptography","decryption","encryption","file","flask","upload","users"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Querdos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-07T12:54:48.000Z","updated_at":"2024-11-03T04:38:19.000Z","dependencies_parsed_at":"2022-08-26T02:43:00.051Z","dependency_job_id":null,"html_url":"https://github.com/Querdos/Flask-Encryptor","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Querdos%2FFlask-Encryptor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Querdos%2FFlask-Encryptor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Querdos%2FFlask-Encryptor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Querdos%2FFlask-Encryptor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Querdos","download_url":"https://codeload.github.com/Querdos/Flask-Encryptor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242058084,"owners_count":20065062,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cryptography","decryption","encryption","file","flask","upload","users"],"created_at":"2024-09-30T16:56:05.416Z","updated_at":"2026-03-08T02:02:41.912Z","avatar_url":"https://github.com/Querdos.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flask-Encryptor\n\n## Introduction\nThis Flask extension attempts to ease users' uploaded file encryption.\n\n- PyCrypto (AES.MODE_CBC, AES.MODE_CFB) is used to encrypt/decrypt files\n- Automatically rename and associate encrypted files\n- Can be used to generate a download link and immediately delete the decrypted file\n\nFor now, for database side, it only support SQLAlchemy.\n\n## Requirements and installation\n\nIn order to work, flask-encryptor is based on these following modules:\n\n- flask\n- flask-sqlalchemy\n- pycrypto\n\nTo install Flask-Encryptor, simply::\n\n```bash\n$ pip install flask-encryptor\n```\n\nOr alternatively, you can download the repository and install manually by running::\n\n```bash\n$ git clone https://github.com/Querdos/Flask-Encryptor.git\n$ cd Flask-Encryptor\n$ python setup.py install\n```\n\n## Usage and documentation\nLet's suppose we have simple Flask application with the following structure::\n```\nflask_app_test\n    |-- data\n    |-- template\n    |-- static\n    config.py\n    models.py\n    main.py\n```\n\nThis flask extension concentrate on personal users files encryption, meaning that we will cover two part of a flask \napplication. The user part and the file upload one. You will need to implement a user class (what attributes depend on\nwhat you need for your application), and UploadedFile and Token classes that both inherit (respectively) from BaseFile and BaseToken::\n\n```python\nfrom main import db\nfrom flask_encryptor.models import BaseFile, BaseToken\n\nclass UploadedFile(db.Model, BaseFile):\n    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)\n\nclass Token(db.Model, BaseToken):\n    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)\n    user    = db.relationship('User', back_populates='token')\n\nclass User(db.Model):\n    __tablename__   = 'users'\n    # whatever attribute you want, such as username, password, etc...\n    uploaded_files      = db.relationship('UploadedFile',   backref='users', lazy=True)\n    token               = db.relationship('Token',          uselist=False, back_populates=\"user\")\n```\n\nLet's concentrate now on the main script, main.py. The initialization is quite simple with a few constants to set if \nneeded:\n```python\nfrom flask              import Flask\nfrom flask_sqlalchemy   import SQLAlchemy\nfrom flask_encryptor    import FileEncryptor\n\n# initializating the application\napp = Flask(__name__)\n\n# database configuration\napp.config['SQLALCHEMY_DATABASE_URI']           = '...'\napp.config['SQLALCHEMY_TRACK_MODIFICATIONS']    = False\n\n# initializing the database\ndb  = SQLAlchemy(app)\n\n# FileEncryptor configuration\n# (Note that values here are default one)\napp.config['FILE_ENCRYPTOR_CHUNK_SIZE'] = 64 * 1024\napp.config['FILE_ENCRYPTOR_TMP_DIR']    = tempfile.mkdtemp(prefix='fencryptor_')\napp.config['FILE_ENCRYPTOR_DATA_DIR']   = app.root_path+'/data/uploads'\napp.config['FILE_ENCRYPTOR_GLOBAL_KEY'] = 'aAa8KxQx4Eoxwu41HTaa'\n\n# initializing the file_encryptor object\nfile_encryptor = FileEncryptor(app, db)\n```\nDetails about constants :\n\n| Constant                  | Description                                                 |\n|---------------------------|-------------------------------------------------------------|\n| FILE_ENCRYPTOR_CHUNK_SIZE | Chunk size that will be used for file encryption/decryption |\n| FILE_ENCRYPTOR_TMP_DIR    | Temporary folder that will be used to store decrypted files |\n| FILE_ENCRYPTOR_DATA_DIR   | Folder used to store encrypted file                         |\n| FILE_ENCRYPTOR_GLOBAL_KEY | Global key used to encrypt file informations                |\n\nFor this example, we are supposing to have three routes:\n```python\n    @app.route('/', methods=['GET', 'POST'])\n    def index_action():\n        # here we suppose that the form for uploading a file is in this route\n        if request.method == 'POST':\n            # checking that file parameter is in request form\n            if 'file' not in request.files:\n                flash('No file part', category='upload_errors')\n                return redirect(request.url)\n\n            # retrieving file object and checking that it is not empty\n            file = request.files['file']\n            if file.filename == '':\n                flash('No selected file', category='upload_errors')\n                return redirect(request.url)\n\n            # just send the file object to file_encryptor with the current user (here, optionnaly with\n            # flask_login extension, but you can query your database if you want)\n            infos = file_encryptor.upload_encrypt(file, current_user)\n            current_user.uploaded_files.append(UploadedFile(\n                filename=infos['filename'],\n                realname=infos['realname'],\n                path=app.config['FILE_ENCRYPTOR_DATA_DIR']\n            ))\n            db.session.commit()\n            return redirect(request.url)\n        \n        return render_template('index.html'), 200\n\n    @app.route('/upload/\u003cupload_filename\u003e', methods=['GET', 'POST'])\n    def uploaded_file_action(upload_filename):\n        # retrieving file in database\n        uploaded_file = UploadedFile.query.filter(UploadedFile.filename == filename).first()\n\n        # validation\n        if uploaded_file is None:\n            abort(400)\n\n        # decrypting the filename\n        filename      = file_encryptor.decrypt_file(uploaded_file, current_user)\n\n        # sending file to the user\n        return send_from_directory(\n            directory       = app.config['FILE_ENCRYPTOR_TMP_DIR'],\n            filename        = filename,\n            as_attachment   = True\n        )\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquerdos%2Fflask-encryptor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquerdos%2Fflask-encryptor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquerdos%2Fflask-encryptor/lists"}