Hexdump 的类型
问题是 xxd 需要一个格式化的十六进制转储,但您发送的是一串十六进制数字。这就是 xxd 所说的“普通 hexdump”。
格式化的 hexdump 如下所示(Joseph Campbell 引用):
$ printf "Computers are like old testament gods; lots of rules and no mercy." | xxd
00000000: 436f 6d70 7574 6572 7320 6172 6520 6c69 Computers are li
00000010: 6b65 206f 6c64 2074 6573 7461 6d65 6e74 ke old testament
00000020: 2067 6f64 733b 206c 6f74 7320 6f66 2072 gods; lots of r
00000030: 756c 6573 2061 6e64 206e 6f20 6d65 7263 ules and no merc
00000040: 792e y.
默认情况下,xxd 将创建这种 hexdump。
对十六进制字符串使用 -p
根据xxd 手册页,-r
期望读取格式化的 hexdump:
-r | -revert
reverse operation: ...
Use the combination -r -p to read plain hexadecimal
dumps without line number information and without a particular column layout.
...
重申一下,默认情况下,xxd -r
需要一个格式化的 hexdump。要使用 xxd 读取或写入“普通 hexdump”,请使用该-p
选项。请注意, xxd 期望标志出现在参数之前,因此如果以标志结尾,您将得到意想不到的行为。
确认
如果您可以看到运行 openssl 两次(最初使用-binary
)并使用 xxd 运行 openssl 会产生相同的结果。
$ openssl dgst -sha256 <( printf "Hello World!" ) | awk '{print $2}' > myHashHex;
$ xxd -r -p myHashHex | openssl dgst -sha256
(stdin)= 61f417374f4400b47dcae1a8f402d4f4dacf455a0442a06aa455a447b0d4e170
$ openssl dgst -sha256 -binary <( printf "Hello World!" ) | openssl dgst -sha256
(stdin)= 61f417374f4400b47dcae1a8f402d4f4dacf455a0442a06aa455a447b0d4e170