Search code examples
bashmultiline

openssl sha256 binary digest piped through fxxd -p is output on multiple lines


I use this command to give me the output from openssl without the (stdin)= the beginning.

openssl x509 -noout -modulus -in certfile.crt | openssl sha1 -binary | xxd -p

with output

7857b35259019acc7484201958ac7e622c227b68

If I change openssl to create a sha256 digest, xxd prints it over two lines

openssl x509 -noout -modulus -in certfile.crt | openssl sha256 -binary | xxd -p

with output

b274c19ac31cc7893dc2297804a2ca666fe168d5cd5eb4d4b6c47616bad9
8996

How can I write that output on line one?

b274c19ac31cc7893dc2297804a2ca666fe168d5cd5eb4d4b6c47616bad98996

Is it something else I have to do with xxd now that the digest is longer or is there a need to pipe it through some other command to get the combined output?


Solution

  • As usual there are several ways. The first general solution which came into my mind is this:

    printf "%s" $( openssl x509 -noout -modulus -in certfile.crt | openssl sha256 -binary | xxd -p )
    

    Of course, if the output is less than 256, you could use xxd -p -c 256 as stated by tshiono.

    Another solution for this special case with openssl would be this:

    openssl x509 -noout -modulus -in certfile.crt | openssl sha256 -r
    

    or

    openssl x509 -noout -modulus -in certfile.crt | openssl sha256 -hex
    

    but both are not exactly like the output you want. The hex string is in the output, but padded before or after which can be cut off, by piping to the command cut -d" " -f 1 or cut -d" " -f 2 for the removal of the prefix or postfix.