为什么 sha1sum 对相同的输入有不同的表现?

twi*_*and 5 linux shell hashing digest

我正在尝试在某些 Java 代码中复制 sha1sum 可执行文件的行为,但是,在此过程中,我发现 sha1sum 在两种情况下输入相同的情况下似乎表现不同。

假设输入 '12345' 没有单引号也没有换行符。

如果我将此数据放入文件 (file1) 并从命令行运行 sha1sum:

$ sha1sum file1
8cb2237d0679ca88db6464eac60da96345513964  file1
Run Code Online (Sandbox Code Playgroud)

但是,如果我这样做,我会得到不同的结果:

$ cat file1 | grep -vi foo | grep -vi bar | sha1sum
2672275fe0c456fb671e4f417fb2f9892c7573ba  -
Run Code Online (Sandbox Code Playgroud)

使用 apache commons-codec jar,我可以读取 file1,获取它的内容,并对内容执行 .shahex() 并获得第一个结果。但是,我需要获得第二个结果(由于遗留代码)并且我无法弄清楚为什么 sha1sum 表现不同,或者 grep 对输入做了什么。

系统运行 CentOS 5.4 和 sha1sum 5.97

任何指针?

whi*_*ark 14

grep 添加换行符。

$ hd file1
00000000  31 32 33 34 35                                    |12345|
00000005
$ grep -vi test <file1 | hd
00000000  31 32 33 34 35 0a                                 |12345.|
00000006
Run Code Online (Sandbox Code Playgroud)

要获得相同的结果,您应该\n 在输入末尾添加一个(如果不存在)。


msw*_*msw 8

确认序列相同,首先:

$ cat file1 | grep -vi foo | grep -vi bar > /tmp/junk
$ cmp file1 /tmp/junk
Run Code Online (Sandbox Code Playgroud)

否则你只会追逐你的尾巴。