如果数据是从文件而不是直接在命令行上回显的,为什么 sha1 会给我不同的哈希值?
Why does sha1 give me different hash if data is echoed from file instead of directly on command line?
我想弄清楚为什么当 运行 一个字符串通过 sha1
算法时我没有得到相同的哈希值。
请考虑这些结果:
echo moosecodes | shasum
538f5d940a8f1aeabde1d5c6da4ebae1230ba5da -
echo -n moosecodes | shasum
c09129372713d1c7005f4aa1d50bf598912c473a -
temp.txt 包含没有换行的字符串 moosecodes
,但是从文件回显字符串时哈希不同:
echo temp.txt | shasum
c3ec5f2c30a4198dc4e0323101441da0bcdd2aa9 -
echo -n temp.txt | shasum
45ebed19db9cfb3cea503d6b62a50ffe6b30247c -
任何人都可以向我解释为什么会这样吗?起初我认为这与文件附加了元数据这一事实有关,但在这种情况下,我只是将其内容回显到哈希器,所以它不会与前两个相同我在上面展示的例子?
你是 运行echo temp.txt | shasum
,而不是 cat temp.txt | shasum
或 shasum < temp.txt
。您没有测试文件 temp.txt
内容的 shasum,而是不小心获取了字符串 "temp.txt".
的 shasum
# Your first example
$ echo moosecodes | shasum
538f5d940a8f1aeabde1d5c6da4ebae1230ba5da *-
$ echo -n moosecodes | shasum
c09129372713d1c7005f4aa1d50bf598912c473a *-
# Now prepare the file...
$ echo -n moosecodes > temp.txt
# But accidentally take the shasum of the string "temp.txt",
# with or without the newline.
$ echo temp.txt | shasum
c3ec5f2c30a4198dc4e0323101441da0bcdd2aa9 *-
$ echo -n temp.txt | shasum
45ebed19db9cfb3cea503d6b62a50ffe6b30247c *-
# However, you can use the "cat" command to pipe the text...
$ cat temp.txt | shasum
c09129372713d1c7005f4aa1d50bf598912c473a *-
# Or properly redirect from a file using < ...
$ shasum < temp.txt
c09129372713d1c7005f4aa1d50bf598912c473a *-
# Or even just pass the filename into shasum directly.
$ shasum temp.txt
c09129372713d1c7005f4aa1d50bf598912c473a *temp.txt
我想弄清楚为什么当 运行 一个字符串通过 sha1
算法时我没有得到相同的哈希值。
请考虑这些结果:
echo moosecodes | shasum
538f5d940a8f1aeabde1d5c6da4ebae1230ba5da -
echo -n moosecodes | shasum
c09129372713d1c7005f4aa1d50bf598912c473a -
temp.txt 包含没有换行的字符串 moosecodes
,但是从文件回显字符串时哈希不同:
echo temp.txt | shasum
c3ec5f2c30a4198dc4e0323101441da0bcdd2aa9 -
echo -n temp.txt | shasum
45ebed19db9cfb3cea503d6b62a50ffe6b30247c -
任何人都可以向我解释为什么会这样吗?起初我认为这与文件附加了元数据这一事实有关,但在这种情况下,我只是将其内容回显到哈希器,所以它不会与前两个相同我在上面展示的例子?
你是 运行echo temp.txt | shasum
,而不是 cat temp.txt | shasum
或 shasum < temp.txt
。您没有测试文件 temp.txt
内容的 shasum,而是不小心获取了字符串 "temp.txt".
shasum
# Your first example
$ echo moosecodes | shasum
538f5d940a8f1aeabde1d5c6da4ebae1230ba5da *-
$ echo -n moosecodes | shasum
c09129372713d1c7005f4aa1d50bf598912c473a *-
# Now prepare the file...
$ echo -n moosecodes > temp.txt
# But accidentally take the shasum of the string "temp.txt",
# with or without the newline.
$ echo temp.txt | shasum
c3ec5f2c30a4198dc4e0323101441da0bcdd2aa9 *-
$ echo -n temp.txt | shasum
45ebed19db9cfb3cea503d6b62a50ffe6b30247c *-
# However, you can use the "cat" command to pipe the text...
$ cat temp.txt | shasum
c09129372713d1c7005f4aa1d50bf598912c473a *-
# Or properly redirect from a file using < ...
$ shasum < temp.txt
c09129372713d1c7005f4aa1d50bf598912c473a *-
# Or even just pass the filename into shasum directly.
$ shasum temp.txt
c09129372713d1c7005f4aa1d50bf598912c473a *temp.txt