Search code examples
linuxshellopensslpipe

Linux command piping in openssl to use string input


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 .