簡體   English   中英

django 文件存儲前加密

[英]django encrypt files before storage

所以我想在存儲在 django 之前加密文件並在檢索時解密它們。

我正在使用自定義存儲 class 和密碼模塊。

import hashlib
import os
import uuid

import django.core.files.storage as storage
from cryptography.fernet import Fernet
from django.conf import settings
from django.core.files import File


class DefaultStorage(storage.FileSystemStorage):
    def __init__(self):
        super(DefaultStorage, self).__init__()
        self.encryptor = Fernet(settings.ENCRYPTION_KEY)

    def _save(self, name, content):
        encrypted = self.encryptor.encrypt(content.file.read())
        content.file.write(encrypted)
        print(content.file.read() == encrypted)
        return super(DefaultStorage, self)._save(name, content)

    def _open(self, name, mode='rb'):
        encrypted = open(self.path(name), mode).read()
        return File(self.encryptor.decrypt(encrypted))

    def get_available_name(self, name, max_length=None):
        # we return a hash of the file given,
        # in case we miss out on uniqueness, django calls
        # the get_alternative_name method
        dir_name, file_name = os.path.split(name)
        file_root, file_ext = os.path.splitext(file_name)

        file_root = hashlib.md5(file_root.encode()).hexdigest()
        name = os.path.join(dir_name, file_root + file_ext)
        return super(DefaultStorage, self).get_available_name(name, max_length)

    def get_alternative_name(self, file_root, file_ext):
        # we insert a random uuid hex string into the given
        # file name before returning the same back
        return '%s%s%s' % (file_root, uuid.uuid4().hex, file_ext)

我在這里覆蓋了_save_open方法,但是 class 沒有按預期工作。

在保存方法下,我想加密文件的內容,但是當我打印這個時:

print(content.file.read() == encrypted)

它返回假。 這意味着文件甚至沒有被加密。 我在這里做錯了什么? _open方法也一樣? 有人可以幫幫我嗎? 多謝!

編輯

    def _save(self, name, content):
        initial = content.file
        encrypted = self.encryptor.encrypt(content.file.read())
        content.file.write(encrypted)
        # the temporary file is already stored in the path
        # content.file.name
        # we need to write to this directory
        print(content.file.seek(0) == encrypted)
        return super(DefaultStorage, self)._save(name, content)

此打印語句也返回 False

編輯 2

當我刪除現有的_open方法時,我發現仍然可以檢索該文件(無需解密)。 這意味着該文件甚至沒有被加密。 知道為什么嗎? 有人可以幫幫我嗎?

_save 方法:

def _save(self, name, content):
        encrypted = self.encryptor.encrypt(content.file.read())
        content.file.write(encrypted)
        return super(DefaultStorage, self)._save(name, content)

通常, content.file.read()將 cursor 留在文件末尾。

如果要再次讀取文件,需要將cursor移到開頭; 否則, .read()只會返回空,因為文件末尾沒有任何內容。

content.file.seek(0)                     # places cursor at the start
print(content.file.read() == encrypted)  # reads file again

這可以使用content.file.seek(0) (將 cursor 放在 position 0 處)或通過再次關閉並打開文件來完成。


但要小心,讀取和寫入同一個文件可能會很棘手並導致問題。 而是寫入輔助文件,然后用新文件替換原始文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM