import struct
import base64
import hashlib


def ri_b64hash(path):
    """Get Base64 encoded hash of a router from a RouterInfo file"""
    with open(path, "rb") as f: data = f.read()
    cert_len = struct.unpack("!H", data[385:387])[0]
    public_data = data[:387+cert_len]
    return base64.b64encode(hashlib.sha256(public_data).digest(), 
                            altchars=b"-~").decode()