cal*_*eld 6 encryption ruby-on-rails carrierwave
我正在尝试使用Lockbox加密和解密文件上传,但是文档假定我不知道我所缺少的知识,并且维护该回购协议的人似乎很忙,无法提供任何帮助。有人可以帮我理解吗?
该文档说,主密钥用于创建存储在模型中的密码。它说,此密码的架构字段应为text或,binary并且命名方式类似于attribute_ciphertext,而不是单纯的attribute。结果,我的模型如下所示:
class IdentityDocument < ApplicationRecord
belongs_to :user, foreign_key: :user_id
validates :document_front_ciphertext, :document_back_ciphertext, presence: true
mount_uploader :document_front_ciphertext, IdentityDocumentsUploader
mount_uploader :document_back_ciphertext, IdentityDocumentsUploader
end
Run Code Online (Sandbox Code Playgroud)
根据关于CarrierWave的部分,加密就像encrypt向我的上传器添加一样容易:
class IdentityDocumentsUploader < CarrierWave::Uploader::Base
encrypt
end
Run Code Online (Sandbox Code Playgroud)
控制器创建模型:
def create
@id_doc = current_user.identity_documents.new(id_doc_params)
end
def id_doc_params
params.require(:identity_document).permit(:document_front_ciphertext, :document_back_ciphertext)
end
Run Code Online (Sandbox Code Playgroud)
然后视图将参数提交给控制器:
<%= form_for @id_doc do |f| %>
<%= f.label :document_front_ciphertext, "Front of driver's license or other state ID" %>
<%= f.file_field :document_front_ciphertext %>
<%= f.label :document_back_ciphertext, "Back of driver's license or other state ID" %>
<%= f.file_field :document_back_ciphertext %>
<%= f.submit "Upload", data: { disable: true } %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这样做的SQL输出看起来未加密(我想这是预期的吗?也许文件上传不需要加密的数据库字段,并且_ciphertext我的架构中不需要?):
INSERT INTO "identity_documents" ("user_id", "document_front_ciphertext", "document_back_ciphertext") VALUES ($1, $2, $3) RETURNING "id" [["user_id", 1], ["document_front_ciphertext", "california-drivers-license.jpg"], ["document_back_ciphertext", "2016-11-25-license-4.jpg"]]
Run Code Online (Sandbox Code Playgroud)
在控制台中查询此模型如下所示:
id = IdentityDocument.find 4
#<IdentityDocument id: 4, user_id: 1, document_front_ciphertext: "california-drivers-license.jpg", document_back_ciphertext: "2016-11-25-license-4.jpg">
id.document_front_ciphertext
#<IdentityDocumentsUploader:0x00007fec0cfad980 @model=#<IdentityDocument id: 4, user_id: 1, document_front_ciphertext: "california-drivers-license.jpg", document_back_ciphertext: "2016-11-25-license-4.jpg">, @mounted_as=:document_front_ciphertext, @file=#<CarrierWave::SanitizedFile:0x00007fec0cfad098 @file="/Users/mkeable/code/fractionclub/public/uploads/identity_document/document_front_ciphertext/4/california-drivers-license.jpg", @original_filename=nil, @content_type=nil, @content=nil>, @filename=nil, @cache_id=nil, @versions={}, @format=nil, @storage=#<CarrierWave::Storage::File:0x00007fec0cfad7c8 @uploader=#<IdentityDocumentsUploader:0x00007fec0cfad980 ...>, @cache_called=nil>>
Run Code Online (Sandbox Code Playgroud)
出于某种原因,此Uploader包含整个模型,但通过@file或只能获得第一张图片@mounted_as。
这使我可以解密并向管理员显示图像。文档说要在我的控制器中执行以下操作:
def id_document
send_data user.identity_document.document_front_ciphertext.read, type: user.identity_document.document_front_ciphertext.content_type
end
Run Code Online (Sandbox Code Playgroud)
...但是,这会导致Errno::ENAMETOOLONG错误。如果我改变了read对path在结束send_data的第一个参数,我的看法与......我什么填满认为是一个加密的文件?看起来像这样:
/Users/me/code/project/public/uploads/identity_document/document_front_ciphertext/1/OWkN7fKEetY2vBWXxkAdWFr96aKOF7Abt8Q7E5cNWvK7zALMwUoH63ZpvYnvYQ7mLqbf32Imqdt+85WpyLjPKf+xmEjHIJ3U7PQNviMDJsBl+OHQRLsfurqNMY2IJXYvmoZLuFN0MykG7BEj9+aKodN0qxlQIHfMQEPBzdQ2G5iss46l7qlyImXt/BH64LYbDxxUqTfCB8B6N7UEZTWy7o8v2ggchQVtleQtxeYsSQoknPlXwa5eEe604c+CHWBXMjisF4LwP+cGcL+j8oIEg+vqwZo3Firxh367b+wzutBWGPNEzNVVtvSKEPToxu9aFjJrBPLwR3LTl8bLqap88i2MqkmtUtAT5UL4514OdqGh5dBduGnpAFOTMVCW4lBPZ7XGltjwCbZ+nLdCAQcT/xgCNRZ8qNpPzTOMsUbNJktwiM5J2J7YfX/H0lhG0CBxFxoGc0FoXs1+Jx82pCHJAdoemk4HNTXIRR3F0qgoVAi16F2wz2ofCJ0DQZT9h5witwxg3XpMYitUw/UnGCoCQpzxV60scnaCRb05wavDRR/97XQvFpBWxIFOjt0MJEzMCpTpCa51KT94a9Q9dL+P04MSQNQ7XVDRvqdsp1LYXKmoxAeBhdZQeOTjBvyLyDvI+TBkqQl/WwsgaabeFDw0O8ovo46yVl0OjjHQZOYmFgRz8VHX0iF6xfiqPRXnZlTB/BbhbRocnEGXav1DpwhLeEFtm/2OigZglBNtbI7B2t6EsEq87/eFHMRjbfUHGAovxfBlc3ZFXS9IlGFmhr5NdhBbEW/oaXc0ZpvR7tTx6H+yyvv6HFAa0tSwV6JThCc3OlvMGpykhQ3R58J5E1BYe/NJrKM3VlvTBptQEP4+RFDct83sthbCYwhlclAeAPDLPjbop6CaA35iV+9skkb1cpGlj3NLx7hY66ZloVDiE8JhCLD/sQTcvZ0zQ3/r7VFeXydiQZpu4QTA9v0Q+Cq5X+qMe1g5cc3cpfP90LfqfIag8Z+Scl18cNteqArl0Ex/lqQktLVINfZW5J+jvXZTV2Al9F5XW14NGlznXe9R4TT4XnlOzHDodThboLtMngWlIf3v/aywevcOE1TtqmxdwGu5wbsaffzKdZGDaxTWuiapMRn4IJUp7zYxUOSAipL7x++Ski+su5bgbUw/Gn2KFomCYRwwvEH1wVKl6BDajL29K6LEFF/jLHEdvwVhj22G0II747xkKtybHStSRrx+3vV...
Run Code Online (Sandbox Code Playgroud)
那么...我该如何显示这些图像?我走的路是正确的;)但是我很困惑。