+ return digest;
+}
+
+
+/** Extract a public key from a private key and create a SHA1 digest of it.
+ * @param private_key_file Private key filename
+ * @param openssl openssl binary name (or full path if openssl is not on the system path).
+ * @return SHA1 digest of corresponding public key, with escaped / characters.
+ */
+string
+dcp::public_key_digest(boost::filesystem::path private_key_file)
+{
+ auto private_key_string = dcp::file_to_string(private_key_file);
+
+ /* Read private key into memory */
+ auto private_key_bio = BIO_new_mem_buf(const_cast<char*>(private_key_string.c_str()), -1);
+ if (!private_key_bio) {
+ throw MiscError("Could not create memory BIO");
+ }
+ dcp::ScopeGuard sg_private_key_bio([private_key_bio]() { BIO_free(private_key_bio); });
+
+ /* Extract private key */
+ auto private_key = PEM_read_bio_PrivateKey(private_key_bio, nullptr, nullptr, nullptr);
+ if (!private_key) {
+ throw MiscError("Could not read private key");
+ }
+ dcp::ScopeGuard sg_private_key([private_key]() { EVP_PKEY_free(private_key); });
+
+ /* Get public key from private key */
+ auto public_key = EVP_PKEY_get1_RSA(private_key);
+ if (!public_key) {
+ throw MiscError("Could not obtain public key");
+ }
+ dcp::ScopeGuard sg_public_key([public_key]() { RSA_free(public_key); });
+
+ return public_key_digest(public_key);