Commit | Line | Data |
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 | |
10 | static int ix2alg[] = |
11 | {1,1,1,224,224,224,256,256,256,384,384,384,512,512,512}; |
12 | |
13 | MODULE = Digest::SHA PACKAGE = Digest::SHA |
14 | |
15 | PROTOTYPES: ENABLE |
16 | |
17 | #include <src/sha.h> |
18 | #include <src/hmac.h> |
19 | |
20 | int |
21 | shaclose(s) |
22 | SHA * s |
23 | |
24 | int |
25 | shadump(file, s) |
26 | char * file |
27 | SHA * s |
28 | |
29 | SHA * |
30 | shadup(s) |
31 | SHA * s |
32 | |
33 | SHA * |
34 | shaload(file) |
35 | char * file |
36 | |
37 | SHA * |
38 | shaopen(alg) |
39 | int alg |
40 | |
41 | void |
42 | sharewind(s) |
43 | SHA * s |
44 | |
45 | unsigned long |
46 | shawrite(bitstr, bitcnt, s) |
47 | unsigned char * bitstr |
48 | unsigned long bitcnt |
49 | SHA * s |
50 | |
51 | void |
52 | sha1(...) |
53 | ALIAS: |
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 |
69 | PREINIT: |
70 | int i; |
71 | unsigned char *data; |
72 | STRLEN len; |
73 | SHA *state; |
74 | char *result; |
75 | PPCODE: |
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 | |
96 | void |
97 | hmac_sha1(...) |
98 | ALIAS: |
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 |
114 | PREINIT: |
115 | int i; |
116 | unsigned char *key; |
117 | unsigned char *data; |
118 | STRLEN len; |
119 | HMAC *state; |
120 | char *result; |
121 | PPCODE: |
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 | |
143 | void |
144 | hashsize(self) |
145 | SV * self |
146 | ALIAS: |
147 | Digest::SHA::hashsize = 0 |
148 | Digest::SHA::algorithm = 1 |
149 | PREINIT: |
150 | SHA *state; |
151 | int result; |
152 | PPCODE: |
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 | |
160 | void |
161 | add(self, ...) |
162 | SV * self |
163 | PREINIT: |
164 | int i; |
165 | unsigned char *data; |
166 | STRLEN len; |
167 | SHA *state; |
168 | PPCODE: |
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 | |
176 | void |
177 | digest(self) |
178 | SV * self |
179 | ALIAS: |
180 | Digest::SHA::digest = 0 |
181 | Digest::SHA::Hexdigest = 1 |
182 | Digest::SHA::B64digest = 2 |
183 | PREINIT: |
184 | STRLEN len; |
185 | SHA *state; |
186 | char *result; |
187 | PPCODE: |
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); |