简体   繁体   English

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

[英]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:注意事项:

  1. Include -binary option.包括-binary选项。
  2. Instead of redirection of file content as input, use echo $body with pipe .不要将文件内容重定向为输入,而是使用echo $body和 pipe 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM