将“ sha”校验和输出以二进制格式而不是bash中的明文十六进制形式输出到磁盘

问题描述 投票:0回答:1

我想将sha校验和(这里使用sha1sum,我知道它不用于加密,请不要担心这对我的需求很好)从bash脚本转储到磁盘。到目前为止,我已经能够将其丢弃(没有任何额外的...)。但是我无法以二进制格式转储它,而是只得到了一个纯文本的十六进制转储:

$ echo "bla" | sha1sum | awk '{print $1}' | head -c-1 > test
$ ls -lrth test
-rw-r--r-- 1 jrlab jrlab 40 mars   2 15:02 test
$ xxd test
00000000: 3034 3735 3935 6430 6661 6539 3732 6662  047595d0fae972fb
00000010: 6564 3063 3531 6234 6134 3163 3761 3334  ed0c51b4a41c7a34
00000020: 3965 3063 3437 6262                      9e0c47bb

例如,如果我是对的,sha1的输出实际上是20个字节长,用40个字符表示十六进制打印输出(即,用ASCII编码十六进制打印输出的40个字节,这就是为什么xxd可以将文件的所有字节记录为字符0-f),这就是我的文件中存在的内容。如何更改此值,以使test的大小在磁盘上为20个字节,即真正以二进制格式转储?

很抱歉,如果我错过了执行此操作的简便方法,我已经搜索了很长时间(可能是错误的问题),但没有找到明确的答案。

bash binary dump sha
1个回答
2
投票

找到了!在论坛/手册页上进行更多的挖掘之后,xxd可以为我解决(在这里找到它:https://gist.github.com/dstebila/6194f90097c43c091dd2):

# Convert hex to binary using xxd's reverser in plain hexdump style
xxd -r -ps

因此xxd能够获取一个用ASCII十六进制转储的文件,并将其转换为二进制内容:

$ echo "48454C4C4F" > test.hex
$ xxd test.hex
00000000: 3438 3435 3443 3443 3446 0a              48454C4C4F.
$ xxd -r -ps test.hex > test.bin
$ xxd test.bin
00000000: 4845 4c4c 4f                             HELLO
$ ls -lrth test.hex
-rw-r--r-- 1 jrlab jrlab 11 mars   2 15:44 test.hex
$ ls -lrth test.bin
-rw-r--r-- 1 jrlab jrlab 5 mars   2 15:44 test.bin
© www.soinside.com 2019 - 2024. All rights reserved.