ripemd160(sha256(publickey)) - Where am I going wrong? (Note: I'm using http://www.royalforkblog.com/2014/08/11/graphical-address-generator/ (use "bitcoin" as the phrase for generating the private key)

I'm trying to take the first step in going from a publickey to an address.

For example, if this is my public key:

publickey = 02218AD6CDC632E7AE7D04472374311CEBBBBF0AB540D2D08C3400BB844C654231


This should be the result of the next step:

ripemd160(sha256(publickey)) = 5238C71458E464D9FF90299ABCA4A1D7B9CB76AB


However, when I try to do this in python, I fail to get the same result:

&gt;&gt;&gt; hashlib.new('sha256', '02218AD6CDC632E7AE7D04472374311CEBBBBF0AB540D2D08C3400BB844C654231').hexdigest()
'6e28eefe40e5855ed8cdf0599ec12e52f73a5b222c84ba957b4dcdd2d8a9cf2a'

&gt;&gt;&gt; hashlib.new('ripemd160', '6e28eefe40e5855ed8cdf0599ec12e52f73a5b222c84ba957b4dcdd2d8a9cf2a').hexdigest()
'e607ff4fa71ef33ca357415e3d2f781c4cedacc3'


Where am I going wrong when using sha256 and ripemd160 in my python code?
moshaholo has the right answer, but not quite the right code.

&gt;&gt;&gt; publickey = '02218AD6CDC632E7AE7D04472374311CEBBBBF0AB540D2D08C3400BB844C654231'.decode('hex')
&gt;&gt;&gt; s = hashlib.new('sha256',    publickey).digest()
&gt;&gt;&gt; r = hashlib.new('ripemd160', s        ).digest()
&gt;&gt;&gt; s.encode('hex')
'ea571f53cb3a9865d3dc74735e0c16643d319c6ad81e199b9c8408cecbcec7bb'
&gt;&gt;&gt; r.encode('hex')
'5238c71458e464d9ff90299abca4a1d7b9cb76ab'


Notice that publickey, s, and r are all binary byte strings (because .digest() is being used instead of .hexdigest()). The only reason to convert them to hex-encoded strings is for display purposes. You can display them directly if you like, but it's not pretty....

&gt;&gt;&gt; publickey
'\x02!\x8a\xd6\xcd\xc62\xe7\xae}\x04G#t1\x1c\xeb\xbb\xbf\n\xb5@\xd2\xd0\x8c4\x00\xbb\x84LeB1'