6 #if PERL_REVISION == 5 && PERL_VERSION < 8
8 #define SvPVbyte(sv, lp) \
9 (sv_utf8_downgrade((sv), 0), SvPV((sv), (lp)))
17 #define PerlIO_read(f, buf, count) fread(buf, 1, count, f)
20 #ifndef sv_derived_from
25 #define Newx(ptr, num, type) New(0, ptr, num, type)
26 #define Newxz(ptr, num, type) Newz(0, ptr, num, type)
32 {1,1,1,224,224,224,256,256,256,384,384,384,512,512,512,
33 512224,512224,512224,512256,512256,512256};
36 #define INT2PTR(p, i) (p) (i)
39 #define MAX_WRITE_SIZE 16384
40 #define IO_BUFFER_SIZE 4096
42 static SHA *getSHA(SV *self)
44 if (!sv_isobject(self) || !sv_derived_from(self, "Digest::SHA"))
46 return INT2PTR(SHA *, SvIV(SvRV(self)));
49 MODULE = Digest::SHA PACKAGE = Digest::SHA
63 shawrite(bitstr, bitcnt, s)
64 unsigned char * bitstr
69 newSHA(classname, alg)
76 if (!shainit(state, alg)) {
81 sv_setref_pv(RETVAL, classname, (void *) state);
82 SvREADONLY_on(SvRV(RETVAL));
93 if ((state = getSHA(self)) == NULL)
97 sv_setref_pv(RETVAL, sv_reftype(SvRV(self), 1), (void *) clone);
98 SvREADONLY_on(SvRV(RETVAL));
99 Copy(state, clone, 1, SHA);
112 Digest::SHA::sha1 = 0
113 Digest::SHA::sha1_hex = 1
114 Digest::SHA::sha1_base64 = 2
115 Digest::SHA::sha224 = 3
116 Digest::SHA::sha224_hex = 4
117 Digest::SHA::sha224_base64 = 5
118 Digest::SHA::sha256 = 6
119 Digest::SHA::sha256_hex = 7
120 Digest::SHA::sha256_base64 = 8
121 Digest::SHA::sha384 = 9
122 Digest::SHA::sha384_hex = 10
123 Digest::SHA::sha384_base64 = 11
124 Digest::SHA::sha512 = 12
125 Digest::SHA::sha512_hex = 13
126 Digest::SHA::sha512_base64 = 14
127 Digest::SHA::sha512224 = 15
128 Digest::SHA::sha512224_hex = 16
129 Digest::SHA::sha512224_base64 = 17
130 Digest::SHA::sha512256 = 18
131 Digest::SHA::sha512256_hex = 19
132 Digest::SHA::sha512256_base64 = 20
140 if (!shainit(&sha, ix2alg[ix]))
142 for (i = 0; i < items; i++) {
143 data = (UCHR *) (SvPVbyte(ST(i), len));
144 while (len > MAX_WRITE_SIZE) {
145 shawrite(data, MAX_WRITE_SIZE << 3, &sha);
146 data += MAX_WRITE_SIZE;
147 len -= MAX_WRITE_SIZE;
149 shawrite(data, len << 3, &sha);
154 result = (char *) shadigest(&sha);
157 else if (ix % 3 == 1)
158 result = shahex(&sha);
160 result = shabase64(&sha);
161 RETVAL = newSVpv(result, len);
168 Digest::SHA::hmac_sha1 = 0
169 Digest::SHA::hmac_sha1_hex = 1
170 Digest::SHA::hmac_sha1_base64 = 2
171 Digest::SHA::hmac_sha224 = 3
172 Digest::SHA::hmac_sha224_hex = 4
173 Digest::SHA::hmac_sha224_base64 = 5
174 Digest::SHA::hmac_sha256 = 6
175 Digest::SHA::hmac_sha256_hex = 7
176 Digest::SHA::hmac_sha256_base64 = 8
177 Digest::SHA::hmac_sha384 = 9
178 Digest::SHA::hmac_sha384_hex = 10
179 Digest::SHA::hmac_sha384_base64 = 11
180 Digest::SHA::hmac_sha512 = 12
181 Digest::SHA::hmac_sha512_hex = 13
182 Digest::SHA::hmac_sha512_base64 = 14
183 Digest::SHA::hmac_sha512224 = 15
184 Digest::SHA::hmac_sha512224_hex = 16
185 Digest::SHA::hmac_sha512224_base64 = 17
186 Digest::SHA::hmac_sha512256 = 18
187 Digest::SHA::hmac_sha512256_hex = 19
188 Digest::SHA::hmac_sha512256_base64 = 20
191 UCHR *key = (UCHR *) "";
198 key = (UCHR *) (SvPVbyte(ST(items-1), len));
200 if (hmacinit(&hmac, ix2alg[ix], key, len) == NULL)
202 for (i = 0; i < items - 1; i++) {
203 data = (UCHR *) (SvPVbyte(ST(i), len));
204 while (len > MAX_WRITE_SIZE) {
205 hmacwrite(data, MAX_WRITE_SIZE << 3, &hmac);
206 data += MAX_WRITE_SIZE;
207 len -= MAX_WRITE_SIZE;
209 hmacwrite(data, len << 3, &hmac);
214 result = (char *) hmacdigest(&hmac);
215 len = hmac.digestlen;
217 else if (ix % 3 == 1)
218 result = hmachex(&hmac);
220 result = hmacbase64(&hmac);
221 RETVAL = newSVpv(result, len);
229 Digest::SHA::hashsize = 0
230 Digest::SHA::algorithm = 1
234 if ((state = getSHA(self)) == NULL)
236 RETVAL = ix ? state->alg : state->digestlen << 3;
249 if ((state = getSHA(self)) == NULL)
251 for (i = 1; i < items; i++) {
252 data = (UCHR *) (SvPVbyte(ST(i), len));
253 while (len > MAX_WRITE_SIZE) {
254 shawrite(data, MAX_WRITE_SIZE << 3, state);
255 data += MAX_WRITE_SIZE;
256 len -= MAX_WRITE_SIZE;
258 shawrite(data, len << 3, state);
266 Digest::SHA::digest = 0
267 Digest::SHA::hexdigest = 1
268 Digest::SHA::b64digest = 2
274 if ((state = getSHA(self)) == NULL)
279 result = (char *) shadigest(state);
280 len = state->digestlen;
283 result = shahex(state);
285 result = shabase64(state);
286 RETVAL = newSVpv(result, len);
299 if ((state = getSHA(self)) == NULL)
301 Copy(digcpy(state), ptr, state->alg <= SHA256 ? 32 : 64, UCHR);
302 ptr += state->alg <= SHA256 ? 32 : 64;
303 Copy(state->block, ptr, state->alg <= SHA256 ? 64 : 128, UCHR);
304 ptr += state->alg <= SHA256 ? 64 : 128;
305 ptr = w32mem(ptr, state->blockcnt);
306 ptr = w32mem(ptr, state->lenhh);
307 ptr = w32mem(ptr, state->lenhl);
308 ptr = w32mem(ptr, state->lenlh);
309 ptr = w32mem(ptr, state->lenll);
310 RETVAL = newSVpv((char *) buf, ptr - buf);
315 _putstate(self, packed_state)
324 if ((state = getSHA(self)) == NULL)
326 data = (UCHR *) SvPV(packed_state, len);
327 if (len != (state->alg <= SHA256 ? 116 : 212))
329 data = statecpy(state, data);
330 Copy(data, state->block, state->blocksize >> 3, UCHR);
331 data += (state->blocksize >> 3);
332 bc = memw32(data), data += 4;
333 if (bc >= (state->alg <= SHA256 ? 512U : 1024U))
335 state->blockcnt = bc;
336 state->lenhh = memw32(data), data += 4;
337 state->lenhl = memw32(data), data += 4;
338 state->lenlh = memw32(data), data += 4;
339 state->lenll = memw32(data);
349 UCHR in[IO_BUFFER_SIZE];
351 if (!f || (state = getSHA(self)) == NULL)
353 while ((n = PerlIO_read(f, in, sizeof(in))) > 0)
354 shawrite(in, n << 3, state);
358 _addfileuniv(self, f)
366 UCHR in[IO_BUFFER_SIZE+1];
369 if (!f || (state = getSHA(self)) == NULL)
371 while ((n = PerlIO_read(f, in+1, IO_BUFFER_SIZE)) > 0) {
372 for (dst = in, src = in + 1; n; n--) {
383 else if (c == '\012') {
394 shawrite(in, (dst - in) << 3, state);
398 shawrite(in, 1 << 3, state);