diff --git a/c/opendht.cpp b/c/opendht.cpp
index ed5412370e5d5424c4ee5ff9af57c9d484105455..55e202d30beb7b93136926af45df00bd28712909 100644
--- a/c/opendht.cpp
+++ b/c/opendht.cpp
@@ -124,7 +124,7 @@ void dht_publickey_delete(dht_publickey* pk) {
 
 int dht_publickey_export(const dht_publickey* pk, char* out, size_t* outlen) {
     const auto& pkey = *reinterpret_cast<const PubkeySp*>(pk);
-    return gnutls_pubkey_export(pkey->pk, GNUTLS_X509_FMT_DER, out, outlen);
+    return pkey->pack(out, outlen);
 }
 
 dht_infohash dht_publickey_get_id(const dht_publickey* pk) {
diff --git a/include/opendht/crypto.h b/include/opendht/crypto.h
index 82e53a3fbf7abf6d0991d3e0225d3cb9f8425dfd..2c0ab8f41716f93cb3aba9a4bd3875f6b6545ecb 100644
--- a/include/opendht/crypto.h
+++ b/include/opendht/crypto.h
@@ -114,6 +114,7 @@ struct OPENDHT_PUBLIC PublicKey
     }
 
     void pack(Blob& b) const;
+    int pack(uint8_t* out, size_t* out_len) const;
     void unpack(const uint8_t* dat, size_t dat_size);
 
     std::string toString() const;
diff --git a/src/crypto.cpp b/src/crypto.cpp
index ba69b8e6030d2f101914a61bde73430bf7909f2c..06c80de617a50d005fa15fb166c3e22ce9066ea5 100644
--- a/src/crypto.cpp
+++ b/src/crypto.cpp
@@ -412,12 +412,18 @@ PublicKey::pack(Blob& b) const
         throw CryptoException(std::string("Could not export public key: null key"));
     std::vector<uint8_t> tmp(2048);
     size_t sz = tmp.size();
-    if (int err = gnutls_pubkey_export(pk, GNUTLS_X509_FMT_DER, tmp.data(), &sz))
+    if (int err = pack(tmp.data(), &sz))
         throw CryptoException(std::string("Could not export public key: ") + gnutls_strerror(err));
     tmp.resize(sz);
     b.insert(b.end(), tmp.begin(), tmp.end());
 }
 
+int
+PublicKey::pack(uint8_t* out, size_t* out_len) const
+{
+    return gnutls_pubkey_export(pk, GNUTLS_X509_FMT_DER, out, out_len);
+}
+
 void
 PublicKey::unpack(const uint8_t* data, size_t data_size)
 {