问题描述
我试图从Python脚本运行openssl命令,但是从脚本中执行的命令和直接在终端中执行的命令获取不同的sha1值.
I am trying to run an openssl command from a Python script, but am getting different sha1 values from the command executed in the script and the command executed directly in the terminal.
这是我的代码:
command = "echo -n '" + hex(key)[2:] + "' | openssl sha1"
print(command)
os.system(command)
output = subprocess.check_output(command, shell=True)
# This converts the bytes object to a string.
output = output.decode("utf-8")
print(output)
os.system(command)
仅用于检查此方法和subprocess.check_output()
是否给出相同的结果.
The os.system(command)
is only there to check whether this method and subprocess.check_output()
give the same result.
这是key=0xabc
的示例输入的程序输出:
Here is the program output for an example input of key=0xabc
:
echo -n 'abc' | openssl sha1
(stdin)= 9d4fe92b8bdc894f5838fad83108bf3841b257fa
(stdin)= 9d4fe92b8bdc894f5838fad83108bf3841b257fa
第一行是要执行的命令,第二行是使用os.system()的命令的结果,第三行是使用subprocess.check_output()的命令的结果.如您所见,这两种方法给出的结果相同.
The first line is the command to be executed, the second is the result of the command using os.system() and the third is the result of the command using subprocess.check_output(). As you can see, both methods give the same result.
现在,如果我复制并粘贴此处显示的命令并在终端中执行该命令,则结果如下:
Now, if I copy and paste the command as it is displayed here and execute it in the terminal, this is the result:
(stdin)= a9993e364706816aba3e25717850c26c9cd0d89d
即使我复制并粘贴了完全相同的命令,但哈希值输出还是不同的.
Even though I copy and pasted the exact same command, the hash value output is different.
即使是陌生人,如果我省略了echo
中的-n
选项,也不会出现不一致的情况:
Even stranger, if I omit the -n
option from echo
, there is no inconsistency:
echo 'abc' | openssl sha1
(stdin)= 03cfd743661f07975fa2f1220c5194cbaff48451
(stdin)= 03cfd743661f07975fa2f1220c5194cbaff48451
在终端:
(stdin)= 03cfd743661f07975fa2f1220c5194cbaff48451
我在网上阅读的有关类似问题的所有信息均来自未使用-n
的原因,而不是看似引起问题的-n
.对于我正在执行的特定任务,我必须使用-n
,因为我正在计算使用echo -n "hash_value" | openssl sha1
计算得出的哈希值的前映像.
Everything I have read online about similar issues stems from not using -n
, rather than -n
seemingly causing the issue. For the particular task I am doing, I have to use -n
as I am computing the pre-image of a hash which was calculated using echo -n "hash_value" | openssl sha1
.
任何帮助将不胜感激:)
Any help would be much appreciated :)
推荐答案
TL; DR :使用/bin/echo
说明:
python系统命令可能使用的是默认外壳程序/bin/sh
,通常将其链接到POSIX兼容外壳程序,而终端会话使用的是用户的登录外壳程序,可能是bash
. POSIX Shell标准不支持echo
内置函数的-n
选项.这意味着您实际上是在计算"-n abc"的摘要.您可以像这样重现bash:
The python system command is likely using a default shell of /bin/sh
which is usually linked to a POSIX compliant shell, while your terminal session is using your user's login shell which is likely bash
. POSIX shell standard does not support the -n
option to the echo
builtin function. This mean you are literally calculating the digest of "-n abc". You can reproduce this is bash like so:
echo "-n abc" | openssl sha1
9d4fe92b8bdc894f5838fad83108bf3841b257fa
二进制可执行文件仍然可用,但是需要使用完整路径来调用,因为内置的shell会覆盖它.代替echo
,使用/bin/echo
.
The binary executable is still available but will need to be called by the full path since the shell builtin will override it. Instead of echo
, use /bin/echo
.
这篇关于openssl sha1在终端与Python中给出不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!