2012-12-20 111 views
2

当我从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

为什么,好吗?

回答

11

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

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

明白了。谢谢。 – cdnbcguy

+3

很高兴帮助 - 随时接受这个答案。 ;-) – GargantuChet