Skip to content

Instantly share code, notes, and snippets.

@MatrixManAtYrService
Created August 3, 2017 03:13
Show Gist options
  • Save MatrixManAtYrService/fa384d0d524c68d6c903e2be281eb672 to your computer and use it in GitHub Desktop.
Save MatrixManAtYrService/fa384d0d524c68d6c903e2be281eb672 to your computer and use it in GitHub Desktop.
a hash curiosity
from hashlib import sha256
import multihash
from multihash import SHA2_256
from base58 import b58encode
import subprocess
class IPFS_interface(unittest.TestCase):
def test_hash_expectations(self):
# add 'hello world' to ipfs, get resulting hash
ps = subprocess.Popen(['ipfs', 'add'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output = ps.communicate(input=b'hello world')[0]
ipfs_hash = output.decode('ascii').split()[1]
# retrieve ipfs object for 'hello world'
proc = subprocess.run(['ipfs', 'object', 'get', ipfs_hash, '--encoding', 'protobuf'], stdout=subprocess.PIPE)
# hash it
hasher = sha256()
hasher.update(proc.stdout)
my_hash = b58encode(bytes(multihash.encode(hasher.digest(), SHA2_256)))
# they should be equal
self.assertEqual(ipfs_hash, my_hash, "I assume that the ipfs hash is a base58 encoded SHA2_256 multihash of the protobuf for the ipfs object (i.e. data plus links")
#AssertionError: 'Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD' != 'QmcxynZDK7xyC5BrUNjGFyf62w99oVoxf62CSLJfDvt3NC'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment