簡體   English   中英

Xcode C ++ MD5哈希

[英]Xcode C++ MD5 hash

我想在Xcode c ++中使用MD5哈希一個簡單的字符串。 我搜索了很多,但是找不到教程。 我必須#import <CommonCrypto/CommonDigest.h> 這就是全部? 之后如何呼叫MD5? 我找到了此代碼,但它給了我一個錯誤。 如何在字符串變量中更新哈希值?

 unsigned char digest[16];
 const char* string = "Hello World";
 struct MD5Context context;     **(error: variable has incomplete type
 MD5Init(&context);
 MD5Update(&context, string, strlen(string));
 MD5Final(digest, &context);

我只是使用一個簡單的命令行應用程序,而在基本main.cpp中沒有標題。 我真的很感謝任何幫助!!!

有一個一次性版本:

#include <CommonCrypto/CommonDigest.h>

unsigned char digest[16];
const char* string = "Hello World";

CC_MD5(string, (CC_LONG)strlen(string), digest);

您將需要包含Security.framework(或租借適用的庫文件)。

您使用了錯誤的API。 我不確定從哪里獲得這些信息(它們看起來像OpenSSL調用),但是應該看起來像這樣:

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

#include <CommonCrypto/CommonDigest.h>

int main()
{
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    const char string[] = "Hello World";

    CC_MD5_CTX context;
    CC_MD5_Init(&context);
    CC_MD5_Update(&context, string, (CC_LONG)strlen(string));
    CC_MD5_Final(digest, &context);

    for (size_t i=0; i<CC_MD5_DIGEST_LENGTH; ++i)
        printf("%.2x", digest[i]);
    fputc('\n', stdout);
    return 0;
}

輸出量

b10a8db164e0754105b7a99be72e3fe5

在這里驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM