1 #define PERL_NO_GET_CONTEXT
7 #if PERL_REVISION == 5 && PERL_VERSION < 8
9 #define SvPVbyte(sv, lp) \
10 (sv_utf8_downgrade((sv), 0), SvPV((sv), (lp)))
23 #define PerlIO_read(f, buf, count) fread(buf, 1, count, f)
26 #ifndef sv_derived_from
31 #define Newx(ptr, num, type) New(0, ptr, num, type)
32 #define Newxz(ptr, num, type) Newz(0, ptr, num, type)
37 static const int ix2alg[] =
38 {1,1,1,224,224,224,256,256,256,384,384,384,512,512,512,
39 512224,512224,512224,512256,512256,512256};
42 #define INT2PTR(p, i) (p) (i)
45 #define MAX_WRITE_SIZE 16384
46 #define IO_BUFFER_SIZE 4096
48 static SHA *getSHA(pTHX_ SV *self)
50 if (!sv_isobject(self) || !sv_derived_from(self, "Digest::SHA"))
52 return INT2PTR(SHA *, SvIV(SvRV(self)));
55 MODULE = Digest::SHA PACKAGE = Digest::SHA
69 shawrite(bitstr, bitcnt, s)
70 unsigned char * bitstr
75 newSHA(classname, alg)
82 if (!shainit(state, alg)) {
87 sv_setref_pv(RETVAL, classname, (void *) state);
88 SvREADONLY_on(SvRV(RETVAL));
99 if ((state = getSHA(aTHX_ self)) == NULL)
103 sv_setref_pv(RETVAL, sv_reftype(SvRV(self), 1), (void *) clone);
104 SvREADONLY_on(SvRV(RETVAL));
105 Copy(state, clone, 1, SHA);
118 Digest::SHA::sha1 = 0
119 Digest::SHA::sha1_hex = 1
120 Digest::SHA::sha1_base64 = 2
121 Digest::SHA::sha224 = 3
122 Digest::SHA::sha224_hex = 4
123 Digest::SHA::sha224_base64 = 5
124 Digest::SHA::sha256 = 6
125 Digest::SHA::sha256_hex = 7
126 Digest::SHA::sha256_base64 = 8
127 Digest::SHA::sha384 = 9
128 Digest::SHA::sha384_hex = 10
129 Digest::SHA::sha384_base64 = 11
130 Digest::SHA::sha512 = 12
131 Digest::SHA::sha512_hex = 13
132 Digest::SHA::sha512_base64 = 14
133 Digest::SHA::sha512224 = 15
134 Digest::SHA::sha512224_hex = 16
135 Digest::SHA::sha512224_base64 = 17
136 Digest::SHA::sha512256 = 18
137 Digest::SHA::sha512256_hex = 19
138 Digest::SHA::sha512256_base64 = 20
146 if (!shainit(&sha, ix2alg[ix]))
148 for (i = 0; i < items; i++) {
149 data = (UCHR *) (SvPVbyte(ST(i), len));
150 while (len > MAX_WRITE_SIZE) {
151 shawrite(data, MAX_WRITE_SIZE << 3, &sha);
152 data += MAX_WRITE_SIZE;
153 len -= MAX_WRITE_SIZE;
155 shawrite(data, len << 3, &sha);
160 result = (char *) shadigest(&sha);
163 else if (ix % 3 == 1)
164 result = shahex(&sha);
166 result = shabase64(&sha);
167 RETVAL = newSVpv(result, len);
174 Digest::SHA::hmac_sha1 = 0
175 Digest::SHA::hmac_sha1_hex = 1
176 Digest::SHA::hmac_sha1_base64 = 2
177 Digest::SHA::hmac_sha224 = 3
178 Digest::SHA::hmac_sha224_hex = 4
179 Digest::SHA::hmac_sha224_base64 = 5
180 Digest::SHA::hmac_sha256 = 6
181 Digest::SHA::hmac_sha256_hex = 7
182 Digest::SHA::hmac_sha256_base64 = 8
183 Digest::SHA::hmac_sha384 = 9
184 Digest::SHA::hmac_sha384_hex = 10
185 Digest::SHA::hmac_sha384_base64 = 11
186 Digest::SHA::hmac_sha512 = 12
187 Digest::SHA::hmac_sha512_hex = 13
188 Digest::SHA::hmac_sha512_base64 = 14
189 Digest::SHA::hmac_sha512224 = 15
190 Digest::SHA::hmac_sha512224_hex = 16
191 Digest::SHA::hmac_sha512224_base64 = 17
192 Digest::SHA::hmac_sha512256 = 18
193 Digest::SHA::hmac_sha512256_hex = 19
194 Digest::SHA::hmac_sha512256_base64 = 20
197 UCHR *key = (UCHR *) "";
204 key = (UCHR *) (SvPVbyte(ST(items-1), len));
206 if (hmacinit(&hmac, ix2alg[ix], key, len) == NULL)
208 for (i = 0; i < items - 1; i++) {
209 data = (UCHR *) (SvPVbyte(ST(i), len));
210 while (len > MAX_WRITE_SIZE) {
211 hmacwrite(data, MAX_WRITE_SIZE << 3, &hmac);
212 data += MAX_WRITE_SIZE;
213 len -= MAX_WRITE_SIZE;
215 hmacwrite(data, len << 3, &hmac);
220 result = (char *) hmacdigest(&hmac);
221 len = hmac.digestlen;
223 else if (ix % 3 == 1)
224 result = hmachex(&hmac);
226 result = hmacbase64(&hmac);
227 RETVAL = newSVpv(result, len);
235 Digest::SHA::hashsize = 0
236 Digest::SHA::algorithm = 1
240 if ((state = getSHA(aTHX_ self)) == NULL)
242 RETVAL = ix ? state->alg : (int) (state->digestlen << 3);
255 if ((state = getSHA(aTHX_ self)) == NULL)
257 for (i = 1; i < items; i++) {
258 data = (UCHR *) (SvPVbyte(ST(i), len));
259 while (len > MAX_WRITE_SIZE) {
260 shawrite(data, MAX_WRITE_SIZE << 3, state);
261 data += MAX_WRITE_SIZE;
262 len -= MAX_WRITE_SIZE;
264 shawrite(data, len << 3, state);
272 Digest::SHA::digest = 0
273 Digest::SHA::hexdigest = 1
274 Digest::SHA::b64digest = 2
280 if ((state = getSHA(aTHX_ self)) == NULL)
285 result = (char *) shadigest(state);
286 len = state->digestlen;
289 result = shahex(state);
291 result = shabase64(state);
292 RETVAL = newSVpv(result, len);
305 if ((state = getSHA(aTHX_ self)) == NULL)
307 Copy(digcpy(state), ptr, state->alg <= SHA256 ? 32 : 64, UCHR);
308 ptr += state->alg <= SHA256 ? 32 : 64;
309 Copy(state->block, ptr, state->alg <= SHA256 ? 64 : 128, UCHR);
310 ptr += state->alg <= SHA256 ? 64 : 128;
311 ptr = w32mem(ptr, state->blockcnt);
312 ptr = w32mem(ptr, state->lenhh);
313 ptr = w32mem(ptr, state->lenhl);
314 ptr = w32mem(ptr, state->lenlh);
315 ptr = w32mem(ptr, state->lenll);
316 RETVAL = newSVpv((char *) buf, (STRLEN) (ptr - buf));
321 _putstate(self, packed_state)
330 if ((state = getSHA(aTHX_ self)) == NULL)
332 data = (UCHR *) SvPV(packed_state, len);
333 if (len != (state->alg <= SHA256 ? 116U : 212U))
335 data = statecpy(state, data);
336 Copy(data, state->block, state->blocksize >> 3, UCHR);
337 data += (state->blocksize >> 3);
338 bc = memw32(data), data += 4;
339 if (bc >= (state->alg <= SHA256 ? 512U : 1024U))
341 state->blockcnt = bc;
342 state->lenhh = memw32(data), data += 4;
343 state->lenhl = memw32(data), data += 4;
344 state->lenlh = memw32(data), data += 4;
345 state->lenll = memw32(data);
355 UCHR in[IO_BUFFER_SIZE];
357 if (!f || (state = getSHA(aTHX_ self)) == NULL)
359 while ((n = PerlIO_read(f, in, sizeof(in))) > 0)
360 shawrite(in, (ULNG) n << 3, state);
364 _addfileuniv(self, f)
372 UCHR in[IO_BUFFER_SIZE+1];
375 if (!f || (state = getSHA(aTHX_ self)) == NULL)
377 while ((n = PerlIO_read(f, in+1, IO_BUFFER_SIZE)) > 0) {
378 for (dst = in, src = in + 1; n; n--) {
389 else if (c == '\012') {
400 shawrite(in, (ULNG) (dst - in) << 3, state);
404 shawrite(in, 1 << 3, state);