Mercurial > hg > toybox
annotate toys/sha1sum.c @ 226:6aac63925eff
Update web pages.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 05 Jan 2008 18:09:49 -0600 |
parents | 8c0809cee2b0 |
children | d4176f3f3835 |
rev | line source |
---|---|
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
2 * Copyright 2007 Rob Landley <rob@landley.net> |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
3 * |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
4 * Based on the public domain SHA-1 in C by Steve Reid <steve@edmweb.com> |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
5 * from http://www.mirrors.wiretapped.net/security/cryptography/hashes/sha1/ |
192 | 6 * |
7 * Not in SUSv3. | |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
8 */ |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
10 #include <toys.h> |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
12 struct sha1 { |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
13 uint32_t state[5]; |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
14 uint32_t oldstate[5]; |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
15 uint64_t count; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
16 union { |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
17 unsigned char c[64]; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
18 uint32_t i[16]; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
19 } buffer; |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
20 }; |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 |
192 | 22 static void sha1_init(struct sha1 *this); |
23 static void sha1_transform(struct sha1 *this); | |
24 static void sha1_update(struct sha1 *this, char *data, unsigned int len); | |
25 static void sha1_final(struct sha1 *this, char digest[20]); | |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 |
192 | 29 // blk0() and blk() perform the initial expand. |
30 // The idea of expanding during the round function comes from SSLeay | |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
31 #if 1 |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
32 #define blk0(i) (block[i] = (rol(block[i],24)&0xFF00FF00) \ |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
33 |(rol(block[i],8)&0x00FF00FF)) |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
34 #else // big endian? |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
35 #define blk0(i) block[i] |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 #endif |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
37 #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \ |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
38 ^block[(i+2)&15]^block[i&15],1)) |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
40 static const uint32_t rconsts[]={0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6}; |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 |
192 | 42 // Hash a single 512-bit block. This is the core of the algorithm. |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 |
192 | 44 static void sha1_transform(struct sha1 *this) |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 { |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
46 int i, j, k, count; |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
47 uint32_t *block = this->buffer.i; |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
48 uint32_t *rot[5], *temp; |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
49 |
192 | 50 // Copy context->state[] to working vars |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
51 for (i=0; i<5; i++) { |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
52 this->oldstate[i] = this->state[i]; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
53 rot[i] = this->state + i; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
54 } |
192 | 55 // 4 rounds of 20 operations each. |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
56 for (i=count=0; i<4; i++) { |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
57 for (j=0; j<20; j++) { |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
58 uint32_t work; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
59 |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
60 work = *rot[2] ^ *rot[3]; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
61 if (!i) work = (work & *rot[1]) ^ *rot[3]; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
62 else { |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
63 if (i==2) |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
64 work = ((*rot[1]|*rot[2])&*rot[3])|(*rot[1]&*rot[2]); |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
65 else work ^= *rot[1]; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
66 } |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
67 if (!i && j<16) work += blk0(count); |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
68 else work += blk(count); |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
69 *rot[4] += work + rol(*rot[0],5) + rconsts[i]; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
70 *rot[1] = rol(*rot[1],30); |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
71 |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
72 // Rotate by one for next time. |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
73 temp = rot[4]; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
74 for (k=4; k; k--) rot[k] = rot[k-1]; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
75 *rot = temp; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
76 count++; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
77 } |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
78 } |
192 | 79 // Add the previous values of state[] |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
80 for (i=0; i<5; i++) this->state[i] += this->oldstate[i]; |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
81 } |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
82 |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 |
192 | 84 // Initialize a struct sha1. |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 |
192 | 86 static void sha1_init(struct sha1 *this) |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
87 { |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
88 /* SHA1 initialization constants */ |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
89 this->state[0] = 0x67452301; |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
90 this->state[1] = 0xEFCDAB89; |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
91 this->state[2] = 0x98BADCFE; |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
92 this->state[3] = 0x10325476; |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
93 this->state[4] = 0xC3D2E1F0; |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
94 this->count = 0; |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 } |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
96 |
192 | 97 // Fill the 64-byte working buffer and call sha1_transform() when full. |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
98 |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
99 void sha1_update(struct sha1 *this, char *data, unsigned int len) |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
100 { |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
101 unsigned int i, j; |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
102 |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
103 j = this->count & 63; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
104 this->count += len; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
105 |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
106 // Enough data to process a frame? |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
107 if ((j + len) > 63) { |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
108 i = 64-j; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
109 memcpy(this->buffer.c + j, data, i); |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
110 sha1_transform(this); |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
111 for ( ; i + 63 < len; i += 64) { |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
112 memcpy(this->buffer.c, data + i, 64); |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
113 sha1_transform(this); |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
114 } |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
115 j = 0; |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
116 } else i = 0; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
117 // Grab remaining chunk |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
118 memcpy(this->buffer.c + j, data + i, len - i); |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
119 } |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
120 |
192 | 121 // Add padding and return the message digest. |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
122 |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
123 void sha1_final(struct sha1 *this, char digest[20]) |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 { |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
125 uint64_t count = this->count << 3; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
126 unsigned int i; |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
127 char buf; |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
128 |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
129 // End the message by appending a "1" bit to the data, ending with the |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
130 // message size (in bits, big endian), and adding enough zero bits in |
192 | 131 // between to pad to the end of the next 64-byte frame. |
132 // | |
133 // Since our input up to now has been in whole bytes, we can deal with | |
134 // bytes here too. | |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
135 |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
136 buf = 0x80; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
137 do { |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
138 sha1_update(this, &buf, 1); |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
139 buf = 0; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
140 } while ((this->count & 63) != 56); |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
141 for (i = 0; i < 8; i++) |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
142 this->buffer.c[56+i] = count >> (8*(7-i)); |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
143 sha1_transform(this); |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
144 |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
145 for (i = 0; i < 20; i++) |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
146 digest[i] = this->state[i>>2] >> ((3-(i & 3)) * 8); |
192 | 147 // Wipe variables. Cryptogropher paranoia. |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
148 memset(this, 0, sizeof(struct sha1)); |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
149 } |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
150 |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
151 // Callback for loopfiles() |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
152 |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
153 static void do_sha1(int fd, char *name) |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
154 { |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
155 struct sha1 this; |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
156 int len; |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
157 |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
158 sha1_init(&this); |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
159 for (;;) { |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
160 len = read(fd, toybuf, sizeof(toybuf)); |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
161 if (len<1) break; |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
162 sha1_update(&this, toybuf, len); |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
163 } |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
164 sha1_final(&this, toybuf); |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
165 for (len = 0; len < 20; len++) printf("%02x", toybuf[len]); |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
166 printf(" %s\n", name); |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
167 } |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
168 |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
169 void sha1sum_main(void) |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
170 { |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
171 loopfiles(toys.optargs, do_sha1); |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
172 } |