1

我正在尝试使用一些 google 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) 时,它确实定义了“符号”:

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'
4

2 回答 2

3

对于有此问题(缺少符号方法)的不使用 openssl 的人,还有另一种解决方案。

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

from Crypto.Signature import PKCS1_v1_5

而不是 Crypto.Cipher 实现:

from Crypto.Cipher import PKCS1_v1_5

此类没有签名方法: 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

于 2017-07-19T15:56:58.990 回答
2

我显然有一些冲突的安装 - 我删除了几个 yum python-crypto 包,然后 pip 卸载了 openssl 并且 /usr/lib64/python2.6/site-packages/OpenSSL/ (包括 crypto.so)中仍然有东西,所以我手动删除了该目录,然后 pip 安装了 pyopenssl 并解决了问题。谢谢指点...

于 2014-08-12T17:15:26.083 回答