Ruby 中的 SHA256 哈希不是预期值
SHA256 Hash in Ruby not expected value
如果我使用 OpenSSL 对字符串进行 SHA256 哈希 hashthisstring123$
> echo -n "hashthisstring123$" | openssl dgst -sha256
我得到这个结果:
> 052582d953f79d1e8502fdf063850888ee8426d3a5a6f46c1a0394d16056a51b
我在 Ruby 中有执行此操作的代码,如下所示:
def self.test1
str = "hashthisstring123$"
retval = Digest::SHA256.hexdigest(str)
return retval
end
它给出了这个(预期的)结果:
> Util.test1
=> "052582d953f79d1e8502fdf063850888ee8426d3a5a6f46c1a0394d16056a51b"
如果我使用 OpenSSL 对字符串进行 SHA256 哈希 6nQ5t$hWGu8Kpassword123
> echo -n "6nQ5t$hWGu8Kpassword123" | openssl dgst -sha256
我得到这个结果:
> 57cd553e7886a2c8eea92926e420d33d315418ffe6b98e5c34f1679607207d75
在Ruby中,我有一个这样的方法:
def self.test2
str = "6nQ5t$hWGu8Kpassword123"
retval = Digest::SHA256.hexdigest(str)
return retval
end
它给出了这个(意外的)结果:
> Util.test2
=> "1924de3da90f631c0943a47e4d4d80362d622e9656b5e2861f252a16bffb3d88"
这是怎么回事?
使用第三个来源(例如this)会告诉您 ruby 版本是正确的。这意味着问题出在您的 terminal/bash 解决方案上。事实上,如果我们稍微简化一下,我们就会发现有问题。
> echo "6nQ5t$hWGu8Kpassword123"
6nQ5t
$是特殊符号,需要转义。
> echo "6nQ5t$hWGu8Kpassword123"
6nQ5t$hWGu8Kpassword123
> echo -n "6nQ5t$hWGu8Kpassword123" | openssl dgst -sha256
1924de3da90f631c0943a47e4d4d80362d622e9656b5e2861f252a16bffb3d88
如果我使用 OpenSSL 对字符串进行 SHA256 哈希 hashthisstring123$
> echo -n "hashthisstring123$" | openssl dgst -sha256
我得到这个结果:
> 052582d953f79d1e8502fdf063850888ee8426d3a5a6f46c1a0394d16056a51b
我在 Ruby 中有执行此操作的代码,如下所示:
def self.test1
str = "hashthisstring123$"
retval = Digest::SHA256.hexdigest(str)
return retval
end
它给出了这个(预期的)结果:
> Util.test1
=> "052582d953f79d1e8502fdf063850888ee8426d3a5a6f46c1a0394d16056a51b"
如果我使用 OpenSSL 对字符串进行 SHA256 哈希 6nQ5t$hWGu8Kpassword123
> echo -n "6nQ5t$hWGu8Kpassword123" | openssl dgst -sha256
我得到这个结果:
> 57cd553e7886a2c8eea92926e420d33d315418ffe6b98e5c34f1679607207d75
在Ruby中,我有一个这样的方法:
def self.test2
str = "6nQ5t$hWGu8Kpassword123"
retval = Digest::SHA256.hexdigest(str)
return retval
end
它给出了这个(意外的)结果:
> Util.test2
=> "1924de3da90f631c0943a47e4d4d80362d622e9656b5e2861f252a16bffb3d88"
这是怎么回事?
使用第三个来源(例如this)会告诉您 ruby 版本是正确的。这意味着问题出在您的 terminal/bash 解决方案上。事实上,如果我们稍微简化一下,我们就会发现有问题。
> echo "6nQ5t$hWGu8Kpassword123"
6nQ5t
$是特殊符号,需要转义。
> echo "6nQ5t$hWGu8Kpassword123"
6nQ5t$hWGu8Kpassword123
> echo -n "6nQ5t$hWGu8Kpassword123" | openssl dgst -sha256
1924de3da90f631c0943a47e4d4d80362d622e9656b5e2861f252a16bffb3d88