开发者

Issues with Python hashlib.sha256 (2.4.3)

开发者 https://www.devze.com 2023-01-06 09:50 出处:网络
So I have some code: signature = hmac.new( key=AWS_SECRET_ACCESS_KEY, msg=string_to_sign, digestmod=hashlib.sha256).digest()

So I have some code:

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

That runs perfectly on my own computer (has python 2.6.1). However, when I run this code on my server (Python 2.4.3) I get the following:

 /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
开发者_C百科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'",) 

I know the obvious response is to just update Python on my server, but my host has to do that and I don;t know how long it'll take. I'm just curious if this is a common/known issue on 2.4.3 or if something else is going on.

Thanks


hashlib is new in 2.5. You'll need the backport for older versions of Python.


This is a hack to make hashlib backport work with hmac on python 2.4:

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

and use hmac like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消