line |
stmt |
bran |
cond |
sub |
time |
code |
1
|
|
|
|
|
|
#include "EXTERN.h" |
2
|
|
|
|
|
|
#include "perl.h" |
3
|
|
|
|
|
|
#include "XSUB.h" |
4
|
|
|
|
|
|
|
5
|
|
|
|
|
|
#ifdef SvPVbyte |
6
|
|
|
|
|
|
#if PERL_REVISION == 5 && PERL_VERSION < 8 |
7
|
|
|
|
|
|
#undef SvPVbyte |
8
|
|
|
|
|
|
#define SvPVbyte(sv, lp) \ |
9
|
|
|
|
|
|
(sv_utf8_downgrade((sv), 0), SvPV((sv), (lp))) |
10
|
|
|
|
|
|
#endif |
11
|
|
|
|
|
|
#else |
12
|
|
|
|
|
|
#define SvPVbyte SvPV |
13
|
|
|
|
|
|
#endif |
14
|
|
|
|
|
|
|
15
|
|
|
|
|
|
#include "src/sha.c" |
16
|
|
|
|
|
|
|
17
|
|
|
|
|
|
static int ix2alg[] = |
18
|
|
|
|
|
|
{1,1,1,224,224,224,256,256,256,384,384,384,512,512,512, |
19
|
|
|
|
|
|
512224,512224,512224,512256,512256,512256}; |
20
|
|
|
|
|
|
|
21
|
|
|
|
|
|
MODULE = Digest::SHA PACKAGE = Digest::SHA |
22
|
|
|
|
|
|
|
23
|
|
|
|
|
|
PROTOTYPES: ENABLE |
24
|
|
|
|
|
|
|
25
|
|
|
|
|
|
#ifndef INT2PTR |
26
|
|
|
|
|
|
#define INT2PTR(p, i) (p) (i) |
27
|
|
|
|
|
|
#endif |
28
|
|
|
|
|
|
|
29
|
|
|
|
|
|
#define MAX_WRITE_SIZE 16384 |
30
|
|
|
|
|
|
|
31
|
|
|
|
|
|
int |
32
|
|
|
|
|
|
shaclose(s) |
33
|
|
|
|
|
|
SHA * s |
34
|
|
|
|
|
|
CODE: |
35
|
70
|
|
|
|
|
RETVAL = shaclose(s); |
36
|
70
|
|
|
|
|
sv_setiv(SvRV(ST(0)), 0); |
37
|
|
|
|
|
|
|
38
|
|
|
|
|
|
int |
39
|
|
|
|
|
|
shadump(file, s) |
40
|
|
|
|
|
|
char * file |
41
|
|
|
|
|
|
SHA * s |
42
|
|
|
|
|
|
|
43
|
|
|
|
|
|
SHA * |
44
|
|
|
|
|
|
shadup(s) |
45
|
|
|
|
|
|
SHA * s |
46
|
|
|
|
|
|
|
47
|
|
|
|
|
|
SHA * |
48
|
|
|
|
|
|
shaload(file) |
49
|
|
|
|
|
|
char * file |
50
|
|
|
|
|
|
|
51
|
|
|
|
|
|
SHA * |
52
|
|
|
|
|
|
shaopen(alg) |
53
|
|
|
|
|
|
int alg |
54
|
|
|
|
|
|
|
55
|
|
|
|
|
|
void |
56
|
|
|
|
|
|
sharewind(s) |
57
|
|
|
|
|
|
SHA * s |
58
|
|
|
|
|
|
|
59
|
|
|
|
|
|
unsigned long |
60
|
|
|
|
|
|
shawrite(bitstr, bitcnt, s) |
61
|
|
|
|
|
|
unsigned char * bitstr |
62
|
|
|
|
|
|
unsigned long bitcnt |
63
|
|
|
|
|
|
SHA * s |
64
|
|
|
|
|
|
|
65
|
|
|
|
|
|
void |
66
|
|
|
|
|
|
sha1(...) |
67
|
|
|
|
|
|
ALIAS: |
68
|
|
|
|
|
|
Digest::SHA::sha1 = 0 |
69
|
|
|
|
|
|
Digest::SHA::sha1_hex = 1 |
70
|
|
|
|
|
|
Digest::SHA::sha1_base64 = 2 |
71
|
|
|
|
|
|
Digest::SHA::sha224 = 3 |
72
|
|
|
|
|
|
Digest::SHA::sha224_hex = 4 |
73
|
|
|
|
|
|
Digest::SHA::sha224_base64 = 5 |
74
|
|
|
|
|
|
Digest::SHA::sha256 = 6 |
75
|
|
|
|
|
|
Digest::SHA::sha256_hex = 7 |
76
|
|
|
|
|
|
Digest::SHA::sha256_base64 = 8 |
77
|
|
|
|
|
|
Digest::SHA::sha384 = 9 |
78
|
|
|
|
|
|
Digest::SHA::sha384_hex = 10 |
79
|
|
|
|
|
|
Digest::SHA::sha384_base64 = 11 |
80
|
|
|
|
|
|
Digest::SHA::sha512 = 12 |
81
|
|
|
|
|
|
Digest::SHA::sha512_hex = 13 |
82
|
|
|
|
|
|
Digest::SHA::sha512_base64 = 14 |
83
|
|
|
|
|
|
Digest::SHA::sha512224 = 15 |
84
|
|
|
|
|
|
Digest::SHA::sha512224_hex = 16 |
85
|
|
|
|
|
|
Digest::SHA::sha512224_base64 = 17 |
86
|
|
|
|
|
|
Digest::SHA::sha512256 = 18 |
87
|
|
|
|
|
|
Digest::SHA::sha512256_hex = 19 |
88
|
|
|
|
|
|
Digest::SHA::sha512256_base64 = 20 |
89
|
|
|
|
|
|
PREINIT: |
90
|
|
|
|
|
|
int i; |
91
|
|
|
|
|
|
unsigned char *data; |
92
|
|
|
|
|
|
STRLEN len; |
93
|
|
|
|
|
|
SHA *state; |
94
|
|
|
|
|
|
char *result; |
95
|
|
|
|
|
|
PPCODE: |
96
|
20
|
|
|
|
|
if ((state = shaopen(ix2alg[ix])) == NULL) |
97
|
0
|
|
|
|
|
XSRETURN_UNDEF; |
98
|
20
|
|
|
|
|
for (i = 0; i < items; i++) { |
99
|
20
|
|
|
|
|
data = (unsigned char *) (SvPVbyte(ST(i), len)); |
100
|
70
|
|
|
|
|
while (len > MAX_WRITE_SIZE) { |
101
|
30
|
|
|
|
|
shawrite(data, MAX_WRITE_SIZE << 3, state); |
102
|
30
|
|
|
|
|
data += MAX_WRITE_SIZE; |
103
|
30
|
|
|
|
|
len -= MAX_WRITE_SIZE; |
104
|
|
|
|
|
|
} |
105
|
20
|
|
|
|
|
shawrite(data, len << 3, state); |
106
|
|
|
|
|
|
} |
107
|
20
|
|
|
|
|
shafinish(state); |
108
|
20
|
|
|
|
|
len = 0; |
109
|
20
|
|
|
|
|
if (ix % 3 == 0) { |
110
|
0
|
|
|
|
|
result = (char *) shadigest(state); |
111
|
0
|
|
|
|
|
len = shadsize(state); |
112
|
|
|
|
|
|
} |
113
|
20
|
|
|
|
|
else if (ix % 3 == 1) |
114
|
20
|
|
|
|
|
result = shahex(state); |
115
|
|
|
|
|
|
else |
116
|
0
|
|
|
|
|
result = shabase64(state); |
117
|
20
|
|
|
|
|
ST(0) = sv_2mortal(newSVpv(result, len)); |
118
|
20
|
|
|
|
|
shaclose(state); |
119
|
20
|
|
|
|
|
XSRETURN(1); |
120
|
|
|
|
|
|
|
121
|
|
|
|
|
|
void |
122
|
|
|
|
|
|
hmac_sha1(...) |
123
|
|
|
|
|
|
ALIAS: |
124
|
|
|
|
|
|
Digest::SHA::hmac_sha1 = 0 |
125
|
|
|
|
|
|
Digest::SHA::hmac_sha1_hex = 1 |
126
|
|
|
|
|
|
Digest::SHA::hmac_sha1_base64 = 2 |
127
|
|
|
|
|
|
Digest::SHA::hmac_sha224 = 3 |
128
|
|
|
|
|
|
Digest::SHA::hmac_sha224_hex = 4 |
129
|
|
|
|
|
|
Digest::SHA::hmac_sha224_base64 = 5 |
130
|
|
|
|
|
|
Digest::SHA::hmac_sha256 = 6 |
131
|
|
|
|
|
|
Digest::SHA::hmac_sha256_hex = 7 |
132
|
|
|
|
|
|
Digest::SHA::hmac_sha256_base64 = 8 |
133
|
|
|
|
|
|
Digest::SHA::hmac_sha384 = 9 |
134
|
|
|
|
|
|
Digest::SHA::hmac_sha384_hex = 10 |
135
|
|
|
|
|
|
Digest::SHA::hmac_sha384_base64 = 11 |
136
|
|
|
|
|
|
Digest::SHA::hmac_sha512 = 12 |
137
|
|
|
|
|
|
Digest::SHA::hmac_sha512_hex = 13 |
138
|
|
|
|
|
|
Digest::SHA::hmac_sha512_base64 = 14 |
139
|
|
|
|
|
|
Digest::SHA::hmac_sha512224 = 15 |
140
|
|
|
|
|
|
Digest::SHA::hmac_sha512224_hex = 16 |
141
|
|
|
|
|
|
Digest::SHA::hmac_sha512224_base64 = 17 |
142
|
|
|
|
|
|
Digest::SHA::hmac_sha512256 = 18 |
143
|
|
|
|
|
|
Digest::SHA::hmac_sha512256_hex = 19 |
144
|
|
|
|
|
|
Digest::SHA::hmac_sha512256_base64 = 20 |
145
|
|
|
|
|
|
PREINIT: |
146
|
|
|
|
|
|
int i; |
147
|
|
|
|
|
|
unsigned char *key; |
148
|
|
|
|
|
|
unsigned char *data; |
149
|
|
|
|
|
|
STRLEN len; |
150
|
|
|
|
|
|
HMAC *state; |
151
|
|
|
|
|
|
char *result; |
152
|
|
|
|
|
|
PPCODE: |
153
|
0
|
|
|
|
|
key = (unsigned char *) (SvPVbyte(ST(items-1), len)); |
154
|
0
|
|
|
|
|
if ((state = hmacopen(ix2alg[ix], key, len)) == NULL) |
155
|
0
|
|
|
|
|
XSRETURN_UNDEF; |
156
|
0
|
|
|
|
|
for (i = 0; i < items - 1; i++) { |
157
|
0
|
|
|
|
|
data = (unsigned char *) (SvPVbyte(ST(i), len)); |
158
|
0
|
|
|
|
|
while (len > MAX_WRITE_SIZE) { |
159
|
|
|
|
|
|
hmacwrite(data, MAX_WRITE_SIZE << 3, state); |
160
|
0
|
|
|
|
|
data += MAX_WRITE_SIZE; |
161
|
0
|
|
|
|
|
len -= MAX_WRITE_SIZE; |
162
|
|
|
|
|
|
} |
163
|
0
|
|
|
|
|
hmacwrite(data, len << 3, state); |
164
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
hmacfinish(state); |
166
|
0
|
|
|
|
|
len = 0; |
167
|
0
|
|
|
|
|
if (ix % 3 == 0) { |
168
|
0
|
|
|
|
|
result = (char *) hmacdigest(state); |
169
|
0
|
|
|
|
|
len = shadsize(state->osha); |
170
|
|
|
|
|
|
} |
171
|
0
|
|
|
|
|
else if (ix % 3 == 1) |
172
|
|
|
|
|
|
result = hmachex(state); |
173
|
|
|
|
|
|
else |
174
|
|
|
|
|
|
result = hmacbase64(state); |
175
|
0
|
|
|
|
|
ST(0) = sv_2mortal(newSVpv(result, len)); |
176
|
|
|
|
|
|
hmacclose(state); |
177
|
0
|
|
|
|
|
XSRETURN(1); |
178
|
|
|
|
|
|
|
179
|
|
|
|
|
|
void |
180
|
|
|
|
|
|
hashsize(self) |
181
|
|
|
|
|
|
SV * self |
182
|
|
|
|
|
|
ALIAS: |
183
|
|
|
|
|
|
Digest::SHA::hashsize = 0 |
184
|
|
|
|
|
|
Digest::SHA::algorithm = 1 |
185
|
|
|
|
|
|
PREINIT: |
186
|
|
|
|
|
|
SHA *state; |
187
|
|
|
|
|
|
int result; |
188
|
|
|
|
|
|
PPCODE: |
189
|
0
|
|
|
|
|
state = INT2PTR(SHA *, SvIV(SvRV(SvRV(self)))); |
190
|
0
|
|
|
|
|
result = ix ? shaalg(state) : shadsize(state) << 3; |
191
|
0
|
|
|
|
|
ST(0) = sv_2mortal(newSViv(result)); |
192
|
0
|
|
|
|
|
XSRETURN(1); |
193
|
|
|
|
|
|
|
194
|
|
|
|
|
|
void |
195
|
|
|
|
|
|
add(self, ...) |
196
|
|
|
|
|
|
SV * self |
197
|
|
|
|
|
|
PREINIT: |
198
|
|
|
|
|
|
int i; |
199
|
|
|
|
|
|
unsigned char *data; |
200
|
|
|
|
|
|
STRLEN len; |
201
|
|
|
|
|
|
SHA *state; |
202
|
|
|
|
|
|
PPCODE: |
203
|
520
|
|
|
|
|
state = INT2PTR(SHA *, SvIV(SvRV(SvRV(self)))); |
204
|
1040
|
|
|
|
|
for (i = 1; i < items; i++) { |
205
|
520
|
|
|
|
|
data = (unsigned char *) (SvPVbyte(ST(i), len)); |
206
|
1040
|
|
|
|
|
while (len > MAX_WRITE_SIZE) { |
207
|
0
|
|
|
|
|
shawrite(data, MAX_WRITE_SIZE << 3, state); |
208
|
0
|
|
|
|
|
data += MAX_WRITE_SIZE; |
209
|
0
|
|
|
|
|
len -= MAX_WRITE_SIZE; |
210
|
|
|
|
|
|
} |
211
|
520
|
|
|
|
|
shawrite(data, len << 3, state); |
212
|
|
|
|
|
|
} |
213
|
520
|
|
|
|
|
XSRETURN(1); |
214
|
|
|
|
|
|
|
215
|
|
|
|
|
|
void |
216
|
|
|
|
|
|
digest(self) |
217
|
|
|
|
|
|
SV * self |
218
|
|
|
|
|
|
ALIAS: |
219
|
|
|
|
|
|
Digest::SHA::digest = 0 |
220
|
|
|
|
|
|
Digest::SHA::Hexdigest = 1 |
221
|
|
|
|
|
|
Digest::SHA::B64digest = 2 |
222
|
|
|
|
|
|
PREINIT: |
223
|
|
|
|
|
|
STRLEN len; |
224
|
|
|
|
|
|
SHA *state; |
225
|
|
|
|
|
|
char *result; |
226
|
|
|
|
|
|
PPCODE: |
227
|
70
|
|
|
|
|
state = INT2PTR(SHA *, SvIV(SvRV(SvRV(self)))); |
228
|
70
|
|
|
|
|
shafinish(state); |
229
|
|
|
|
|
|
len = 0; |
230
|
70
|
|
|
|
|
if (ix == 0) { |
231
|
0
|
|
|
|
|
result = (char *) shadigest(state); |
232
|
0
|
|
|
|
|
len = shadsize(state); |
233
|
|
|
|
|
|
} |
234
|
70
|
|
|
|
|
else if (ix == 1) |
235
|
70
|
|
|
|
|
result = shahex(state); |
236
|
|
|
|
|
|
else |
237
|
0
|
|
|
|
|
result = shabase64(state); |
238
|
70
|
|
|
|
|
ST(0) = sv_2mortal(newSVpv(result, len)); |
239
|
70
|
|
|
|
|
sharewind(state); |
240
|
70
|
|
|
|
|
XSRETURN(1); |