-
Notifications
You must be signed in to change notification settings - Fork 4
/
md.t
213 lines (184 loc) · 8.8 KB
/
md.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use v6;
BEGIN { @*INC.unshift: './lib'; }
use Test;
plan 59;
use Sum::MD;
ok 1,'We use Sum::MD and we are still alive';
# fmt('%x') seems to have its limitations at present.
sub hexify ($i is copy) {
join('',reverse (gather while $i { take ($i +& 0xffffffffffffffff).fmt('%x'); $i +>= 64; }));
}
class MD4t does Sum::MD4 does Sum::Marshal::Raw { };
my MD4t $s .= new();
ok $s.WHAT === MD4t, 'We create a MD4 class and object';
given (MD4t.new()) {
is .size, 128, "MD4.size is correct";
is .finalize(Buf.new()),
0x31d6cfe0d16ae931b73c59d7e0c089c0,
"MD4 of an empty buffer is correct.";
is .Buf.values, (0x31,0xd6,0xcf,0xe0,0xd1,0x6a,0xe9,0x31,0xb7,0x3c,0x59,0xd7,0xe0,0xc0,0x89,0xc0), "MD4 Buf method works";
}
is MD4t.new().finalize(Buf.new(97)),
0xbde52cb31de33e46245e05fbdbd6fb24,
"MD4 of a 1-byte buffer is correct.";
is MD4t.new().finalize(Buf.new(97 xx 55)),
0xc889c81dd86c4d2e025778944ea02881,
"MD4 of a 55-byte buffer is correct.";
is MD4t.new().finalize(Buf.new(97 xx 56)),
0xd5f9a9e9257077a5f08b0b92f348b0ad,
"MD4 of a 56-byte buffer is correct.";
is MD4t.new().finalize(Buf.new(97 xx 64)),
0x52f5076fabd22680234a3fa9f9dc5732,
"MD4 of a 64-byte buffer is correct.";
is MD4t.new().finalize(Buf.new(),True),
0x15f8f7419944ac564526a3c65da2c5f3,
"MD4 of a 1-bit buffer is correct.";
is MD4t.new().finalize(Buf.new(),True,False,True,False,True,False,False),
0x81d0ea938ae2ad56f51c1228b5d26d47,
"MD4 of a 7-bit buffer is correct.";
is MD4t.new().finalize(Buf.new(97),True),
0x6b9ffb14ef505495fa6323ffcac69967,
"MD4 of a 9-bit buffer is correct.";
is MD4t.new().finalize(Buf.new(97 xx 55),True,False,True,False,True,False,False),
0xbde1a4654c6b80bff32ee4a0fde4d5d5,
"MD4 of a 447-bit buffer is correct.";
is MD4t.new().finalize(Buf.new(97 xx 56),True),
0x70489fa63a69a637c09c4999e994877c,
"MD4 of a 449-bit buffer is correct.";
is MD4t.new().finalize(Buf.new(97 xx 63),True,False,True,False,True,False,False),
0x0719b5d879deafc63150bc5aa45ba714,
"MD4 of a 511-bit buffer is correct.";
class MD4dwim does Sum::MD4 does Sum::Marshal::Block[] { }
my MD4dwim $dwim .= new();
ok $dwim.WHAT === MD4dwim, 'We create a dwimmy Block MD4 class and object';
is $dwim.finalize(('0123456789' x 100).ords), 0x895ffd5f1acfe6f760c777e7883605e9, "MD4 of 1000 Ints is correct";
$dwim .= new();
is $dwim.finalize(Buf.new((0x30..0x39) xx 100)), 0x895ffd5f1acfe6f760c777e7883605e9, "MD4 of 8000-bit Buf is correct";
class MD4ext does Sum::MD4ext[] does Sum::Marshal::Raw { };
is MD4ext.size, 256, "extended MD4 .size is correct. And a class method.";
todo "Need extended MD4 test vector", 1;
is hexify(MD4ext.new().finalize(Buf.new(97 xx 55))),
0x0,
"Extended MD4 works.";
class MD5t does Sum::MD5[] does Sum::Marshal::Raw { };
my MD5t $md5 .= new();
ok $md5.WHAT === MD5t, 'We create an MD5 class and object';
given (MD5t.new()) {
is .size, 128, "MD5.size is correct";
is .finalize(Buf.new()),
0xd41d8cd98f00b204e9800998ecf8427e,
"MD5 of an empty buffer is correct.";
is .Buf.values.fmt("%x"), "d4 1d 8c d9 8f 0 b2 4 e9 80 9 98 ec f8 42 7e", "MD5 Buf method works";
}
# Since it uses the same buffering code as MD4 we don't need to test
# different lengths thoroughly, but a few more test vectors would be good.
is MD5t.new().finalize(Buf.new(97 xx 64)),
0x014842d480b571495a4a0363793f7367,
"MD5 is correct (test vector 1).";
is MD5t.new().finalize(Buf.new(97 xx 64), Buf.new(97 xx 64), Buf.new(97 xx 56)),
0x63642b027ee89938c922722650f2eb9b,
"MD5 is correct (test vector 2).";
class r160t does Sum::RIPEMD160[] does Sum::Marshal::Raw { };
my r160t $r160 .= new();
ok $r160.WHAT === r160t, 'We create a RIPEMD-160 class and object';
given (r160t.new()) {
is .size, 160, "RIPEMD-160.size is correct";
is .finalize(Buf.new()),
0x9c1185a5c5e9fc54612808977ee8f548b2258d31,
"RIPEMD-160 of an empty buffer is correct.";
is .Buf.values.fmt("%x"), "9c 11 85 a5 c5 e9 fc 54 61 28 8 97 7e e8 f5 48 b2 25 8d 31", "MD5 Buf method works";
}
# Since it uses the same buffering code as MD4 we don't need to test
# different lengths thoroughly, but a few more test vectors would be good.
is r160t.new().finalize(Buf.new(97 xx 64)),
0x9dfb7d374ad924f3f88de96291c33e9abed53e32,
"RIPEMD-160 is correct (test vector 1).";
is r160t.new().finalize(Buf.new(97 xx 64), Buf.new(97 xx 64), Buf.new(97 xx 56)),
0x52a7ad26b98c60e2f14e0863c1b58de525888b11,
"RIPEMD-160 is correct (test vector 2).";
class r128t does Sum::RIPEMD128[] does Sum::Marshal::Raw { };
my r128t $r128 .= new();
ok $r128.WHAT === r128t, 'We create a RIPEMD-128 class and object';
given (r128t.new()) {
is .size, 128, "RIPEMD-128.size is correct";
is .finalize(Buf.new()),
0xcdf26213a150dc3ecb610f18f6b38b46,
"RIPEMD-128 of an empty buffer is correct.";
is .Buf.values.fmt("%x"), "cd f2 62 13 a1 50 dc 3e cb 61 f 18 f6 b3 8b 46", "RIPEMD-128 Buf method works";
}
# Since it uses the same buffering code as MD4 we don't need to test
# different lengths thoroughly, but a few more test vectors would be good.
is r128t.new().finalize(Buf.new(97 xx 64)),
0x680716ac638f0d601982c696d37e5e56,
"RIPEMD-128 is correct (test vector 1).";
is r128t.new().finalize(Buf.new(97 xx 64), Buf.new(97 xx 64), Buf.new(97 xx 56)),
0x481285089b4b03da9eeffc2721680354,
"RIPEMD-128 is correct (test vector 2).";
class r320t does Sum::RIPEMD320[] does Sum::Marshal::Raw { };
my r320t $r320 .= new();
ok $r320.WHAT === r320t, 'We create a RIPEMD-320 class and object';
given (r320t.new()) {
is .size, 320, "RIPEMD-320.size is correct";
is .finalize(Buf.new()),
0x22d65d5661536cdc75c1fdf5c6de7b41b9f27325ebc61e8557177d705a0ec880151c3a32a00899b8,
"RIPEMD-320 of an empty buffer is correct.";
is .Buf.values.fmt("%x"), "22 d6 5d 56 61 53 6c dc 75 c1 fd f5 c6 de 7b 41 b9 f2 73 25 eb c6 1e 85 57 17 7d 70 5a e c8 80 15 1c 3a 32 a0 8 99 b8", "RIPEMD-320 Buf method works";
}
# Since it uses the same buffering code as MD4 we don't need to test
# different lengths thoroughly, but a few more test vectors would be good.
is r320t.new().finalize(Buf.new(97 xx 64)),
0x6e815badcf69d2978caf8b8bbaba941239f9847d1ff140062484cb57a0745bccf21c427705fdd30d,
"RIPEMD-320 is correct (test vector 1).";
is r320t.new().finalize(Buf.new(97 xx 64), Buf.new(97 xx 64), Buf.new(97 xx 56)),
0x82c5b5ffb960376afb6e21b33bd2367197080dd9724f7e2947e1075347462e603649bca32ad1b824,
"RIPEMD-320 is correct (test vector 2).";
class r256t does Sum::RIPEMD256[] does Sum::Marshal::Raw { };
my r256t $r256 .= new();
ok $r256.WHAT === r256t, 'We create a RIPEMD-256 class and object';
given (r256t.new()) {
is .size, 256, "RIPEMD-256.size is correct";
is .finalize(Buf.new()),
0x02ba4c4e5f8ecd1877fc52d64d30e37a2d9774fb1e5d026380ae0168e3c5522d,
"RIPEMD-256 of an empty buffer is correct.";
is .Buf.values.fmt("%x"), "2 ba 4c 4e 5f 8e cd 18 77 fc 52 d6 4d 30 e3 7a 2d 97 74 fb 1e 5d 2 63 80 ae 1 68 e3 c5 52 2d", "RIPEMD-256 Buf method works";
}
# Since it uses the same buffering code as MD4 we don't need to test
# different lengths thoroughly, but a few more test vectors would be good.
is r256t.new().finalize(Buf.new(97 xx 64)),
0x8147678472c129cabb59f57f637c622ccd5707af80a583303e6dde7d0800ced6,
"RIPEMD-256 is correct (test vector 1).";
is r256t.new().finalize(Buf.new(97 xx 64), Buf.new(97 xx 64), Buf.new(97 xx 56)),
0x8e7bc719ca3cdbb9411e43f18955a1f305e7643a0ae20a7a01823e80090fcf37,
"RIPEMD-256 is correct (test vector 2).";
class MD2t does Sum::MD2 does Sum::Marshal::Raw { };
my MD2t $s2 .= new();
ok $s2.WHAT === MD2t, 'We create a MD2 class and object';
given (MD2t.new()) {
is .size, 128, "MD2.size is correct";
is .finalize(Buf.new()),
0x8350e5a3e24c153df2275c9f80692773,
"MD2 of an empty buffer is correct.";
is .Buf.values, (0x83, 0x50, 0xe5, 0xa3, 0xe2, 0x4c, 0x15, 0x3d, 0xf2, 0x27, 0x5c, 0x9f, 0x80, 0x69, 0x27, 0x73), "MD2 Buf method works";
}
is MD2t.new().finalize(Buf.new(97)),
0x32ec01ec4a6dac72c0ab96fb34c0b5d1,
"MD2 of a 1-byte buffer is correct.";
is MD2t.new().finalize(Buf.new(97 xx 15)),
0xa1379a1027d0d29af98200799b8d5d8e,
"MD2 of a 15-byte buffer is correct.";
is MD2t.new().finalize(Buf.new(97 xx 16)),
0xb437ae50feb09a37c16b4c605cd642da,
"MD2 of a 16-byte buffer is correct.";
is MD2t.new().finalize(Buf.new(97 xx 16), Buf.new(97)),
0xdbf15a5fdfd6f7e9ece27d5e310c58ed,
"MD2 of a 17-byte buffer is correct.";
# Now grab the code in the synopsis from the POD and make sure it runs.
# This is currently complete hackery but might improve when pod support does.
# And also an outputs_ok Test.pm function that redirects $*OUT might be nice.
class sayer {
has $.accum is rw = "";
method print (*@s) { $.accum ~= [~] @s }
}
my sayer $p .= new();
{ temp $*OUT = $p; eval $Sum::MD::Doc::synopsis; }
is $p.accum, $Sum::MD::Doc::synopsis.comb(/<.after \#\s> (<.ws> <.xdigit>+)+/).join("\n") ~ "\n", 'Code in manpage synopsis actually works';