找不到python crypto.sign,虽然它在模块中

时间:2014-08-11 23:37:46

标签: python pyopenssl

我试图使用一些谷歌api示例代码,但它无法正常工作。不可否认,我在python上是绿色的,但我已经将它归结为这个简单的测试程序:

#!/usr/bin/python

from OpenSSL import crypto

print crypto.sign('key', 'xyzzy', 'sha256')

导致:

Traceback (most recent call last):
  File "./ot", line 5, in <module>
    print crypto.sign('key', 'xyzzy', 'sha256')
AttributeError: 'module' object has no attribute 'sign'

当我查看openssl加密模块(/usr/lib/python2.6/site-packages/OpenSSL/crypto.py)时,它实际上确实有&#34;签署&#34;定义:

def sign(self, pkey, digest):
    """
    Sign the certificate request using the supplied key and digest

所以我非常困惑。就像我所知,没有其他版本存在

pip show pyopenssl
---
Name: pyOpenSSL
Version: 0.14
Location: /usr/lib/python2.6/site-packages
Requires: cryptography, six

根据评论扩大输出:

openssl file:
/usr/lib64/python2.6/site-packages/OpenSSL/__init__.pyc

dir(crypto):
['Error', 'FILETYPE_ASN1', 'FILETYPE_PEM', 'FILETYPE_TEXT', 'NetscapeSPKI', 'NetscapeSPKIType', 'PKCS12', 'PKCS12Type', 'PKCS7Type', 'PKey', 'PKeyType', 'TYPE_DSA', 'TYPE_RSA', 'X509', 'X509Extension', 'X509ExtensionType', 'X509Name', 'X509NameType', 'X509Req', 'X509ReqType', 'X509StoreType', 'X509Type', 'X509_verify_cert_error_string', '_C_API', '__doc__', '__file__', '__name__', '__package__', '_exception_from_error_queue', 'dump_certificate', 'dump_certificate_request', 'dump_privatekey', 'load_certificate', 'load_certificate_request', 'load_pkcs12', 'load_pkcs7_data', 'load_privatekey']

crypto file
/usr/lib64/python2.6/site-packages/OpenSSL/crypto.so

crypto.sign:
Traceback (most recent call last):
  File "./ot", line 16, in <module>
print crypto.sign('key', 'xyzzy', 'sha256')
AttributeError: 'module' object has no attribute 'sign'

2 个答案:

答案 0 :(得分:2)

我显然有一些相互矛盾的安装 - 我删除了几个yum python-crypto软件包,然后pip卸载了openssl,还有/usr/lib64/python2.6/site-packages/OpenSSL/中的东西(包括crypto。所以,所以我手动删除该目录,然后pip安装pyopenssl,这解决了问题。感谢指点...

答案 1 :(得分:1)

对于没有使用openssl的人有这个问题(缺少签名方法),还有另一个解决方案。

检查代码顶部的导入部分,您需要进行此导入:

from Crypto.Signature import PKCS1_v1_5

而不是Crypto.Cipher实现:

from Crypto.Cipher import PKCS1_v1_5

此类没有sign方法: https://www.dlitz.net/software/pycrypto/api/2.6/Crypto.Cipher.PKCS1_v1_5.PKCS115_Cipher-class.html

Crypto.Signature PKCS1_v1_5类具有符号方法:https://www.dlitz.net/software/pycrypto/api/2.6/Crypto.Signature.PKCS1_v1_5.PKCS115_SigScheme-class.html