use ed25519_dalek::PublicKey;
use sha3::Digest;

pub fn public_key_to_hostname(public_key: &PublicKey) -> String {
    let mut buf = [0u8; 35];
    public_key.to_bytes().iter().copied().enumerate().for_each(|(i, b)| {
        buf[i] = b;
    });

    let mut h = sha3::Sha3_256::new();
    h.update(b".onion checksum");
    h.update(public_key);
    h.update(b"\x03");
    let res_vec = h.finalize().to_vec();
    buf[32] = res_vec[0];
    buf[33] = res_vec[1];
    buf[34] = 0x03;
    base32::encode(base32::Alphabet::RFC4648 { padding: false }, &buf).to_ascii_lowercase()
}
