必须指定iv attr_encrypted,如何检查登录名和密码

您好,有以下型号:

class User  secret_key attr_encrypted :password, :key => secret_key [...] end 

我将4个cols添加到我的模型中

 rails g migration AddEncryptedColumnsToUser encrypted_email:string encrypted_password:string encrypted_email_iv:string encrypted_password_iv:string 

现在我想检查电子邮件和密码是否正确,但我不知道如何处理:

 secret_key_data = "my big secret 32 bits key " email = User.encrypt_email("test@test.com", key: secret_key_data) password = User.encrypt_password("test", key: secret_key_data) User.where('(encrypted_email) LIKE ? AND (encrypted_password) LIKE ? ', email,password) 

但当我这样做时:

 email = User.encrypt_email("test@test.com", key: secret_key_data) 

我收到了这个错误:

 ArgumentError: must specify an iv 

问题是, 我从哪里获取iv,如果登录是正确的,我如何加密以便能够在数据库中进行测试?

非常感谢!

一些较旧版本的attr_encrypted具有古怪(或没有)初始向量(iv)处理。 请注意您正在使用的attr_encrypted版本。 我想这是你的问题。 使用Rails v4.1.16尝试attr_encrypted v3.1.0

在您的迁移中:

 class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :username, null: false t.string :encrypted_email t.string :encrypted_email_iv t.string :encrypted_password t.string :encrypted_password_iv t.timestamps end end end 

在你的模型中:

 class User < ActiveRecord::Base attr_encrypted :email, :password, key: 'Some 256-bit key here' end 

在你的控制器中:

  private # Never trust parameters from the scary internet, only allow the white list through. def server_params params.require(:server).permit(:username, :email, :password) end 

这个版本/配置适合我。