Remove a (UINT) cast to silence a VC6 compiler warning
[p5sagit/p5-mst-13.2.git] / ext / Digest / SHA / SHA.xs
CommitLineData
6bc89f92 1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
4
5#include "ppport.h"
6
7#include <src/sha.c>
8#include <src/hmac.c>
9
10static int ix2alg[] =
11 {1,1,1,224,224,224,256,256,256,384,384,384,512,512,512};
12
13MODULE = Digest::SHA PACKAGE = Digest::SHA
14
15PROTOTYPES: ENABLE
16
17#include <src/sha.h>
18#include <src/hmac.h>
19
20int
21shaclose(s)
22 SHA * s
23
24int
25shadump(file, s)
26 char * file
27 SHA * s
28
29SHA *
30shadup(s)
31 SHA * s
32
33SHA *
34shaload(file)
35 char * file
36
37SHA *
38shaopen(alg)
39 int alg
40
41void
42sharewind(s)
43 SHA * s
44
45unsigned long
46shawrite(bitstr, bitcnt, s)
47 unsigned char * bitstr
48 unsigned long bitcnt
49 SHA * s
50
51void
52sha1(...)
53ALIAS:
54 Digest::SHA::sha1 = 0
55 Digest::SHA::sha1_hex = 1
56 Digest::SHA::sha1_base64 = 2
57 Digest::SHA::sha224 = 3
58 Digest::SHA::sha224_hex = 4
59 Digest::SHA::sha224_base64 = 5
60 Digest::SHA::sha256 = 6
61 Digest::SHA::sha256_hex = 7
62 Digest::SHA::sha256_base64 = 8
63 Digest::SHA::sha384 = 9
64 Digest::SHA::sha384_hex = 10
65 Digest::SHA::sha384_base64 = 11
66 Digest::SHA::sha512 = 12
67 Digest::SHA::sha512_hex = 13
68 Digest::SHA::sha512_base64 = 14
69PREINIT:
70 int i;
71 unsigned char *data;
72 STRLEN len;
73 SHA *state;
74 char *result;
75PPCODE:
76 if ((state = shaopen(ix2alg[ix])) == NULL)
77 XSRETURN_UNDEF;
78 for (i = 0; i < items; i++) {
79 data = (unsigned char *) (SvPV(ST(i), len));
80 shawrite(data, len << 3, state);
81 }
82 shafinish(state);
83 len = 0;
84 if (ix % 3 == 0) {
85 result = (char *) shadigest(state);
86 len = shadsize(state);
87 }
88 else if (ix % 3 == 1)
89 result = shahex(state);
90 else
91 result = shabase64(state);
92 ST(0) = sv_2mortal(newSVpv(result, len));
93 shaclose(state);
94 XSRETURN(1);
95
96void
97hmac_sha1(...)
98ALIAS:
99 Digest::SHA::hmac_sha1 = 0
100 Digest::SHA::hmac_sha1_hex = 1
101 Digest::SHA::hmac_sha1_base64 = 2
102 Digest::SHA::hmac_sha224 = 3
103 Digest::SHA::hmac_sha224_hex = 4
104 Digest::SHA::hmac_sha224_base64 = 5
105 Digest::SHA::hmac_sha256 = 6
106 Digest::SHA::hmac_sha256_hex = 7
107 Digest::SHA::hmac_sha256_base64 = 8
108 Digest::SHA::hmac_sha384 = 9
109 Digest::SHA::hmac_sha384_hex = 10
110 Digest::SHA::hmac_sha384_base64 = 11
111 Digest::SHA::hmac_sha512 = 12
112 Digest::SHA::hmac_sha512_hex = 13
113 Digest::SHA::hmac_sha512_base64 = 14
114PREINIT:
115 int i;
116 unsigned char *key;
117 unsigned char *data;
118 STRLEN len;
119 HMAC *state;
120 char *result;
121PPCODE:
122 key = (unsigned char *) (SvPV(ST(items-1), len));
123 if ((state = hmacopen(ix2alg[ix], key, len)) == NULL)
124 XSRETURN_UNDEF;
125 for (i = 0; i < items - 1; i++) {
126 data = (unsigned char *) (SvPV(ST(i), len));
127 hmacwrite(data, len << 3, state);
128 }
129 hmacfinish(state);
130 len = 0;
131 if (ix % 3 == 0) {
132 result = (char *) hmacdigest(state);
133 len = shadsize(state->osha);
134 }
135 else if (ix % 3 == 1)
136 result = hmachex(state);
137 else
138 result = hmacbase64(state);
139 ST(0) = sv_2mortal(newSVpv(result, len));
140 hmacclose(state);
141 XSRETURN(1);
142
143void
144hashsize(self)
145 SV * self
146ALIAS:
147 Digest::SHA::hashsize = 0
148 Digest::SHA::algorithm = 1
149PREINIT:
150 SHA *state;
151 int result;
152PPCODE:
153 state = INT2PTR(SHA *, SvIV(SvRV(SvRV(self))));
154 result = shadsize(state) << 3;
155 if (ix == 1 && result == 160)
156 result = 1;
157 ST(0) = sv_2mortal(newSViv(result));
158 XSRETURN(1);
159
160void
161add(self, ...)
162 SV * self
163PREINIT:
164 int i;
165 unsigned char *data;
166 STRLEN len;
167 SHA *state;
168PPCODE:
169 state = INT2PTR(SHA *, SvIV(SvRV(SvRV(self))));
170 for (i = 1; i < items; i++) {
171 data = (unsigned char *) (SvPV(ST(i), len));
172 shawrite(data, len << 3, state);
173 }
174 XSRETURN(1);
175
176void
177digest(self)
178 SV * self
179ALIAS:
180 Digest::SHA::digest = 0
181 Digest::SHA::Hexdigest = 1
182 Digest::SHA::B64digest = 2
183PREINIT:
184 STRLEN len;
185 SHA *state;
186 char *result;
187PPCODE:
188 state = INT2PTR(SHA *, SvIV(SvRV(SvRV(self))));
189 shafinish(state);
190 len = 0;
191 if (ix == 0) {
192 result = (char *) shadigest(state);
193 len = shadsize(state);
194 }
195 else if (ix == 1)
196 result = shahex(state);
197 else
198 result = shabase64(state);
199 ST(0) = sv_2mortal(newSVpv(result, len));
200 sharewind(state);
201 XSRETURN(1);