void process_block(sha1_ctx *ctx) { pad_block(ctx); sha1_core(ctx); if (ctx->overflow) { memmove(ctx->block, ctx->block + SHA_BLOCK_SIZE, SHA_BLOCK_SIZE); ctx->overflow = 0; sha1_core(ctx); } }
int main(int argc, char *argv[]) { if (argc > 1) { int fd; struct stat sb; uint64_t size_to_md5 = 0; fd = open(argv[1], O_RDONLY); if (fd == -1) { perror("open"); exit(-1); } if (fstat(fd, &sb) == -1) { perror("fstat"); exit(-1); } size_t file_size = sb.st_size; void *addr = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0); if (addr == MAP_FAILED) { perror("mmap"); exit(-1); } uint8_t *buf = (uint8_t *)malloc(file_size + (MD5_FILE_PADDING)); memcpy(buf, addr, file_size); munmap(addr, file_size); struct md5_context ctx; init_md5_ctx(&ctx, buf, file_size); //pad_md5(buf, file_size); uint64_t num_chunks = ctx.size / MD5_BLOCK_SIZE; if (((ctx.size % MD5_BLOCK_SIZE) == 0) && ctx.size != 0) num_chunks++; // go until the last block which may need to be padded for (;num_chunks > 1 ; num_chunks--) { md5_hash_block(&ctx); ctx.curptr += MD5_BLOCK_SIZE; } int need_another_block = pad_block(&ctx); md5_hash_block(&ctx); if (need_another_block) { ctx.curptr += MD5_BLOCK_SIZE; md5_hash_block(&ctx); } display_md5hash(&ctx); printf("\t%s\n", argv[1]); free(buf); close(fd); } return 0; }