【问题标题】:Why am I getting a hash other than the one I expected?为什么我得到的哈希值不是我预期的?
【发布时间】:2012-12-20 04:00:14
【问题描述】:

当我从http://ruby.about.com/od/advancedruby/ss/Cryptographic-Hashes-In-Ruby.htm输入此代码时

#!/usr/bin/env ruby
require 'digest'

password = "A user's password"
hash = Digest::SHA1.hexdigest(password)
puts hash

# This will produce the hash
# 62018390552aaba3d344e3b43bfa14e49e535dfc

我得到了他们说我会的答案。

但是当我输入这个shell命令时

echo "A user's password" | openssl dgst -sha1 -hex

我得到 95f33732bafc1744bf24e0cae4e014ab2e5f1580

为什么?

【问题讨论】:

    标签: ruby openssl sha1


    【解决方案1】:

    您的命令行示例包含一个换行符,它没有在 Ruby 字符串中指定。尝试使用-n 以便echo 跳过换行符:

    $ echo "A user's password" | openssl dgst -sha1 -hex
    95f33732bafc1744bf24e0cae4e014ab2e5f1580
    $ echo -n "A user's password" | openssl dgst -sha1 -hex
    62018390552aaba3d344e3b43bfa14e49e535dfc
    

    【讨论】:

      猜你喜欢
      • 2015-07-11
      • 1970-01-01
      • 2011-12-27
      • 2022-01-15
      • 2011-08-18
      • 1970-01-01
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多