openssl中的Linux命令管道使用字符串输入



我有一个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)";

注意事项:

  1. 包含-binary选项
  2. 使用echo $body代替重定向文件内容作为输入。

最新更新