0
|
1 #include <stdio.h>
|
|
2 #include <zlib.h>
|
|
3 #include "md5.h"
|
|
4 #include "kseq.h"
|
|
5
|
|
6 #define HEX_STR "0123456789abcdef"
|
|
7
|
|
8 KSEQ_INIT(gzFile, gzread)
|
|
9
|
|
10 static void md5_one(const char *fn)
|
|
11 {
|
|
12 MD5_CTX md5_one, md5_all;
|
|
13 int l, i, k;
|
|
14 gzFile fp;
|
|
15 kseq_t *seq;
|
|
16 unsigned char unordered[16], digest[16];
|
|
17
|
|
18 for (l = 0; l < 16; ++l) unordered[l] = 0;
|
|
19 fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
|
|
20 if (fp == 0) {
|
|
21 fprintf(stderr, "md5fa: %s: No such file or directory\n", fn);
|
|
22 exit(1);
|
|
23 }
|
|
24
|
|
25 MD5Init(&md5_all);
|
|
26 seq = kseq_init(fp);
|
|
27 while ((l = kseq_read(seq)) >= 0) {
|
|
28 for (i = k = 0; i < seq->seq.l; ++i) {
|
|
29 if (islower(seq->seq.s[i])) seq->seq.s[k++] = toupper(seq->seq.s[i]);
|
|
30 else if (isupper(seq->seq.s[i])) seq->seq.s[k++] = seq->seq.s[i];
|
|
31 }
|
|
32 MD5Init(&md5_one);
|
|
33 MD5Update(&md5_one, (unsigned char*)seq->seq.s, k);
|
|
34 MD5Final(digest, &md5_one);
|
|
35 for (l = 0; l < 16; ++l) {
|
|
36 printf("%c%c", HEX_STR[digest[l]>>4&0xf], HEX_STR[digest[l]&0xf]);
|
|
37 unordered[l] ^= digest[l];
|
|
38 }
|
|
39 printf(" %s %s\n", fn, seq->name.s);
|
|
40 MD5Update(&md5_all, (unsigned char*)seq->seq.s, k);
|
|
41 }
|
|
42 MD5Final(digest, &md5_all);
|
|
43 kseq_destroy(seq);
|
|
44 for (l = 0; l < 16; ++l)
|
|
45 printf("%c%c", HEX_STR[digest[l]>>4&0xf], HEX_STR[digest[l]&0xf]);
|
|
46 printf(" %s >ordered\n", fn);
|
|
47 for (l = 0; l < 16; ++l)
|
|
48 printf("%c%c", HEX_STR[unordered[l]>>4&0xf], HEX_STR[unordered[l]&0xf]);
|
|
49 printf(" %s >unordered\n", fn);
|
|
50 }
|
|
51
|
|
52 int main(int argc, char *argv[])
|
|
53 {
|
|
54 int i;
|
|
55 if (argc == 1) md5_one("-");
|
|
56 else for (i = 1; i < argc; ++i) md5_one(argv[i]);
|
|
57 return 0;
|
|
58 }
|