InfoHash PublicKey::getId() const { InfoHash id; size_t sz = id.size(); if (gnutls_pubkey_get_key_id(pk, 0, id.data(), &sz) != GNUTLS_E_SUCCESS || sz != id.size()) return {}; return id; }
InfoHash Certificate::getId() const { if (not cert) return {}; InfoHash id; size_t sz = id.size(); if (gnutls_x509_crt_get_key_id(cert, 0, id.data(), &sz) != GNUTLS_E_SUCCESS || sz != id.size()) throw CryptoException("Can't get certificate public key ID."); return id; }
InfoHash InfoHash::get(const uint8_t* data, size_t data_len) { InfoHash h; size_t s = h.size(); const gnutls_datum_t gnudata = {(uint8_t*)data, (unsigned)data_len}; const gnutls_digest_algorithm_t algo = (HASH_LEN == 64) ? GNUTLS_DIG_SHA512 : ( (HASH_LEN == 32) ? GNUTLS_DIG_SHA256 : ( (HASH_LEN == 20) ? GNUTLS_DIG_SHA1 : GNUTLS_DIG_NULL )); static_assert(algo != GNUTLS_DIG_NULL, "Can't find hash function to use."); int rc = gnutls_fingerprint(algo, &gnudata, h.data(), &s); if (rc == 0 && s == HASH_LEN) return h; throw std::string("Error while hashing"); }