gpt4 book ai didi

c - c中int的md5sum数组

转载 作者:太空宇宙 更新时间:2023-11-04 02:25:05 26 4
gpt4 key购买 nike

我想用代码 C 和命令 md5sum file.data 得到相同的总和每次我得到 md5sum 和 to_md5() 的输出不同,如何得到相同的?

我的代码如下:

 char* to_md5(int* buffer,int buffersize){
MD5_CTX ctx;
int fg,n,i;
char* out = (char*)malloc(33);
char digest[MD5_DIGEST_LENGTH];

for(i = 0 ; i <= buffersize ; i=i+10)
{
fg = MD5_Init(&ctx);
fg = MD5_Update(&ctx,buffer+i,i);
fg = MD5_Final(digest,&ctx);
if(fg != 1) {fprintf(stderr,"error in MD5_Update() ");exit(-1);}
printf("digest = %x\n",digest);
}
for (n = 0; n < 16; ++n)
{
snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
}
printf(" char = %s\n",out);
return out ;
}

int main (){
int i,ierr,rc,iter;
char out[256];
int* buffer;
int buffersize;
int nb_iter;
FILE * file;

if(ierr != 0) exit(-1);
buffersize = 5;
nb_iter = 1;

buffersize *= 10;
printf("bufsize: %ld\n",buffersize);
buffer = (int*) calloc(buffersize,sizeof(int));


srand(time(NULL));
for(i = 0; i < buffersize; i ++){
buffer[i] = rand();
}

file = fopen("buffer.dat","w");
fwrite(buffer,sizeof(int),buffersize,file);
fclose(file);

printf(" :md5sum:%s]\n",to_md5(buffer,buffersize));
// the command to compare my checksum of my array
printf("md5sum = ");system("md5sum buffer.dat");
printf("\n");

free(buffer);
return 0 ;
}

each time i get the the md5sum and the output of to_md5() different, how to get the same ?

最佳答案

我想要一个 C 程序 提供与 md5sum 相同的东西 输入(缓冲区):是一个随机整数数组; 输出(out):是一个md5校验和;这是我的代码示例,它确实有效:

  #include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<stdint.h>
#include<string.h>
#include<unistd.h>

#include<openssl/md5.h>


int get_md5sum(void* buffer,
unsigned long buffersize,
char* checksum){

MD5_CTX ctx;
int rc,i;
unsigned char digest[MD5_DIGEST_LENGTH];

rc = MD5_Init(&ctx);
if(rc != 1) {
fprintf(stderr,"error in get_md5sum : MD5_Init\n");
return 1;
}

rc = MD5_Update(&ctx,buffer,sizeof(int)*buffersize);
if(rc != 1) {
fprintf(stderr,"error in get_md5sum : MD5_Update\n");
return 1;
}

rc = MD5_Final(digest,&ctx);
if(rc != 1) {
fprintf(stderr,"error in get_md5sum : MD5_Final\n");
return 1;
}

for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
snprintf(&(checksum[i*2]), 16*2, "%02x", (unsigned int)digest[i]);
}

checksum[2*MD5_DIGEST_LENGTH+1] = '\0';

return 0;
}

int main (){
int i,ierr;
char out[33];
int* buffer;
int buffersize;
int iter,nb_iter;

FILE* file;

buffersize = 5;
nb_iter = 8;

buffersize *= 10;
printf("bufsize: %ld\n",buffersize);

buffer = (int*) calloc(buffersize,sizeof(int));


srand(time(NULL));
for(i = 0; i < buffersize; i++){
buffer[i] = rand();
}

ierr = get_md5sum(buffer,buffersize,out);
if(ierr!=0) exit(-1);
file= fopen("buffer.dat","w");
fwrite((int*)buffer,sizeof(int),buffersize,file);
fclose(file);
printf("\nwith function get_md5sum :\n%s\n",out);
printf("with command\t\t");
system(" md5sum buffer.dat");



free(buffer);


return 0 ;
}

关于c - c中int的md5sum数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52760570/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com