2010-07-03 77 views
0

問題,所以我有一些代碼:與Python hashlib.sha256(2.4.3)

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

,完美運行我自己的電腦上(有蟒蛇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上的一個常見/已知問題或者其他事情正在發生。

感謝

回答

0

hashlib是2.5新。對於舊版本的Python,您將需要backport

+0

伊格納西奧,謝謝!這解釋了一切:) – 2010-07-03 19:14:17

+0

其實這並不能解釋一切。所提到的backport與hmac模塊不兼容,python 2.5+的方式是...它錯過了sha256.new方法,這意味着它不能以digestmod的形式傳遞給hmac .... – 2011-12-31 06:21:12

0

這是一個黑客,使hashlib反向移植工作,HMAC Python的2.4:

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()