taf2-md5-partial 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +51 -0
- data/README +16 -0
- data/Rakefile +24 -0
- data/ext/extconf.rb +4 -0
- data/ext/md5.c +433 -0
- data/ext/md5.h +74 -0
- data/test/test.rb +45 -0
- metadata +61 -0
data/LICENSE
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Copyright (c) 2009 Todd A Fisher (todd DOT fisher AT gmail DOT com ).
|
|
2
|
+
Curb is free software licensed under the following terms:
|
|
3
|
+
|
|
4
|
+
1. You may make and give away verbatim copies of the source form of the
|
|
5
|
+
software without restriction, provided that you duplicate all of the
|
|
6
|
+
original copyright notices and associated disclaimers.
|
|
7
|
+
|
|
8
|
+
2. You may modify your copy of the software in any way, provided that
|
|
9
|
+
you do at least ONE of the following:
|
|
10
|
+
|
|
11
|
+
a) place your modifications in the Public Domain or otherwise
|
|
12
|
+
make them Freely Available, such as by posting said
|
|
13
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
|
14
|
+
the author to include your modifications in the software.
|
|
15
|
+
|
|
16
|
+
b) use the modified software only within your corporation or
|
|
17
|
+
organization.
|
|
18
|
+
|
|
19
|
+
c) give non-standard binaries non-standard names, with
|
|
20
|
+
instructions on where to get the original software distribution.
|
|
21
|
+
|
|
22
|
+
d) make other distribution arrangements with the author.
|
|
23
|
+
|
|
24
|
+
3. You may distribute the software in object code or binary form,
|
|
25
|
+
provided that you do at least ONE of the following:
|
|
26
|
+
|
|
27
|
+
a) distribute the binaries and library files of the software,
|
|
28
|
+
together with instructions (in the manual page or equivalent)
|
|
29
|
+
on where to get the original distribution.
|
|
30
|
+
|
|
31
|
+
b) accompany the distribution with the machine-readable source of
|
|
32
|
+
the software.
|
|
33
|
+
|
|
34
|
+
c) give non-standard binaries non-standard names, with
|
|
35
|
+
instructions on where to get the original software distribution.
|
|
36
|
+
|
|
37
|
+
d) make other distribution arrangements with the author.
|
|
38
|
+
|
|
39
|
+
4. You may modify and include the part of the software into any other
|
|
40
|
+
software (possibly commercial).
|
|
41
|
+
|
|
42
|
+
5. The scripts and library files supplied as input to or produced as
|
|
43
|
+
output from the software do not automatically fall under the
|
|
44
|
+
copyright of the software, but belong to whomever generated them,
|
|
45
|
+
and may be sold commercially, and may be aggregated with this
|
|
46
|
+
software.
|
|
47
|
+
|
|
48
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
|
49
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
50
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
51
|
+
PURPOSE.
|
data/README
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Make it possible to store a partial md5 calculation for later use.
|
|
2
|
+
Have you ever received a very large file and wanted to calculate a checksum for the file to verify it's correctness?
|
|
3
|
+
You probably noticed it took a very long time to compute. What if you could calculate the checksum as you received the file.
|
|
4
|
+
Okay you can do this with existing Digest::MD5, but what if you received a little bit of the file and stopped. The process receiving
|
|
5
|
+
or server receiving the file stopped. Then sometime later you are able to resume receiving the file. You don't want to have to start
|
|
6
|
+
recalculating the file from the beginning again, because lets say you already received 75% of the file. If it took a long time to compute
|
|
7
|
+
the MD5 for 100% of the file it will still take some time for 75% of the file. MD5Partial allows you to save the computation to disk, or any
|
|
8
|
+
other storage so that you can continue from where ever you left off.
|
|
9
|
+
|
|
10
|
+
It has exactly the same interface as Digest::MD5, but adds 2 additional functions, save and restore.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
Here's how it works, the MD5_CTX structure can be serialized to a string. Once in string form it can be stored using any method you like...
|
|
14
|
+
file system, database, etc...
|
|
15
|
+
|
|
16
|
+
see test/test.rb for example usage
|
data/Rakefile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'rake/clean'
|
|
2
|
+
|
|
3
|
+
DLEXT=Config::MAKEFILE_CONFIG['DLEXT']
|
|
4
|
+
CLEAN.include '**/*.o'
|
|
5
|
+
CLEAN.include "**/*.#{DLEXT}"
|
|
6
|
+
CLOBBER.include '**/*.log'
|
|
7
|
+
CLOBBER.include '**/Makefile'
|
|
8
|
+
TARGET="md5partial.#{DLEXT}"
|
|
9
|
+
|
|
10
|
+
task :default => :test
|
|
11
|
+
|
|
12
|
+
file 'ext/Makefile' => 'ext/extconf.rb' do
|
|
13
|
+
Dir.chdir('ext') { ruby 'extconf.rb' }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
file TARGET => (['ext/Makefile'] + Dir['ext/*.c'] + Dir['ext/*.h']) do
|
|
17
|
+
Dir.chdir('ext') { system("make") }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
task :compile => TARGET
|
|
21
|
+
|
|
22
|
+
task :test => :compile do
|
|
23
|
+
Dir.chdir('test') { ruby 'test.rb' }
|
|
24
|
+
end
|
data/ext/extconf.rb
ADDED
data/ext/md5.c
ADDED
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (C) 1999, 2000 Aladdin Enterprises. All rights reserved.
|
|
3
|
+
|
|
4
|
+
This software is provided 'as-is', without any express or implied
|
|
5
|
+
warranty. In no event will the authors be held liable for any damages
|
|
6
|
+
arising from the use of this software.
|
|
7
|
+
|
|
8
|
+
Permission is granted to anyone to use this software for any purpose,
|
|
9
|
+
including commercial applications, and to alter it and redistribute it
|
|
10
|
+
freely, subject to the following restrictions:
|
|
11
|
+
|
|
12
|
+
1. The origin of this software must not be misrepresented; you must not
|
|
13
|
+
claim that you wrote the original software. If you use this software
|
|
14
|
+
in a product, an acknowledgment in the product documentation would be
|
|
15
|
+
appreciated but is not required.
|
|
16
|
+
2. Altered source versions must be plainly marked as such, and must not be
|
|
17
|
+
misrepresented as being the original software.
|
|
18
|
+
3. This notice may not be removed or altered from any source distribution.
|
|
19
|
+
|
|
20
|
+
L. Peter Deutsch
|
|
21
|
+
ghost@aladdin.com
|
|
22
|
+
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/*
|
|
26
|
+
Independent implementation of MD5 (RFC 1321).
|
|
27
|
+
|
|
28
|
+
This code implements the MD5 Algorithm defined in RFC 1321.
|
|
29
|
+
It is derived directly from the text of the RFC and not from the
|
|
30
|
+
reference implementation.
|
|
31
|
+
|
|
32
|
+
The original and principal author of md5.c is L. Peter Deutsch
|
|
33
|
+
<ghost@aladdin.com>. Other authors are noted in the change history
|
|
34
|
+
that follows (in reverse chronological order):
|
|
35
|
+
|
|
36
|
+
2000-07-03 lpd Patched to eliminate warnings about "constant is
|
|
37
|
+
unsigned in ANSI C, signed in traditional";
|
|
38
|
+
made test program self-checking.
|
|
39
|
+
1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
|
|
40
|
+
1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
|
|
41
|
+
1999-05-03 lpd Original version.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/*
|
|
45
|
+
This code was modified for use in Ruby.
|
|
46
|
+
|
|
47
|
+
- Akinori MUSHA <knu@idaemons.org>
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
/*$OrigId: md5c.c,v 1.2 2001/03/26 08:57:14 matz Exp $ */
|
|
51
|
+
/*$RoughId: md5.c,v 1.2 2001/07/13 19:48:41 knu Exp $ */
|
|
52
|
+
/*$Id: md5.c 18459 2008-08-09 23:15:08Z nobu $ */
|
|
53
|
+
|
|
54
|
+
#include "md5.h"
|
|
55
|
+
#include <stdio.h>
|
|
56
|
+
#include <string.h>
|
|
57
|
+
|
|
58
|
+
#ifdef T_MASK
|
|
59
|
+
#undef T_MASK
|
|
60
|
+
#endif
|
|
61
|
+
#define T_MASK ((uint32_t)~0)
|
|
62
|
+
#define T1 /* 0xd76aa478 */ (T_MASK ^ 0x28955b87)
|
|
63
|
+
#define T2 /* 0xe8c7b756 */ (T_MASK ^ 0x173848a9)
|
|
64
|
+
#define T3 0x242070db
|
|
65
|
+
#define T4 /* 0xc1bdceee */ (T_MASK ^ 0x3e423111)
|
|
66
|
+
#define T5 /* 0xf57c0faf */ (T_MASK ^ 0x0a83f050)
|
|
67
|
+
#define T6 0x4787c62a
|
|
68
|
+
#define T7 /* 0xa8304613 */ (T_MASK ^ 0x57cfb9ec)
|
|
69
|
+
#define T8 /* 0xfd469501 */ (T_MASK ^ 0x02b96afe)
|
|
70
|
+
#define T9 0x698098d8
|
|
71
|
+
#define T10 /* 0x8b44f7af */ (T_MASK ^ 0x74bb0850)
|
|
72
|
+
#define T11 /* 0xffff5bb1 */ (T_MASK ^ 0x0000a44e)
|
|
73
|
+
#define T12 /* 0x895cd7be */ (T_MASK ^ 0x76a32841)
|
|
74
|
+
#define T13 0x6b901122
|
|
75
|
+
#define T14 /* 0xfd987193 */ (T_MASK ^ 0x02678e6c)
|
|
76
|
+
#define T15 /* 0xa679438e */ (T_MASK ^ 0x5986bc71)
|
|
77
|
+
#define T16 0x49b40821
|
|
78
|
+
#define T17 /* 0xf61e2562 */ (T_MASK ^ 0x09e1da9d)
|
|
79
|
+
#define T18 /* 0xc040b340 */ (T_MASK ^ 0x3fbf4cbf)
|
|
80
|
+
#define T19 0x265e5a51
|
|
81
|
+
#define T20 /* 0xe9b6c7aa */ (T_MASK ^ 0x16493855)
|
|
82
|
+
#define T21 /* 0xd62f105d */ (T_MASK ^ 0x29d0efa2)
|
|
83
|
+
#define T22 0x02441453
|
|
84
|
+
#define T23 /* 0xd8a1e681 */ (T_MASK ^ 0x275e197e)
|
|
85
|
+
#define T24 /* 0xe7d3fbc8 */ (T_MASK ^ 0x182c0437)
|
|
86
|
+
#define T25 0x21e1cde6
|
|
87
|
+
#define T26 /* 0xc33707d6 */ (T_MASK ^ 0x3cc8f829)
|
|
88
|
+
#define T27 /* 0xf4d50d87 */ (T_MASK ^ 0x0b2af278)
|
|
89
|
+
#define T28 0x455a14ed
|
|
90
|
+
#define T29 /* 0xa9e3e905 */ (T_MASK ^ 0x561c16fa)
|
|
91
|
+
#define T30 /* 0xfcefa3f8 */ (T_MASK ^ 0x03105c07)
|
|
92
|
+
#define T31 0x676f02d9
|
|
93
|
+
#define T32 /* 0x8d2a4c8a */ (T_MASK ^ 0x72d5b375)
|
|
94
|
+
#define T33 /* 0xfffa3942 */ (T_MASK ^ 0x0005c6bd)
|
|
95
|
+
#define T34 /* 0x8771f681 */ (T_MASK ^ 0x788e097e)
|
|
96
|
+
#define T35 0x6d9d6122
|
|
97
|
+
#define T36 /* 0xfde5380c */ (T_MASK ^ 0x021ac7f3)
|
|
98
|
+
#define T37 /* 0xa4beea44 */ (T_MASK ^ 0x5b4115bb)
|
|
99
|
+
#define T38 0x4bdecfa9
|
|
100
|
+
#define T39 /* 0xf6bb4b60 */ (T_MASK ^ 0x0944b49f)
|
|
101
|
+
#define T40 /* 0xbebfbc70 */ (T_MASK ^ 0x4140438f)
|
|
102
|
+
#define T41 0x289b7ec6
|
|
103
|
+
#define T42 /* 0xeaa127fa */ (T_MASK ^ 0x155ed805)
|
|
104
|
+
#define T43 /* 0xd4ef3085 */ (T_MASK ^ 0x2b10cf7a)
|
|
105
|
+
#define T44 0x04881d05
|
|
106
|
+
#define T45 /* 0xd9d4d039 */ (T_MASK ^ 0x262b2fc6)
|
|
107
|
+
#define T46 /* 0xe6db99e5 */ (T_MASK ^ 0x1924661a)
|
|
108
|
+
#define T47 0x1fa27cf8
|
|
109
|
+
#define T48 /* 0xc4ac5665 */ (T_MASK ^ 0x3b53a99a)
|
|
110
|
+
#define T49 /* 0xf4292244 */ (T_MASK ^ 0x0bd6ddbb)
|
|
111
|
+
#define T50 0x432aff97
|
|
112
|
+
#define T51 /* 0xab9423a7 */ (T_MASK ^ 0x546bdc58)
|
|
113
|
+
#define T52 /* 0xfc93a039 */ (T_MASK ^ 0x036c5fc6)
|
|
114
|
+
#define T53 0x655b59c3
|
|
115
|
+
#define T54 /* 0x8f0ccc92 */ (T_MASK ^ 0x70f3336d)
|
|
116
|
+
#define T55 /* 0xffeff47d */ (T_MASK ^ 0x00100b82)
|
|
117
|
+
#define T56 /* 0x85845dd1 */ (T_MASK ^ 0x7a7ba22e)
|
|
118
|
+
#define T57 0x6fa87e4f
|
|
119
|
+
#define T58 /* 0xfe2ce6e0 */ (T_MASK ^ 0x01d3191f)
|
|
120
|
+
#define T59 /* 0xa3014314 */ (T_MASK ^ 0x5cfebceb)
|
|
121
|
+
#define T60 0x4e0811a1
|
|
122
|
+
#define T61 /* 0xf7537e82 */ (T_MASK ^ 0x08ac817d)
|
|
123
|
+
#define T62 /* 0xbd3af235 */ (T_MASK ^ 0x42c50dca)
|
|
124
|
+
#define T63 0x2ad7d2bb
|
|
125
|
+
#define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
static void
|
|
129
|
+
md5_process(MD5_CTX *pms, const uint8_t *data /*[64]*/)
|
|
130
|
+
{
|
|
131
|
+
uint32_t
|
|
132
|
+
a = pms->state[0], b = pms->state[1],
|
|
133
|
+
c = pms->state[2], d = pms->state[3];
|
|
134
|
+
uint32_t t;
|
|
135
|
+
|
|
136
|
+
#ifdef WORDS_BIGENDIAN
|
|
137
|
+
|
|
138
|
+
/*
|
|
139
|
+
* On big-endian machines, we must arrange the bytes in the right
|
|
140
|
+
* order. (This also works on machines of unknown byte order.)
|
|
141
|
+
*/
|
|
142
|
+
uint32_t X[16];
|
|
143
|
+
const uint8_t *xp = data;
|
|
144
|
+
int i;
|
|
145
|
+
|
|
146
|
+
for (i = 0; i < 16; ++i, xp += 4)
|
|
147
|
+
X[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
|
|
148
|
+
|
|
149
|
+
#else
|
|
150
|
+
|
|
151
|
+
/*
|
|
152
|
+
* On little-endian machines, we can process properly aligned data
|
|
153
|
+
* without copying it.
|
|
154
|
+
*/
|
|
155
|
+
uint32_t xbuf[16];
|
|
156
|
+
const uint32_t *X;
|
|
157
|
+
|
|
158
|
+
if (!((data - (const uint8_t *)0) & 3)) {
|
|
159
|
+
/* data are properly aligned */
|
|
160
|
+
X = (const uint32_t *)data;
|
|
161
|
+
} else {
|
|
162
|
+
/* not aligned */
|
|
163
|
+
memcpy(xbuf, data, 64);
|
|
164
|
+
X = xbuf;
|
|
165
|
+
}
|
|
166
|
+
#endif
|
|
167
|
+
|
|
168
|
+
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
|
|
169
|
+
|
|
170
|
+
/* Round 1. */
|
|
171
|
+
/* Let [abcd k s i] denote the operation
|
|
172
|
+
a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
|
|
173
|
+
#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
|
|
174
|
+
#define SET(a, b, c, d, k, s, Ti)\
|
|
175
|
+
t = a + F(b,c,d) + X[k] + Ti;\
|
|
176
|
+
a = ROTATE_LEFT(t, s) + b
|
|
177
|
+
/* Do the following 16 operations. */
|
|
178
|
+
SET(a, b, c, d, 0, 7, T1);
|
|
179
|
+
SET(d, a, b, c, 1, 12, T2);
|
|
180
|
+
SET(c, d, a, b, 2, 17, T3);
|
|
181
|
+
SET(b, c, d, a, 3, 22, T4);
|
|
182
|
+
SET(a, b, c, d, 4, 7, T5);
|
|
183
|
+
SET(d, a, b, c, 5, 12, T6);
|
|
184
|
+
SET(c, d, a, b, 6, 17, T7);
|
|
185
|
+
SET(b, c, d, a, 7, 22, T8);
|
|
186
|
+
SET(a, b, c, d, 8, 7, T9);
|
|
187
|
+
SET(d, a, b, c, 9, 12, T10);
|
|
188
|
+
SET(c, d, a, b, 10, 17, T11);
|
|
189
|
+
SET(b, c, d, a, 11, 22, T12);
|
|
190
|
+
SET(a, b, c, d, 12, 7, T13);
|
|
191
|
+
SET(d, a, b, c, 13, 12, T14);
|
|
192
|
+
SET(c, d, a, b, 14, 17, T15);
|
|
193
|
+
SET(b, c, d, a, 15, 22, T16);
|
|
194
|
+
#undef SET
|
|
195
|
+
|
|
196
|
+
/* Round 2. */
|
|
197
|
+
/* Let [abcd k s i] denote the operation
|
|
198
|
+
a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
|
|
199
|
+
#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
|
|
200
|
+
#define SET(a, b, c, d, k, s, Ti)\
|
|
201
|
+
t = a + G(b,c,d) + X[k] + Ti;\
|
|
202
|
+
a = ROTATE_LEFT(t, s) + b
|
|
203
|
+
/* Do the following 16 operations. */
|
|
204
|
+
SET(a, b, c, d, 1, 5, T17);
|
|
205
|
+
SET(d, a, b, c, 6, 9, T18);
|
|
206
|
+
SET(c, d, a, b, 11, 14, T19);
|
|
207
|
+
SET(b, c, d, a, 0, 20, T20);
|
|
208
|
+
SET(a, b, c, d, 5, 5, T21);
|
|
209
|
+
SET(d, a, b, c, 10, 9, T22);
|
|
210
|
+
SET(c, d, a, b, 15, 14, T23);
|
|
211
|
+
SET(b, c, d, a, 4, 20, T24);
|
|
212
|
+
SET(a, b, c, d, 9, 5, T25);
|
|
213
|
+
SET(d, a, b, c, 14, 9, T26);
|
|
214
|
+
SET(c, d, a, b, 3, 14, T27);
|
|
215
|
+
SET(b, c, d, a, 8, 20, T28);
|
|
216
|
+
SET(a, b, c, d, 13, 5, T29);
|
|
217
|
+
SET(d, a, b, c, 2, 9, T30);
|
|
218
|
+
SET(c, d, a, b, 7, 14, T31);
|
|
219
|
+
SET(b, c, d, a, 12, 20, T32);
|
|
220
|
+
#undef SET
|
|
221
|
+
|
|
222
|
+
/* Round 3. */
|
|
223
|
+
/* Let [abcd k s t] denote the operation
|
|
224
|
+
a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
|
|
225
|
+
#define H(x, y, z) ((x) ^ (y) ^ (z))
|
|
226
|
+
#define SET(a, b, c, d, k, s, Ti)\
|
|
227
|
+
t = a + H(b,c,d) + X[k] + Ti;\
|
|
228
|
+
a = ROTATE_LEFT(t, s) + b
|
|
229
|
+
/* Do the following 16 operations. */
|
|
230
|
+
SET(a, b, c, d, 5, 4, T33);
|
|
231
|
+
SET(d, a, b, c, 8, 11, T34);
|
|
232
|
+
SET(c, d, a, b, 11, 16, T35);
|
|
233
|
+
SET(b, c, d, a, 14, 23, T36);
|
|
234
|
+
SET(a, b, c, d, 1, 4, T37);
|
|
235
|
+
SET(d, a, b, c, 4, 11, T38);
|
|
236
|
+
SET(c, d, a, b, 7, 16, T39);
|
|
237
|
+
SET(b, c, d, a, 10, 23, T40);
|
|
238
|
+
SET(a, b, c, d, 13, 4, T41);
|
|
239
|
+
SET(d, a, b, c, 0, 11, T42);
|
|
240
|
+
SET(c, d, a, b, 3, 16, T43);
|
|
241
|
+
SET(b, c, d, a, 6, 23, T44);
|
|
242
|
+
SET(a, b, c, d, 9, 4, T45);
|
|
243
|
+
SET(d, a, b, c, 12, 11, T46);
|
|
244
|
+
SET(c, d, a, b, 15, 16, T47);
|
|
245
|
+
SET(b, c, d, a, 2, 23, T48);
|
|
246
|
+
#undef SET
|
|
247
|
+
|
|
248
|
+
/* Round 4. */
|
|
249
|
+
/* Let [abcd k s t] denote the operation
|
|
250
|
+
a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
|
|
251
|
+
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
|
|
252
|
+
#define SET(a, b, c, d, k, s, Ti)\
|
|
253
|
+
t = a + I(b,c,d) + X[k] + Ti;\
|
|
254
|
+
a = ROTATE_LEFT(t, s) + b
|
|
255
|
+
/* Do the following 16 operations. */
|
|
256
|
+
SET(a, b, c, d, 0, 6, T49);
|
|
257
|
+
SET(d, a, b, c, 7, 10, T50);
|
|
258
|
+
SET(c, d, a, b, 14, 15, T51);
|
|
259
|
+
SET(b, c, d, a, 5, 21, T52);
|
|
260
|
+
SET(a, b, c, d, 12, 6, T53);
|
|
261
|
+
SET(d, a, b, c, 3, 10, T54);
|
|
262
|
+
SET(c, d, a, b, 10, 15, T55);
|
|
263
|
+
SET(b, c, d, a, 1, 21, T56);
|
|
264
|
+
SET(a, b, c, d, 8, 6, T57);
|
|
265
|
+
SET(d, a, b, c, 15, 10, T58);
|
|
266
|
+
SET(c, d, a, b, 6, 15, T59);
|
|
267
|
+
SET(b, c, d, a, 13, 21, T60);
|
|
268
|
+
SET(a, b, c, d, 4, 6, T61);
|
|
269
|
+
SET(d, a, b, c, 11, 10, T62);
|
|
270
|
+
SET(c, d, a, b, 2, 15, T63);
|
|
271
|
+
SET(b, c, d, a, 9, 21, T64);
|
|
272
|
+
#undef SET
|
|
273
|
+
|
|
274
|
+
/* Then perform the following additions. (That is increment each
|
|
275
|
+
of the four registers by the value it had before this block
|
|
276
|
+
was started.) */
|
|
277
|
+
pms->state[0] += a;
|
|
278
|
+
pms->state[1] += b;
|
|
279
|
+
pms->state[2] += c;
|
|
280
|
+
pms->state[3] += d;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
void
|
|
284
|
+
rb_MD5_Init(MD5_CTX *pms)
|
|
285
|
+
{
|
|
286
|
+
pms->count[0] = pms->count[1] = 0;
|
|
287
|
+
pms->state[0] = 0x67452301;
|
|
288
|
+
pms->state[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476;
|
|
289
|
+
pms->state[2] = /*0x98badcfe*/ T_MASK ^ 0x67452301;
|
|
290
|
+
pms->state[3] = 0x10325476;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
void
|
|
294
|
+
rb_MD5_Update(MD5_CTX *pms, const uint8_t *data, size_t nbytes)
|
|
295
|
+
{
|
|
296
|
+
const uint8_t *p = data;
|
|
297
|
+
size_t left = nbytes;
|
|
298
|
+
size_t offset = (pms->count[0] >> 3) & 63;
|
|
299
|
+
uint32_t nbits = (uint32_t)(nbytes << 3);
|
|
300
|
+
|
|
301
|
+
if (nbytes <= 0)
|
|
302
|
+
return;
|
|
303
|
+
|
|
304
|
+
/* Update the message length. */
|
|
305
|
+
pms->count[1] += nbytes >> 29;
|
|
306
|
+
pms->count[0] += nbits;
|
|
307
|
+
if (pms->count[0] < nbits)
|
|
308
|
+
pms->count[1]++;
|
|
309
|
+
|
|
310
|
+
/* Process an initial partial block. */
|
|
311
|
+
if (offset) {
|
|
312
|
+
size_t copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
|
|
313
|
+
|
|
314
|
+
memcpy(pms->buffer + offset, p, copy);
|
|
315
|
+
if (offset + copy < 64)
|
|
316
|
+
return;
|
|
317
|
+
p += copy;
|
|
318
|
+
left -= copy;
|
|
319
|
+
md5_process(pms, pms->buffer);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/* Process full blocks. */
|
|
323
|
+
for (; left >= 64; p += 64, left -= 64)
|
|
324
|
+
md5_process(pms, p);
|
|
325
|
+
|
|
326
|
+
/* Process a final partial block. */
|
|
327
|
+
if (left)
|
|
328
|
+
memcpy(pms->buffer, p, left);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
void
|
|
332
|
+
rb_MD5_Finish(MD5_CTX *pms, uint8_t *digest)
|
|
333
|
+
{
|
|
334
|
+
static const uint8_t pad[64] = {
|
|
335
|
+
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
336
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
337
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
338
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
339
|
+
};
|
|
340
|
+
uint8_t data[8];
|
|
341
|
+
size_t i;
|
|
342
|
+
|
|
343
|
+
/* Save the length before padding. */
|
|
344
|
+
for (i = 0; i < 8; ++i)
|
|
345
|
+
data[i] = (uint8_t)(pms->count[i >> 2] >> ((i & 3) << 3));
|
|
346
|
+
/* Pad to 56 bytes mod 64. */
|
|
347
|
+
rb_MD5_Update(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
|
|
348
|
+
/* Append the length. */
|
|
349
|
+
rb_MD5_Update(pms, data, 8);
|
|
350
|
+
for (i = 0; i < 16; ++i)
|
|
351
|
+
digest[i] = (uint8_t)(pms->state[i >> 2] >> ((i & 3) << 3));
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/*
|
|
355
|
+
* most of this code is from the ruby internal ext/digest/md5/md5.c and md5init.c
|
|
356
|
+
*/
|
|
357
|
+
/* digest.h */
|
|
358
|
+
#define RUBY_DIGEST_API_VERSION 2
|
|
359
|
+
|
|
360
|
+
typedef void (*rb_digest_hash_init_func_t)(void *);
|
|
361
|
+
typedef void (*rb_digest_hash_update_func_t)(void *, unsigned char *, size_t);
|
|
362
|
+
typedef void (*rb_digest_hash_finish_func_t)(void *, unsigned char *);
|
|
363
|
+
|
|
364
|
+
typedef struct {
|
|
365
|
+
int api_version;
|
|
366
|
+
size_t digest_len;
|
|
367
|
+
size_t block_len;
|
|
368
|
+
size_t ctx_size;
|
|
369
|
+
rb_digest_hash_init_func_t init_func;
|
|
370
|
+
rb_digest_hash_update_func_t update_func;
|
|
371
|
+
rb_digest_hash_finish_func_t finish_func;
|
|
372
|
+
} rb_digest_metadata_t;
|
|
373
|
+
|
|
374
|
+
/* md5.c */
|
|
375
|
+
|
|
376
|
+
static const rb_digest_metadata_t md5 = {
|
|
377
|
+
RUBY_DIGEST_API_VERSION,
|
|
378
|
+
MD5_DIGEST_LENGTH,
|
|
379
|
+
MD5_BLOCK_LENGTH,
|
|
380
|
+
sizeof(MD5_CTX),
|
|
381
|
+
(rb_digest_hash_init_func_t)rb_MD5_Init,
|
|
382
|
+
(rb_digest_hash_update_func_t)rb_MD5_Update,
|
|
383
|
+
(rb_digest_hash_finish_func_t)rb_MD5_Finish,
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
/* save the current state to a rb_str
|
|
387
|
+
* serialize the MD5_CTX structure to a string
|
|
388
|
+
*/
|
|
389
|
+
VALUE rb_MD5_Save(VALUE self) {
|
|
390
|
+
MD5_CTX *ctx;
|
|
391
|
+
char output[sizeof(MD5_CTX)];
|
|
392
|
+
VALUE str;
|
|
393
|
+
|
|
394
|
+
Data_Get_Struct(self, MD5_CTX, ctx);
|
|
395
|
+
|
|
396
|
+
memcpy(output, ctx, sizeof(MD5_CTX));
|
|
397
|
+
|
|
398
|
+
return rb_str_new(output, sizeof(MD5_CTX));
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/* restore the current state from a rb_str */
|
|
402
|
+
VALUE rb_MD5_Restore(VALUE self, VALUE str) {
|
|
403
|
+
MD5_CTX *ctx;
|
|
404
|
+
|
|
405
|
+
Data_Get_Struct(self, MD5_CTX, ctx);
|
|
406
|
+
|
|
407
|
+
memcpy(ctx, RSTRING_PTR(str), sizeof(MD5_CTX));
|
|
408
|
+
|
|
409
|
+
return self;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/*
|
|
413
|
+
* A class for calculating message digests using the MD5
|
|
414
|
+
* Message-Digest Algorithm by RSA Data Security, Inc., described in
|
|
415
|
+
* RFC1321.
|
|
416
|
+
*/
|
|
417
|
+
void
|
|
418
|
+
Init_md5partial()
|
|
419
|
+
{
|
|
420
|
+
VALUE mDigest, cDigest_Base, cDigest_MD5;
|
|
421
|
+
|
|
422
|
+
rb_require("digest");
|
|
423
|
+
|
|
424
|
+
mDigest = rb_path2class("Digest");
|
|
425
|
+
cDigest_Base = rb_path2class("Digest::Base");
|
|
426
|
+
|
|
427
|
+
cDigest_MD5 = rb_define_class_under(mDigest, "MD5Partial", cDigest_Base);
|
|
428
|
+
rb_define_method(cDigest_MD5, "save", rb_MD5_Save, 0);
|
|
429
|
+
rb_define_method(cDigest_MD5, "restore", rb_MD5_Restore, 1);
|
|
430
|
+
|
|
431
|
+
rb_ivar_set(cDigest_MD5, rb_intern("metadata"),
|
|
432
|
+
Data_Wrap_Struct(rb_cObject, 0, 0, (void *)&md5));
|
|
433
|
+
}
|
data/ext/md5.h
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (C) 1999 Aladdin Enterprises. All rights reserved.
|
|
3
|
+
|
|
4
|
+
This software is provided 'as-is', without any express or implied
|
|
5
|
+
warranty. In no event will the authors be held liable for any damages
|
|
6
|
+
arising from the use of this software.
|
|
7
|
+
|
|
8
|
+
Permission is granted to anyone to use this software for any purpose,
|
|
9
|
+
including commercial applications, and to alter it and redistribute it
|
|
10
|
+
freely, subject to the following restrictions:
|
|
11
|
+
|
|
12
|
+
1. The origin of this software must not be misrepresented; you must not
|
|
13
|
+
claim that you wrote the original software. If you use this software
|
|
14
|
+
in a product, an acknowledgment in the product documentation would be
|
|
15
|
+
appreciated but is not required.
|
|
16
|
+
2. Altered source versions must be plainly marked as such, and must not be
|
|
17
|
+
misrepresented as being the original software.
|
|
18
|
+
3. This notice may not be removed or altered from any source distribution.
|
|
19
|
+
|
|
20
|
+
L. Peter Deutsch
|
|
21
|
+
ghost@aladdin.com
|
|
22
|
+
|
|
23
|
+
*/
|
|
24
|
+
/*
|
|
25
|
+
Independent implementation of MD5 (RFC 1321).
|
|
26
|
+
|
|
27
|
+
This code implements the MD5 Algorithm defined in RFC 1321.
|
|
28
|
+
It is derived directly from the text of the RFC and not from the
|
|
29
|
+
reference implementation.
|
|
30
|
+
|
|
31
|
+
The original and principal author of md5.h is L. Peter Deutsch
|
|
32
|
+
<ghost@aladdin.com>. Other authors are noted in the change history
|
|
33
|
+
that follows (in reverse chronological order):
|
|
34
|
+
|
|
35
|
+
1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
|
|
36
|
+
1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5);
|
|
37
|
+
added conditionalization for C++ compilation from Martin
|
|
38
|
+
Purschke <purschke@bnl.gov>.
|
|
39
|
+
1999-05-03 lpd Original version.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/* $OrigId: md5.h,v 1.2 2001/03/26 08:57:14 matz Exp $ */
|
|
43
|
+
/* $RoughId: md5.h,v 1.3 2002/02/24 08:14:31 knu Exp $ */
|
|
44
|
+
/* $Id: md5.h 11708 2007-02-12 23:01:19Z shyouhei $ */
|
|
45
|
+
|
|
46
|
+
#ifndef MD5_INCLUDED
|
|
47
|
+
#define MD5_INCLUDED
|
|
48
|
+
|
|
49
|
+
#include <ruby.h>
|
|
50
|
+
#include <stdint.h>
|
|
51
|
+
|
|
52
|
+
/*
|
|
53
|
+
* This code has some adaptations for the Ghostscript environment, but it
|
|
54
|
+
* will compile and run correctly in any environment with 8-bit chars and
|
|
55
|
+
* 32-bit ints. Specifically, it assumes that if the following are
|
|
56
|
+
* defined, they have the same meaning as in Ghostscript: P1, P2, P3.
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
/* Define the state of the MD5 Algorithm. */
|
|
60
|
+
typedef struct md5_state_s {
|
|
61
|
+
uint32_t count[2]; /* message length in bits, lsw first */
|
|
62
|
+
uint32_t state[4]; /* digest buffer */
|
|
63
|
+
uint8_t buffer[64]; /* accumulate block */
|
|
64
|
+
} MD5_CTX;
|
|
65
|
+
|
|
66
|
+
void rb_MD5_Init(MD5_CTX *pms);
|
|
67
|
+
void rb_MD5_Update(MD5_CTX *pms, const uint8_t *data, size_t nbytes);
|
|
68
|
+
void rb_MD5_Finish(MD5_CTX *pms, uint8_t *digest);
|
|
69
|
+
|
|
70
|
+
#define MD5_BLOCK_LENGTH 64
|
|
71
|
+
#define MD5_DIGEST_LENGTH 16
|
|
72
|
+
#define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1)
|
|
73
|
+
|
|
74
|
+
#endif /* MD5_INCLUDED */
|
data/test/test.rb
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'ext', 'md5partial')
|
|
2
|
+
require 'test/unit'
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class TestPartial < Test::Unit::TestCase
|
|
6
|
+
def test_chunked_partials
|
|
7
|
+
files = Dir['../ext/*.*'].each do|file|
|
|
8
|
+
puts "test #{file}"
|
|
9
|
+
# calculate a using a range of chunk sizes
|
|
10
|
+
100.times do|i|
|
|
11
|
+
buf_size = 10*(i+1)
|
|
12
|
+
hasher = Digest::MD5Partial.new
|
|
13
|
+
offset = 0
|
|
14
|
+
total = File.size(file)
|
|
15
|
+
|
|
16
|
+
until offset >= total do
|
|
17
|
+
buf = nil
|
|
18
|
+
File.open(file, 'rb') do|io|
|
|
19
|
+
io.seek(offset, IO::SEEK_SET)
|
|
20
|
+
buf = io.readpartial(buf_size)
|
|
21
|
+
hasher.update(buf)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# save the partial
|
|
25
|
+
File.open("partial", "wb") do|io|
|
|
26
|
+
str = hasher.save
|
|
27
|
+
io << str
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# restore the partial
|
|
31
|
+
hasher.restore(File.read("partial"))
|
|
32
|
+
|
|
33
|
+
# advance the offset
|
|
34
|
+
offset += buf.size
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
from_partial = hasher.hexdigest
|
|
38
|
+
directly = Digest::MD5.hexdigest(File.read(file))
|
|
39
|
+
assert_equal directly, from_partial
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
ensure
|
|
43
|
+
File.unlink("partial") if File.exist?("partial")
|
|
44
|
+
end
|
|
45
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: taf2-md5-partial
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Todd A. Fisher
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-06-28 00:00:00 -07:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description: Calculate an MD5 digest in parts, saving and restoring the calculation
|
|
17
|
+
email: todd.fisher@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions:
|
|
21
|
+
- ext/extconf.rb
|
|
22
|
+
extra_rdoc_files:
|
|
23
|
+
- LICENSE
|
|
24
|
+
- README
|
|
25
|
+
files:
|
|
26
|
+
- LICENSE
|
|
27
|
+
- README
|
|
28
|
+
- Rakefile
|
|
29
|
+
- ext/md5.h
|
|
30
|
+
- ext/md5.c
|
|
31
|
+
- ext/extconf.rb
|
|
32
|
+
has_rdoc: false
|
|
33
|
+
homepage: http://github.com/taf2/md5-partial/tree/master
|
|
34
|
+
post_install_message:
|
|
35
|
+
rdoc_options:
|
|
36
|
+
- --main
|
|
37
|
+
- README
|
|
38
|
+
require_paths:
|
|
39
|
+
- lib
|
|
40
|
+
- ext
|
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: "0"
|
|
46
|
+
version:
|
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: "0"
|
|
52
|
+
version:
|
|
53
|
+
requirements: []
|
|
54
|
+
|
|
55
|
+
rubyforge_project:
|
|
56
|
+
rubygems_version: 1.2.0
|
|
57
|
+
signing_key:
|
|
58
|
+
specification_version: 2
|
|
59
|
+
summary: Partial MD5 Digest with save/restore
|
|
60
|
+
test_files:
|
|
61
|
+
- test/test.rb
|