Skip to content

Instantly share code, notes, and snippets.

@davidlatwe
Created August 11, 2018 12:02
Show Gist options
  • Save davidlatwe/0767478902dca1e94660b039f87b7ad7 to your computer and use it in GitHub Desktop.
Save davidlatwe/0767478902dca1e94660b039f87b7ad7 to your computer and use it in GitHub Desktop.
"""Avalanche-io C4 Framework - AssetID
Simplified from:
https://github.com/Avalanche-io/pyc4
"""
import hashlib
def _b58encode(bytes):
"""
Base58 Encode bytes to string
"""
b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
b58base = 58
long_value = int(bytes.encode("hex_codec"), 16)
result = ''
while long_value >= b58base:
div, mod = divmod(long_value, b58base)
result = b58chars[mod] + result
long_value = div
result = b58chars[long_value] + result
return result
def c4_hash(file_path):
hash_obj = hashlib.sha512()
with open(file_path, "rb") as file:
for chunk in iter(lambda: file.read(40960), b""):
hash_obj.update(chunk)
c4_id_length = 90
b58_hash = _b58encode(hash_obj.digest())
padding = ""
if len(b58_hash) < (c4_id_length - 2):
padding = "1" * (c4_id_length - 2 - len(b58_hash))
c4id = "c4" + padding + b58_hash
return c4id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment