gpt4 book ai didi

ruby - 为什么我得到的不是我预期的哈希值?

转载 作者:数据小太阳 更新时间:2023-10-29 08:16:33 24 4
gpt4 key购买 nike

当我从 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 字符串中指定。尝试使用 -n 以便 echo 跳过换行符:

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

关于ruby - 为什么我得到的不是我预期的哈希值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13965092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com