Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file md5 error #25

Open
kmss-w opened this issue Sep 29, 2019 · 0 comments
Open

file md5 error #25

kmss-w opened this issue Sep 29, 2019 · 0 comments

Comments

@kmss-w
Copy link

kmss-w commented Sep 29, 2019

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant