[英]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:我有一个 shell 脚本,其中文件路径$path
有一些我加密如下的文本,它可以工作:
content_sha256="$(openssl dgst -binary -sha256 < $path | openssl enc -e -base64)";
The value of variable content_sha256
works correctly.变量content_sha256
的值工作正常。
Now, I have a string $body
which I want to encrypt.现在,我有一个要加密的字符串$body
。 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?我是不是管道有问题或者openssl
的选项应该不同?
Correct answer below content_sha256="$(echo $body | openssl dgst -binary -sha256 | openssl enc -e -base64)";
正确答案content_sha256="$(echo $body | openssl dgst -binary -sha256 | openssl enc -e -base64)";
Points to note:注意事项:
-binary
option.包括-binary
选项。echo $body
with pipe .不要将文件内容重定向为输入,而是使用echo $body
和 pipe 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.