将十六进制数据从unsigned char *转换为字符串

时间:2018-04-04 13:41:47

标签: c++ c++11

我有这段代码:

string HMACsha256(string message, string APIkey){

    // The secret key for hashing
    char key[APIkey.length() + 1];
    strcpy(key, APIkey.c_str());

    // The data that we're going to hash
    char data[message.length() + 1];
    strcpy(data, message.c_str());

    unsigned char* result;
    unsigned int len = 64;

    result = (unsigned char*)malloc(sizeof(char) * len);

    HMAC_CTX ctx;
    HMAC_CTX_init(&ctx);

    HMAC_Init_ex(&ctx, key, strlen(key), EVP_sha256(), NULL);
    HMAC_Update(&ctx, (unsigned char*)&data, strlen(data));
    HMAC_Final(&ctx, result, &len);
    HMAC_CTX_cleanup(&ctx);

    printf("HMAC digest: ");

    string signature; // <-------- I want to store the values from the printf with result in the loop below in this

    for (int i = 0; i != len; i++){
        printf("%02x", (unsigned int)result[i]); //<--- I want this in signature string
    }

    cout << endl<<  "SIG: " << signature << endl;

    free(result);

    return signature;
}

关于它的一切工作正常,除了我需要能够将结果作为字符串返回。我一直无法想出一种从unsigned char * result变量获取十六进制数据并将其存储到签名字符串中的方法。有谁知道我怎么能这样做?

编辑:此问题被标记为convert a char* to std::string的副本 但是,我已经在那个做

的主题上试过了例外的答案
const char *s = "Hello, World!";
std::string str(s);

使用我的代码看起来像:

string signature(result);

运行我的代码会产生此错误:

error: no matching constructor for initialization of 'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >')
    string signature(result);

 string signature(reinterpret_cast<char*>(result));

结果是:

 Result:
 20ef96358c199befc0a0e6de47170734532e48e7ddfbf4ea48ef207989342677
 Signature:
 ?5???????G4S.H?????H? y?4&w?Ήf?

引起我的注意,我“不想将数据转换为字符串,而是十六进制表示”,如果这使得这个问题对任何人都更清楚。

1 个答案:

答案 0 :(得分:0)

此代码执行了太多不必要的复制和调用。

更简单的版本:

inline char binary_to_hex_digit(unsigned a) {
    return a + (a < 10 ? '0' : 'a' - 10);
}

std::string binary_to_hex(unsigned char const* binary, unsigned binary_len) {
    std::string r(binary_len * 2, '\0');
    for(unsigned i = 0; i < binary_len; ++i) {
        r[i * 2] = binary_to_hex_digit(binary[i] >> 4);
        r[i * 2 + 1] = binary_to_hex_digit(binary[i] & 15);
    }
    return r;
}

std::string HMACsha256(std::string const& message, std::string const& key) {
    unsigned char result[EVP_MAX_MD_SIZE];
    unsigned result_len = 0;
    HMAC(EVP_sha256(), key.data(), key.size(), reinterpret_cast<unsigned char const*>(message.data()), message.size(), result, &result_len);
    return binary_to_hex(result, result_len);
}

int main() {
    auto mac = HMACsha256("message", "password");
    std::cout << mac << '\n';
}