将"sha"校验和输出以二进制格式而不是bash中的纯文本十六进制格式转储到磁盘 [英] Dump a ```sha``` checksum output to disk in binary format instead of plaintext hex in bash
问题描述
我想将sha
校验和(这里使用sha1sum
,我知道它不用于加密,请不要担心这对我的需求很好)从bash脚本转储到磁盘.到目前为止,我已经能够将其丢弃(没有任何额外的...).但是我没有设法以二进制格式转储它,而是只得到了一个纯文本的十六进制转储:
I want to dump a sha
checksum (here using sha1sum
, I know it is not to be used for crypto, do not worry this is fine for my need) to disk from a bash script. So far, I am able to get it dumped (without anything extra...). But I do not manage to dump it in binary format, instead I am only getting a plaintext hex dump:
$ 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个字节,即真正以二进制格式转储?
For example here, if I am right, the output of sha1
is truly 20 bytes long, which takes 40 chars to represent in hex printout (i.e. 40 bytes with encoding the hex printout in ASCII, and this is the reason why xxd can transcript all bytes of the file as chars 0-f), and this is what is present on my file. How can I change this so that the size of test
is 20 bytes on disk, i.e. truly dumped in binary format?
很抱歉,如果我错过了执行此操作的简单方法,那么我已经搜索了很长时间(可能是错误的问题),但没有找到明确的答案.
Sorry if I missed an easy way to do this, I have been googling (probably the wrong question) for quite some time without finding a clear answer.
推荐答案
找到了!在论坛/手册页上进行了更多挖掘之后,xxd
可以为我解决(在这里找到:
Found it! After a bit more digging in forums / man pages, xxd
is able to solve it for me (found it here: https://gist.github.com/dstebila/6194f90097c43c091dd2 ):
# Convert hex to binary using xxd's reverser in plain hexdump style
xxd -r -ps
因此xxd
能够获取一个用ASCII十六进制转储的文件,并将其转换为二进制内容:
So xxd
is able to take a file filled with ASCII hexdump, and convert it to a binary content:
$ 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
这篇关于将"sha"校验和输出以二进制格式而不是bash中的纯文本十六进制格式转储到磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!