我有一个shell脚本,其中文件路径$path
有一些文本,我加密如下,它的工作:
content_sha256="$(openssl dgst -binary -sha256 < $path | openssl enc -e -base64)";
变量content_sha256
的取值正常
现在,我有一个字符串$body
,我想加密。我在下面试了一下,但结果完全不同。
content_sha256="$(echo $body | openssl dgst -sha256 | openssl enc -e -base64)";
我管道错误或选项openssl
应该是不同的?
正确答案如下content_sha256="$(echo $body | openssl dgst -binary -sha256 | openssl enc -e -base64)";
注意事项:
- 包含
-binary
选项 - 使用
echo $body
代替重定向文件内容作为输入。