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 中使用一个简单的命令行应用程序 headers。
非常感谢任何帮助!!!!
有一个one-shot版本:
#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
已验证 here。
我想在 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 中使用一个简单的命令行应用程序 headers。 非常感谢任何帮助!!!!
有一个one-shot版本:
#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
已验证 here。