Ruby中的SHA256哈希值不是预期值

Ruby中的SHA256哈希值不是预期值,ruby,hash,openssl,sha256,Ruby,Hash,Openssl,Sha256,如果我使用OpenSSL对字符串进行SHA256散列hashthisstring123$ > echo -n "hashthisstring123$" | openssl dgst -sha256 我得到这个结果: > 052582d953f79d1e8502fdf063850888ee8426d3a5a6f46c1a0394d16056a51b > 57cd553e7886a2c8eea92926e420d33d315418ffe6b98e5c34f1

如果我使用OpenSSL对字符串进行SHA256散列
hashthisstring123$

> echo -n "hashthisstring123$" | openssl dgst -sha256
我得到这个结果:

> 052582d953f79d1e8502fdf063850888ee8426d3a5a6f46c1a0394d16056a51b
> 57cd553e7886a2c8eea92926e420d33d315418ffe6b98e5c34f1679607207d75
我用Ruby编写了这样的代码:

def self.test1
    str = "hashthisstring123$"
    retval = Digest::SHA256.hexdigest(str)
    return retval
end
def self.test2
    str = "6nQ5t$hWGu8Kpassword123"
    retval = Digest::SHA256.hexdigest(str)
    return retval
end
它给出了以下(预期)结果:

如果我使用OpenSSL对字符串进行SHA256散列
6nQ5t$hWGu8Kpassword123

> echo -n "6nQ5t$hWGu8Kpassword123" | openssl dgst -sha256
我得到这个结果:

> 052582d953f79d1e8502fdf063850888ee8426d3a5a6f46c1a0394d16056a51b
> 57cd553e7886a2c8eea92926e420d33d315418ffe6b98e5c34f1679607207d75
在Ruby中,我有这样一个方法:

def self.test1
    str = "hashthisstring123$"
    retval = Digest::SHA256.hexdigest(str)
    return retval
end
def self.test2
    str = "6nQ5t$hWGu8Kpassword123"
    retval = Digest::SHA256.hexdigest(str)
    return retval
end
它给出了这个(意想不到的)结果:

这里发生了什么?

使用第三个源代码(例如)会告诉您ruby版本是正确的。这意味着问题在于终端/bash解决方案。事实上,如果我们把它简化一点,我们就会发现有些地方出了问题

> echo "6nQ5t$hWGu8Kpassword123"
6nQ5t
$是一个特殊符号,需要转义

> echo "6nQ5t\$hWGu8Kpassword123"
6nQ5t$hWGu8Kpassword123
> echo -n "6nQ5t\$hWGu8Kpassword123" | openssl dgst -sha256
1924de3da90f631c0943a47e4d4d80362d622e9656b5e2861f252a16bffb3d88
使用第三个源代码(例如)会告诉您ruby版本是正确的。这意味着问题在于终端/bash解决方案。事实上,如果我们把它简化一点,我们就会发现有些地方出了问题

> echo "6nQ5t$hWGu8Kpassword123"
6nQ5t
$是一个特殊符号,需要转义

> echo "6nQ5t\$hWGu8Kpassword123"
6nQ5t$hWGu8Kpassword123
> echo -n "6nQ5t\$hWGu8Kpassword123" | openssl dgst -sha256
1924de3da90f631c0943a47e4d4d80362d622e9656b5e2861f252a16bffb3d88

或者,等效地,
echo'6nQ5t$hWGu8Kpassword123'
-单引号内没有变量替换。或者,等效地,
echo'6nQ5t$hWGu8Kpassword123'
-单引号内没有变量替换。