【问题标题】:Digest::SHA1::hexdigest producing different hashes for same stringDigest::SHA1::hexdigest 为相同的字符串产生不同的哈希值
【发布时间】:2012-06-19 17:48:52
【问题描述】:

我正在尝试进行简单的用户身份验证,但会话控制器中对 Digest::SHA1::hexdigest 的调用产生的哈希值与用户模型中的哈希值不同。

user.rb:

    class User < ActiveRecord::Base

    before_save {|user| user.password = Digest::SHA1.hexdigest(user.password)}
    ...

    end

sessions_controller.rb

    def create
        user = User.where(:username => params[:username], :password => Digest::SHA1.hexdigest(params[:password])).first
    end

有没有办法解决这个问题?

例如,字符串“密码”在我尝试登录时显示为“5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8”。

但是,在数据库中,相同的字符串“密码”显示为: "353e8061f2befecb6818ba0c034c632fb0bcae1b"

【问题讨论】:

  • 你有真实的例子吗?相同的字符串生成不同的哈希?
  • @airplaneman19 除非您使用某种盐来加密它,否则极不可能。但在你的情况下,我猜你不是。我怀疑您的 param[:password] 与 user.password 相同,我猜有一些额外的字符,例如 spaces 等导致这种情况

标签: ruby-on-rails string encryption sha1


【解决方案1】:

before_save 在您保存的任何时候运行,因此创建您加密,然后在您正在加密的任何更新(已经加密的更新)上再次运行。尝试使用 before_create。

【讨论】:

    猜你喜欢
    • 2016-08-17
    • 2014-11-30
    • 2011-11-12
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 2011-11-02
    • 1970-01-01
    相关资源
    最近更新 更多