Я пытаюсь запустить/скомпилировать старый код, созданный в 2018 году, с использованием старой версии OpenSSL.
Я столкнулся со многими ошибками, но я решил их все. Теперь я просто застрял с последней ошибкой в этой строке:
EVP_MD_CTX_cleanup(ctx)
Ошибка здесь:
error: ‘EVP_MD_CTX_cleanup’ was not declared in this scope; did you mean ‘EVP_MD_CTX_create’?
22 | if ( EVP_MD_CTX_cleanup(ctx ) != 1 ) { }
Код для этой части ниже:
#ifndef SHA256_HPP
#define SHA256_HPP
#include <openssl/evp.h>
#include <array>
class sha256
{
public:
inline sha256()
: ctx()
{
if ( EVP_DigestInit_ex(ctx, EVP_sha256(), nullptr ) != 1 ) {
throw "EVP_DigestInit_ex() failed";
}
}
inline ~sha256()
{
if ( EVP_MD_CTX_cleanup(ctx ) != 1 ) { }
}
sha256( sha256 const& ) = delete;
sha256& operator=( sha256 const& ) = delete;
sha256( sha256&& ) = delete;
sha256& operator=( sha256&& ) = delete;
inline std::array<unsigned char, 32> hash( unsigned char const* const data, int const len )
{
if ( EVP_DigestUpdate(ctx, data, len ) != 1 ) {
throw "EVP_DigestUpdate() failed";
}
std::array<unsigned char, 32> hash;
unsigned int hash_size = sizeof( hash );
if ( EVP_DigestFinal_ex(ctx, hash.data(), &hash_size ) != 1 ) {
throw "EVP_DigestFinal_ex() failed";
}
if ( hash_size != sizeof( hash ) ) {
throw "unexpected hash size";
}
return hash;
}
private:
EVP_MD_CTX *ctx;
};
#endif // SHA256_HPP
Я попытался:
if ( EVP_MD_CTX_free(ctx ) != 1 )
if ( EVP_MD_CTX_destroy (ctx ) != 1 )
Но все равно у меня такая же ошибка.
Есть идеи, что мне нужно изменить, чтобы он работал на Openssl 1.1.0 на cygwin64?
OpenSSL 1.1.0 не так уж и стар, ему всего несколько лет. Вы уверены, что это правильная версия, которую вы должны использовать? Скорее всего, вам следует использовать вместо этого 1.0.2, так как EVP_MD_CTX_cleanup()
не существует в 1.1.0.
Одним из основных изменений в версии 1.1.0 было превращение большинства структур OpenSSL в непрозрачные указатели. Код, который вы показали, не должен работать ни в одной версии, поскольку вы передаете нулевой указатель EVP_MD_CTX*
всем функциям EVP_Digest...()
, которые не будут работать.
Код, скорее всего, изначально выглядел примерно так, до того, как вы с ним повозились:
class sha256
{
public:
inline sha256()
{
EVP_MD_CTX_init(&ctx); // <-- init with this
if ( EVP_DigestInit_ex(&ctx, ...) != 1 ) { // <-- use '&' here
...
}
}
inline ~sha256()
{
if ( EVP_MD_CTX_cleanup(&ctx) != 1 ) { } // <-- use '&' here
}
...
inline std::array<unsigned char, 32> hash( unsigned char const* const data, int const len )
{
if ( EVP_DigestUpdate(&ctx, ...) != 1 ) { // <-- use '&' here
...
}
...
if ( EVP_DigestFinal_ex(&ctx, ...) != 1 ) { // <-- use '&' here
...
}
...
}
private:
EVP_MD_CTX ctx; // <-- no '*' here
};
В OpenSSL 1.1.0 это будет выглядеть так:
class sha256
{
public:
inline sha256()
{
ctx = EVP_MD_CTX_new(); // <-- init with this
if ( !ctx ) {
...
}
if ( EVP_DigestInit_ex(ctx, ...) != 1 ) { // <-- no '&' here
...
}
}
inline ~sha256()
{
EVP_MD_CTX_free(ctx); // <-- no '&' here
}
...
inline std::array<unsigned char, 32> hash( unsigned char const* const data, int const len )
{
if ( EVP_DigestUpdate(ctx, ...) != 1 ) { // <-- no '&' here
...
}
...
if ( EVP_DigestFinal_ex(ctx, ...) != 1 ) { // <-- no '&' here
...
}
...
}
private:
EVP_MD_CTX *ctx; // <-- use '*' here
};