3

我正在尝试编译一些将 OpenSSL 与 emscripten 一起使用的 C 代码,但我收到未解析的符号警告,例如:

warning: unresolved symbol: SHA256_Init
warning: unresolved symbol: SHA256_Final
warning: unresolved symbol: SHA256_Update

我使用以下命令编译了代码:

emcc SHA256.c -lssl -lcrypto -L /usr/local/openssl-1.0.2k/lib/ -I /usr/local/openssl-1.0.2k/include -s WASM=1 -o SHA256.html --emrun

使用以下源代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>


void sha256(char *string, char outputBuffer[65])
{
    unsigned char hash[SHA256_DIGEST_LENGTH];
    SHA256_CTX sha256;
    SHA256_Init(&sha256);
    SHA256_Update(&sha256, string, strlen(string));
    SHA256_Final(hash, &sha256);
    int i = 0;
    for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
    {
        sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
    }
    outputBuffer[64] = 0;
}


int main (void)
{
    static unsigned char buffer[65];
    sha256("string", buffer);
    printf("%s\n", buffer);

    return 0;
}
4

1 回答 1

1

使用 emscripten 编译两个库:crypto 和 openssl,而不是使用系统版本。

使用为这些库生成的 .bc 文件作为 em++ 命令的链接器参数,它应该可以工作。

于 2017-09-02T10:48:16.193 回答