Hash (sha512) of public_key in PHP and Python

I had a public_key generated by open ssl that was used to create a hash for encryption in PHP. But I had a hard time converting the equivalent in Python because that’s where I wanted to go. But sooner rather than later, I figured that out. Covered both examples here:

PHP Code:

$public_key = '-----BEGIN PUBLIC KEY-----
abcdMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0wSUI+wSKOqvpm79kNslxOw3D
OLi76AEqsB5w/mQETdpOU0hTGHKNBBIO3ZVzTks3FUs+NCfHuG3bUYW3ss8OjV8f
klmeq9Xt9HgMHH51nBvBQnNJ14q7KB1onH5oBnLpIA3Yo0Xc9F0CHTiRYZzObhij
PdTycTAn5RYg3gzWQwIDAQAB
-----END PUBLIC KEY-----'
$hash = hash("sha512", $public_key);
echo "$hash\n";

Python Code: The key here is to have \n\ at each terminating line in the key. \n denotes end of line character and \ denotes concatenation of string.

import hashlib
public_key = '-----BEGIN PUBLIC KEY-----\n\
abcdMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0wSUI+wSKOqvpm79kNslxOw3D\n\
OLi76AEqsB5w/mQETdpOU0hTGHKNBBIO3ZVzTks3FUs+NCfHuG3bUYW3ss8OjV8f\n\
klmeq9Xt9HgMHH51nBvBQnNJ14q7KB1onH5oBnLpIA3Yo0Xc9F0CHTiRYZzObhij\n\
PdTycTAn5RYg3gzWQwIDAQAB\n\
-----END PUBLIC KEY-----'
m = hashlib.sha512()
m.update(public_key)
print m.hexdigest()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s