00001 #include <stdio.h> 00002 #include <stdlib.h> 00003 #include <sys/types.h> 00004 #include <sys/stat.h> 00005 #include <fcntl.h> 00006 #include <unistd.h> 00007 #include <openssl/md5.h> 00008 00009 int main(int argc, char *argv[]) 00010 { 00011 int bs, nr, nw, n; 00012 unsigned char *buf; 00013 unsigned char md5[16]; 00014 MD5_CTX c; 00015 FILE *fp; 00016 00017 if (argc != 3) { 00018 fprintf(stderr, "Usage: %s block-size checksum_file\n", argv[0]); 00019 exit(1); 00020 } 00021 00022 bs = atoi(argv[1]); 00023 if (bs < 0) { 00024 fprintf(stderr, "%s: illegal block size %d\n", argv[0], bs); 00025 exit(1); 00026 } 00027 bs *= 512; 00028 buf = (unsigned char *) malloc(bs); 00029 if (! buf) { 00030 fprintf(stderr, "%s: out of memory\n", argv[0]); 00031 exit(1); 00032 } 00033 00034 fp = fopen(argv[2], "w"); 00035 if (!fp) { 00036 fprintf(stderr, "%s: can't checksum file %s\n", argv[0], argv[2]); 00037 exit(1); 00038 } 00039 00040 MD5_Init(&c); 00041 while (nr = read(0, buf, bs)) { 00042 if (nr < 0) { 00043 fprintf(stderr, "%s: read failed\n", argv[0]); 00044 exit(1); 00045 } 00046 nw = 0; 00047 do { 00048 n = write(1, &buf[nw], nr - nw); 00049 if (n < 0) { 00050 fprintf(stderr, "%s: write failed\n", argv[0]); 00051 exit(1); 00052 } 00053 nw += n; 00054 } while (nw < nr); 00055 MD5_Update(&c, buf, n); 00056 } 00057 MD5_Final(md5, &c); 00058 00059 for (n=0; n<16; fprintf(fp, "%02x", md5[n++])) 00060 ; 00061 fprintf(fp, "\n"); 00062 00063 return 0; 00064 }