【问题标题】:Issues with Python hashlib.sha256 (2.4.3)Python hashlib.sha256 (2.4.3) 的问题
【发布时间】:2012-01-01 07:54:11
【问题描述】:

所以我有一些代码:

signature = hmac.new(
    key=AWS_SECRET_ACCESS_KEY,
    msg=string_to_sign,
    digestmod=hashlib.sha256).digest()

这在我自己的计算机上完美运行(具有 python 2.6.1)。但是,当我在我的服务器(Python 2.4.3)上运行此代码时,我得到以下信息:

 /home/MYUSERNAME/public_html/Foo.com/cgi-bin/foo.py
   66     key=AWS_SECRET_ACCESS_KEY,
   67     msg=string_to_sign,
   68     digestmod=hashlib.sha1).digest()
   69  
   70 # Base64 encode the signature
digestmod = <built-in function openssl_sha256>, hashlib = <module 'hashlib' from '/usr/lib/python2.4/site-...shlib-20081119-py2.4-linux-i686.egg/hashlib.pyc'>, hashlib.sha1 = <built-in function openssl_sha1>, ).digest undefined
 /usr/lib/python2.4/hmac.py in new(key='xR6MsC/+Vc2xkd0YYbER0meR/IkWEU', msg='GET\necs.amazonaws.com\n/onca/xml\nAWSAccessKeyId=A...CommerceService&Timestamp=2010-07-03T18%3A56%3A48', digestmod=<built-in function openssl_sha1>)
  103     You can now feed arbitrary strings into the object using its update()
  104     method, and can ask for the hash value at any time by calling its digest()
  105     method.
  106     """
  107     return HMAC(key, msg, digestmod)
global HMAC = <class hmac.HMAC>, key = 'xR6MsC/+Vc2xkd0YYbER0meR/IkWEU', msg = 'GET\necs.amazonaws.com\n/onca/xml\nAWSAccessKeyId=A...CommerceService&Timestamp=2010-07-03T18%3A56%3A48', digestmod = <built-in function openssl_sha1>
 /usr/lib/python2.4/hmac.py in __init__(self=<hmac.HMAC instance>, key='xR6MsC/+Vc2xkd0YYbER0meR/IkWEU', msg='GET\necs.amazonaws.com\n/onca/xml\nAWSAccessKeyId=A...CommerceService&Timestamp=2010-07-03T18%3A56%3A48', digestmod=<built-in function openssl_sha1>)
   40 
   41         self.digestmod = digestmod
   42         self.outer = digestmod.new()
   43         self.inner = digestmod.new()
   44         self.digest_size = digestmod.digest_size
self = <hmac.HMAC instance>, self.outer undefined, digestmod = <built-in function openssl_sha1>, digestmod.new undefined

AttributeError: 'builtin_function_or_method' object has no attribute 'new'
      args = ("'builtin_function_or_method' object has no attribute 'new'",) 

我知道显而易见的响应是在我的服务器上更新 Python,但我的主机必须这样做,我不知道需要多长时间。我只是好奇这是 2.4.3 上的常见/已知问题还是发生了其他事情。

谢谢

【问题讨论】:

    标签: python sha256 hashlib


    【解决方案1】:

    hashlib 是 2.5 中的新功能。对于旧版本的 Python,您需要 backport

    【讨论】:

    • 其实这并不能说明一切。提到的后端与 hmac 模块不兼容,就像 python 2.5+ 一样......它错过了 sha256.new 方法,这意味着它不能以 digestmod 的形式传递给 hmac ....
    【解决方案2】:

    这是使 hashlib 反向移植在 python 2.4 上与 hmac 一起工作的 hack:

    class mysha256:
        digest_size = 32
        def new(self, inp=''):
            return hashlib.sha256(inp)
    

    并像这样使用 hmac:

    signature = hmac.new(
        key=AWS_SECRET_ACCESS_KEY,
        msg=string_to_sign,
        digestmod=mysha256()).digest()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 2013-08-07
      相关资源
      最近更新 更多