Dump a ```sha``` checksum output to disk in binary format instead of plaintext hex in bash

孤者浪人 提交于 2020-03-26 15:10:36

问题


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

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.


回答1:


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

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


来源:https://stackoverflow.com/questions/60490752/dump-a-sha-checksum-output-to-disk-in-binary-format-instead-of-plaintext-h

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!