openssl 和 linux bash 计算之间的 MD5 不匹配
MD5 mismatch between openssl and linux bash calculation
我在网上找到这段计算md5的代码:
#include<openssl/evp.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
unsigned char *getMd5Hash(unsigned char *data, size_t dataLen, int *mdLen)
{
unsigned char *md = NULL;
EVP_MD_CTX *ctx = NULL;
const EVP_MD *mdType = EVP_md5();
*mdLen = EVP_MD_size(mdType);
md = (unsigned char *) malloc(*mdLen);
ctx = EVP_MD_CTX_create();
EVP_MD_CTX_init(ctx);
EVP_DigestInit_ex(ctx, mdType, NULL);
EVP_DigestUpdate(ctx, data, dataLen);
EVP_DigestFinal_ex(ctx, md, NULL);
EVP_MD_CTX_cleanup(ctx);
EVP_MD_CTX_destroy(ctx);
return md;
}
int main()
{
char data[1024];
unsigned char *md;
int i = 0, mdLen = 0;
memset(&data, 0, 1024);
std::cout<<"Enter string: ";
std::cin>>data;
md = getMd5Hash((unsigned char *)data, strlen(data), &mdLen);
for(i = 0; i < mdLen; i++) {
printf("%02x",md[i]);
} printf("\n");
return 0;
}
我建造了它。
g++ aaa.cpp -lcrypto
但是这段代码的结果和Linux的md5sum的结果不一样 bash:
$ ./a.out
Enter string: aaa
47bce5c74f589f4867dbd57e9ca9f808
$ echo -e "aaa"|md5sum
5c9597f3c8245907ea71a89d9d39d08e -
这种差异从何而来?
尝试echo -n
。您还在散列行尾标记。
$ echo -e "aaa" | md5sum
5c9597f3c8245907ea71a89d9d39d08e -
$ echo -n "aaa" | md5sum
47bce5c74f589f4867dbd57e9ca9f808 -
我在网上找到这段计算md5的代码:
#include<openssl/evp.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
unsigned char *getMd5Hash(unsigned char *data, size_t dataLen, int *mdLen)
{
unsigned char *md = NULL;
EVP_MD_CTX *ctx = NULL;
const EVP_MD *mdType = EVP_md5();
*mdLen = EVP_MD_size(mdType);
md = (unsigned char *) malloc(*mdLen);
ctx = EVP_MD_CTX_create();
EVP_MD_CTX_init(ctx);
EVP_DigestInit_ex(ctx, mdType, NULL);
EVP_DigestUpdate(ctx, data, dataLen);
EVP_DigestFinal_ex(ctx, md, NULL);
EVP_MD_CTX_cleanup(ctx);
EVP_MD_CTX_destroy(ctx);
return md;
}
int main()
{
char data[1024];
unsigned char *md;
int i = 0, mdLen = 0;
memset(&data, 0, 1024);
std::cout<<"Enter string: ";
std::cin>>data;
md = getMd5Hash((unsigned char *)data, strlen(data), &mdLen);
for(i = 0; i < mdLen; i++) {
printf("%02x",md[i]);
} printf("\n");
return 0;
}
我建造了它。
g++ aaa.cpp -lcrypto
但是这段代码的结果和Linux的md5sum的结果不一样 bash:
$ ./a.out
Enter string: aaa
47bce5c74f589f4867dbd57e9ca9f808
$ echo -e "aaa"|md5sum
5c9597f3c8245907ea71a89d9d39d08e -
这种差异从何而来?
尝试echo -n
。您还在散列行尾标记。
$ echo -e "aaa" | md5sum
5c9597f3c8245907ea71a89d9d39d08e -
$ echo -n "aaa" | md5sum
47bce5c74f589f4867dbd57e9ca9f808 -