crypto-algorithms icon indicating copy to clipboard operation
crypto-algorithms copied to clipboard

file md5 error

Open kmss-w opened this issue 5 years ago • 0 comments

int md5_file(char * name, char * out) { if (name == NULL || out == NULL) { return -1; }

MD5_CTX ctx; md5_init(&ctx);

struct stat st;

if(-1 == stat(name, &st)) { md5_update(&ctx, name, strlen(name)); } else { FILE * fp = fopen(name, "r+");

if (!fp) {
  fprintf(stderr, "md5_file: open file(%s) error!\n", name);
  fclose(fp);
  return -1;
}

char buff[1024];
memset(buff, 0, sizeof(buff));
size_t len = 0;

while (len = fread(buff, 1, sizeof(buff), fp)) {
  md5_update(&ctx, &buff, len);
}

fclose(fp);

}

BYTE digest[MD5_BLOCK_SIZE] = {0}; md5_final(&ctx, digest);

for(int idx = 0; idx < MD5_BLOCK_SIZE; idx += 1) { sprintf(out + (idx * 2), "%02x", digest[idx]); }

return 0; }

kmss-w avatar Sep 29 '19 09:09 kmss-w