(fname)
Loads an onion-service private-key from the given file. This can
    be either a 'key blog' as returned from a previous ADD_ONION call,
    or a v3 or v2 file as created by Tor when using the
    HiddenServiceDir directive.

    In any case, a key-blob suitable for ADD_ONION use is returned.
Loads an onion-service private-key from the given file. This can
    be either a 'key blog' as returned from a previous ADD_ONION call,
    or a v3 or v2 file as created by Tor when using the
    HiddenServiceDir directive.
Loads an onion - service private - key from the given file . This can be either a key blog as returned from a previous ADD_ONION call or a v3 or v2 file as created by Tor when using the HiddenServiceDir directive .
def _load_private_key_file(fname):
    """
    Loads an onion-service private-key from the given file. This can
    be either a 'key blog' as returned from a previous ADD_ONION call,
    or a v3 or v2 file as created by Tor when using the
    HiddenServiceDir directive.

    In any case, a key-blob suitable for ADD_ONION use is returned.
    """
    with open(fname, "rb") as f:
        data = f.read()
    if b"\x00\x00\x00" in data:  # v3 private key file
        blob = data[data.find(b"\x00\x00\x00") + 3:]
        return u"ED25519-V3:{}".format(b2a_base64(blob.strip()).decode('ascii').strip())
    if b"-----BEGIN RSA PRIVATE KEY-----" in data:  # v2 RSA key
        blob = "".join(data.decode('ascii').split('\n')[1:-2])
        return u"RSA1024:{}".format(blob)
    blob = data.decode('ascii').strip()
    if ':' in blob:
        kind, key = blob.split(':', 1)
        if kind in ['ED25519-V3', 'RSA1024']:
            return blob
    raise ValueError(
        "'{}' does not appear to contain v2 or v3 private key data".format(
            fname,
        )
    )
def _load_private_key_file ( fname ) : with open ( fname , "rb" ) as f : data = f . read ( ) if b"\x00\x00\x00" in data : # v3 private key file blob = data [ data . find ( b"\x00\x00\x00" ) + 3 : ] return u"ED25519-V3:{}" . format ( b2a_base64 ( blob . strip ( ) ) . decode ( 'ascii' ) . strip ( ) ) if b"-----BEGIN RSA PRIVATE KEY-----" in data : # v2 RSA key blob = "" . join ( data . decode ( 'ascii' ) . split ( '\n' ) [ 1 : - 2 ] ) return u"RSA1024:{}" . format ( blob ) blob = data . decode ( 'ascii' ) . strip ( ) if ':' in blob : kind , key = blob . split ( ':' , 1 ) if kind in [ 'ED25519-V3' , 'RSA1024' ] : return blob raise ValueError ( "'{}' does not appear to contain v2 or v3 private key data" . format ( fname , ) )