md5.hexdigest() converting unicode string to ascii

uebertester mgibson at tripwire.com
Thu Apr 15 18:50:00 EDT 2004


I'm trying to get a MD5 hash of a registry key value on Windows 2000
Server.  The registry value is a string which is stored as a unicode
string.  I can get the registry key value and pass it to md5 via
update(), however, hexdigest() returns a hash value for the ascii
equivalent of the unicode string.  Not the hash of the unicode string
itself.  I've verified this by creating a text file containing an
ascii form of the string and one containing a unicode form of the
string.  The hash returned by md5.hexdigest() matches that when I run
md5sum on the ascii file.

Here is the code I'm using:

import _winreg
from md5 import md5

x=_winreg.ConnectRegistry(None,_winreg.HKEY_LOCAL_MACHINE)
y= _winreg.OpenKey(x,
r"SOFTWARE\Microsoft\Windows\CurrentVersion\URL\DefaultPrefix")

sValue = _winreg.QueryValueEx(y,"")
print sValue

m = md5()
m.update(unicode(sValue[0]))
MD5 = m.hexdigest()

print "%s \n%d" % (sValue[0], sValue[1])
print MD5

_winreg.CloseKey(y)
_winreg.CloseKey(x)

Any help would be appreciated.



More information about the Python-list mailing list