Linux command piping in openssl to use string input

Issue

I have a shell script where a file path $path have some text which I encrypt as below and it works:

content_sha256="$(openssl dgst -binary -sha256 < $path | openssl enc -e -base64)";

The value of variable content_sha256 works correctly.

Now, I have a string $body which I want to encrypt. I am trying below but it gives me entirely different result.

content_sha256="$(echo $body | openssl dgst -sha256 | openssl enc -e -base64)";

Am I piping something wrong or option for openssl should be different?

Solution

Correct answer below
content_sha256="$(echo $body | openssl dgst -binary -sha256 | openssl enc -e -base64)";

Points to note:

  1. Include -binary option.
  2. Instead of redirection of file content as input, use echo $body with pipe .

Answered By – Om Sao

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published