Mercurial > hg > toybox
annotate toys/lsb/md5sum.c @ 1566:62a7d617e1ce draft 0.5.1
Make md5sum and sha1sum work on big endian systems.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 19 Nov 2014 21:38:00 -0600 |
parents | 319e79bab052 |
children |
rev | line source |
---|---|
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
685
diff
changeset
|
1 /* md5sum.c - Calculate RFC 1321 md5 hash and sha1 hash. |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * Copyright 2012 Rob Landley <rob@landley.net> |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 * |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/md5sum.html |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 * and http://www.ietf.org/rfc/rfc1321.txt |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 * |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 * They're combined this way to share infrastructure, and because md5sum is |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 * and LSB standard command, sha1sum is just a good idea. |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 |
1335
d90f692a50d4
Make md5sum/sha1sum -b flag be "brief" output (just the hash).
Rob Landley <rob@landley.net>
parents:
1286
diff
changeset
|
11 USE_MD5SUM(NEWTOY(md5sum, "b", TOYFLAG_USR|TOYFLAG_BIN)) |
1499
319e79bab052
Separate more commands so single.sh can build them standalone.
Rob Landley <rob@landley.net>
parents:
1335
diff
changeset
|
12 USE_SHA1SUM(NEWTOY(sha1sum, "b", TOYFLAG_USR|TOYFLAG_BIN)) |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 config MD5SUM |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
15 bool "md5sum" |
685
38e07dba9b20
Make sha1sum and md5sum default to y.
Rob Landley <rob@landley.net>
parents:
680
diff
changeset
|
16 default y |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 help |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 usage: md5sum [FILE]... |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
19 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
20 Calculate md5 hash for each input file, reading from stdin if none. |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 Output one hash (16 hex digits) for each input file, followed by |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 filename. |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
23 |
1335
d90f692a50d4
Make md5sum/sha1sum -b flag be "brief" output (just the hash).
Rob Landley <rob@landley.net>
parents:
1286
diff
changeset
|
24 -b brief (hash only, no filename) |
d90f692a50d4
Make md5sum/sha1sum -b flag be "brief" output (just the hash).
Rob Landley <rob@landley.net>
parents:
1286
diff
changeset
|
25 |
1499
319e79bab052
Separate more commands so single.sh can build them standalone.
Rob Landley <rob@landley.net>
parents:
1335
diff
changeset
|
26 config SHA1SUM |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 bool "sha1sum" |
685
38e07dba9b20
Make sha1sum and md5sum default to y.
Rob Landley <rob@landley.net>
parents:
680
diff
changeset
|
28 default y |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 help |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 usage: sha1sum [FILE]... |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 |
1335
d90f692a50d4
Make md5sum/sha1sum -b flag be "brief" output (just the hash).
Rob Landley <rob@landley.net>
parents:
1286
diff
changeset
|
32 calculate sha1 hash for each input file, reading from stdin if none. |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 Output one hash (20 hex digits) for each input file, followed by |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 filename. |
1335
d90f692a50d4
Make md5sum/sha1sum -b flag be "brief" output (just the hash).
Rob Landley <rob@landley.net>
parents:
1286
diff
changeset
|
35 |
d90f692a50d4
Make md5sum/sha1sum -b flag be "brief" output (just the hash).
Rob Landley <rob@landley.net>
parents:
1286
diff
changeset
|
36 -b brief (hash only, no filename) |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 */ |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 #define FOR_md5sum |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 #include "toys.h" |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 GLOBALS( |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 unsigned state[5]; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 unsigned oldstate[5]; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 uint64_t count; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 union { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 char c[64]; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 unsigned i[16]; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 } buffer; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 ) |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 |
1286
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
52 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
53 |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 // for(i=0; i<64; i++) md5table[i] = abs(sin(i+1))*(1<<32); But calculating |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 // that involves not just floating point but pulling in -lm (and arguing with |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 // C about whether 1<<32 is a valid thing to do on 32 bit platforms) so: |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 static uint32_t md5table[64] = { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
68 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
70 }; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
71 |
1286
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
72 static const uint8_t md5rot[64] = { |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
73 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
74 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
75 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
76 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
77 }; |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
78 |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
79 // Mix next 64 bytes of data into md5 hash |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
80 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
81 static void md5_transform(void) |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
82 { |
1286
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
83 unsigned x[4], *b = TT.buffer.i; |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
84 int i; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
86 memcpy(x, TT.state, sizeof(x)); |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 for (i=0; i<64; i++) { |
1286
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
89 unsigned int in, temp, swap; |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 if (i<16) { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 in = i; |
1286
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
92 temp = x[1]; |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
93 temp = (temp & x[2]) | ((~temp) & x[3]); |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 } else if (i<32) { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 in = (1+(5*i))&15; |
1286
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
96 temp = x[3]; |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
97 temp = (x[1] & temp) | (x[2] & ~temp); |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
98 } else if (i<48) { |
1286
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
99 in = (3*i+5)&15; |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
100 temp = x[1] ^ x[2] ^ x[3]; |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
101 } else { |
1286
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
102 in = (7*i)&15; |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
103 temp = x[2] ^ (x[1] | ~x[3]); |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
104 } |
1286
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
105 temp += x[0] + b[in] + md5table[i]; |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
106 swap = x[3]; |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
107 x[3] = x[2]; |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
108 x[2] = x[1]; |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
109 x[1] += rol(temp, md5rot[i]); |
5bdd2ee6f3c4
Here's a quick cleanup of md5sum. Executive summary: smaller and faster.
Daniel Verkamp <daniel@drv.nu>
parents:
694
diff
changeset
|
110 x[0] = swap; |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
111 } |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
112 for (i=0; i<4; i++) TT.state[i] += x[i]; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
113 } |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
114 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
115 // Mix next 64 bytes of data into sha1 hash. |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
116 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
117 static const unsigned rconsts[]={0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6}; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
118 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
119 static void sha1_transform(void) |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
120 { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
121 int i, j, k, count; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
122 unsigned *block = TT.buffer.i; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
123 unsigned *rot[5], *temp; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
125 // Copy context->state[] to working vars |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
126 for (i=0; i<5; i++) { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
127 TT.oldstate[i] = TT.state[i]; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
128 rot[i] = TT.state + i; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
129 } |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
130 // 4 rounds of 20 operations each. |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
131 for (i=count=0; i<4; i++) { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
132 for (j=0; j<20; j++) { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
133 unsigned work; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
134 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
135 work = *rot[2] ^ *rot[3]; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
136 if (!i) work = (work & *rot[1]) ^ *rot[3]; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
137 else { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
138 if (i==2) work = ((*rot[1]|*rot[2])&*rot[3])|(*rot[1]&*rot[2]); |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
139 else work ^= *rot[1]; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
140 } |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
141 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
142 if (!i && j<16) |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
143 work += block[count] = (rol(block[count],24)&0xFF00FF00) |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
144 | (rol(block[count],8)&0x00FF00FF); |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
145 else |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
146 work += block[count&15] = rol(block[(count+13)&15] |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
147 ^ block[(count+8)&15] ^ block[(count+2)&15] ^ block[count&15], 1); |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
148 *rot[4] += work + rol(*rot[0],5) + rconsts[i]; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
149 *rot[1] = rol(*rot[1],30); |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
150 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
151 // Rotate by one for next time. |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
152 temp = rot[4]; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
153 for (k=4; k; k--) rot[k] = rot[k-1]; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
154 *rot = temp; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
155 count++; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
156 } |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
157 } |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
158 // Add the previous values of state[] |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
159 for (i=0; i<5; i++) TT.state[i] += TT.oldstate[i]; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
160 } |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
161 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
162 // Fill the 64-byte working buffer and call transform() when full. |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
163 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
164 static void hash_update(char *data, unsigned int len, void (*transform)(void)) |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
165 { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
166 unsigned int i, j; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
167 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
168 j = TT.count & 63; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
169 TT.count += len; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
170 |
1566
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
171 for (;;) { |
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
172 // Grab next chunk of data, return if it's not enough to process a frame |
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
173 i = 64 - j; |
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
174 if (i>len) i = len; |
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
175 memcpy(TT.buffer.c+j, data, i); |
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
176 if (j+i != 64) break; |
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
177 |
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
178 // Process a frame |
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
179 if (IS_BIG_ENDIAN) |
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
180 for (j=0; j<16; j++) TT.buffer.i[j] = SWAP_LE32(TT.buffer.i[j]); |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
181 transform(); |
1566
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
182 j=0; |
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
183 data += i; |
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
184 len -= i; |
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
185 } |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
186 } |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
187 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
188 // Callback for loopfiles() |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
189 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
190 static void do_hash(int fd, char *name) |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
191 { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
192 uint64_t count; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
193 int i, sha1=toys.which->name[0]=='s';; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
194 char buf; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
195 void (*transform)(void); |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
196 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
197 /* SHA1 initialization constants (md5sum uses first 4) */ |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
198 TT.state[0] = 0x67452301; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
199 TT.state[1] = 0xEFCDAB89; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
200 TT.state[2] = 0x98BADCFE; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
201 TT.state[3] = 0x10325476; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
202 TT.state[4] = 0xC3D2E1F0; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
203 TT.count = 0; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
204 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
205 transform = sha1 ? sha1_transform : md5_transform; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
206 for (;;) { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
207 i = read(fd, toybuf, sizeof(toybuf)); |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
208 if (i<1) break; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
209 hash_update(toybuf, i, transform); |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
210 } |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
211 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
212 count = TT.count << 3; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
213 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
214 // End the message by appending a "1" bit to the data, ending with the |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
215 // message size (in bits, big endian), and adding enough zero bits in |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
216 // between to pad to the end of the next 64-byte frame. |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
217 // |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
218 // Since our input up to now has been in whole bytes, we can deal with |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
219 // bytes here too. |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
220 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
221 buf = 0x80; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
222 do { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
223 hash_update(&buf, 1, transform); |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
224 buf = 0; |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
225 } while ((TT.count & 63) != 56); |
1566
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
226 count = sha1 ? SWAP_BE64(count) : SWAP_LE64(count); |
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
227 hash_update((void *)&count, 8, transform); |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
228 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
229 if (sha1) |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
230 for (i = 0; i < 20; i++) |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
231 printf("%02x", 255&(TT.state[i>>2] >> ((3-(i & 3)) * 8))); |
1566
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
232 else for (i=0; i<4; i++) printf("%08x", bswap_32(TT.state[i])); |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
233 |
1566
62a7d617e1ce
Make md5sum and sha1sum work on big endian systems.
Rob Landley <rob@landley.net>
parents:
1499
diff
changeset
|
234 // Wipe variables. Cryptographer paranoia. |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
235 memset(&TT, 0, sizeof(TT)); |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
236 |
1335
d90f692a50d4
Make md5sum/sha1sum -b flag be "brief" output (just the hash).
Rob Landley <rob@landley.net>
parents:
1286
diff
changeset
|
237 printf((toys.optflags & FLAG_b) ? "\n" : " %s\n", name); |
680
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
238 } |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
239 |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
240 void md5sum_main(void) |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
241 { |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
242 loopfiles(toys.optargs, do_hash); |
ff2cf02d9703
Add md5sum in lsb, combine sha1sum (mostly shared infrastructure). Downside: current infrastructure can't give them different help text. Hmmm...
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
243 } |
1499
319e79bab052
Separate more commands so single.sh can build them standalone.
Rob Landley <rob@landley.net>
parents:
1335
diff
changeset
|
244 |
319e79bab052
Separate more commands so single.sh can build them standalone.
Rob Landley <rob@landley.net>
parents:
1335
diff
changeset
|
245 void sha1sum_main(void) |
319e79bab052
Separate more commands so single.sh can build them standalone.
Rob Landley <rob@landley.net>
parents:
1335
diff
changeset
|
246 { |
319e79bab052
Separate more commands so single.sh can build them standalone.
Rob Landley <rob@landley.net>
parents:
1335
diff
changeset
|
247 md5sum_main(); |
319e79bab052
Separate more commands so single.sh can build them standalone.
Rob Landley <rob@landley.net>
parents:
1335
diff
changeset
|
248 } |