line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Javascript::SHA1; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Name: |
4
|
|
|
|
|
|
|
# Javascript::SHA1. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Documentation: |
7
|
|
|
|
|
|
|
# POD-style documentation is at the end. Extract it with pod2html.*. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# Reference: |
10
|
|
|
|
|
|
|
# Object Oriented Perl |
11
|
|
|
|
|
|
|
# Damian Conway |
12
|
|
|
|
|
|
|
# Manning |
13
|
|
|
|
|
|
|
# 1-884777-79-1 |
14
|
|
|
|
|
|
|
# P 114 |
15
|
|
|
|
|
|
|
# |
16
|
|
|
|
|
|
|
# Note: |
17
|
|
|
|
|
|
|
# o Tab = 4 spaces || die. |
18
|
|
|
|
|
|
|
# |
19
|
|
|
|
|
|
|
# Author: |
20
|
|
|
|
|
|
|
# Ron Savage |
21
|
|
|
|
|
|
|
# Home page: http://savage.net.au/index.html |
22
|
|
|
|
|
|
|
# |
23
|
|
|
|
|
|
|
# Licence: |
24
|
|
|
|
|
|
|
# Australian copyright (c) 2004 Ron Savage. |
25
|
|
|
|
|
|
|
# |
26
|
|
|
|
|
|
|
# All Programs of mine are 'OSI Certified Open Source Software'; |
27
|
|
|
|
|
|
|
# you can redistribute them and/or modify them under the terms of |
28
|
|
|
|
|
|
|
# The Artistic License, a copy of which is available at: |
29
|
|
|
|
|
|
|
# http://www.opensource.org/licenses/index.html |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
1
|
|
29342
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
34
|
|
32
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
33
|
1
|
|
|
1
|
|
4
|
no warnings 'redefine'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
494
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
require 5.005_62; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
require Exporter; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
42
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
43
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# This allows declaration use Javascript::SHA1 ':all'; |
46
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
47
|
|
|
|
|
|
|
# will save memory. |
48
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
) ] ); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
our @EXPORT = qw( |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
our $VERSION = '1.07'; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# ----------------------------------------------- |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# Preloaded methods go here. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# ----------------------------------------------- |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Encapsulated class data. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
{ |
68
|
|
|
|
|
|
|
my(%_attr_data) = |
69
|
|
|
|
|
|
|
( # Alphabetical order. |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _default_for |
73
|
|
|
|
|
|
|
{ |
74
|
0
|
|
|
0
|
|
|
my($self, $attr_name) = @_; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
$_attr_data{$attr_name}; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _standard_keys |
80
|
|
|
|
|
|
|
{ |
81
|
0
|
|
|
0
|
|
|
keys %_attr_data; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
} # End of encapsulated class data. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# ----------------------------------------------- |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub javascript |
89
|
|
|
|
|
|
|
{ |
90
|
0
|
|
|
0
|
0
|
|
my($self, $field_name, $form_number) = @_; |
91
|
0
|
|
0
|
|
|
|
$field_name ||= 'password'; |
92
|
0
|
|
0
|
|
|
|
$form_number ||= 0; |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
return <
|
95
|
|
|
|
|
|
|
/* |
96
|
|
|
|
|
|
|
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined |
97
|
|
|
|
|
|
|
* in FIPS PUB 180-1 |
98
|
|
|
|
|
|
|
* Version 2.1 Copyright Paul Johnston 2000 - 2002. |
99
|
|
|
|
|
|
|
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet |
100
|
|
|
|
|
|
|
* Distributed under the BSD License |
101
|
|
|
|
|
|
|
* See http://pajhome.org.uk/crypt/md5 for details. |
102
|
|
|
|
|
|
|
* Patch by Ron Savage 2004-04-27: |
103
|
|
|
|
|
|
|
* o Add the 3 functions str2hex_sha1, str2b64_sha1 and str2str_sha1(). |
104
|
|
|
|
|
|
|
*/ |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
function str2hex_sha1() |
107
|
|
|
|
|
|
|
{ |
108
|
|
|
|
|
|
|
document.forms[$form_number].$field_name.value = hex_sha1(document.forms[$form_number].$field_name.value); |
109
|
|
|
|
|
|
|
return true; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
function str2b64_sha1() |
113
|
|
|
|
|
|
|
{ |
114
|
|
|
|
|
|
|
document.forms[$form_number].$field_name.value = b64_sha1(document.forms[$form_number].$field_name.value); |
115
|
|
|
|
|
|
|
return true; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
function str2str_sha1() |
119
|
|
|
|
|
|
|
{ |
120
|
|
|
|
|
|
|
document.forms[$form_number].$field_name.value = str_sha1(document.forms[$form_number].$field_name.value); |
121
|
|
|
|
|
|
|
return true; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
/* |
125
|
|
|
|
|
|
|
* Configurable variables. You may need to tweak these to be compatible with |
126
|
|
|
|
|
|
|
* the server-side, but the defaults work in most cases. |
127
|
|
|
|
|
|
|
*/ |
128
|
|
|
|
|
|
|
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ |
129
|
|
|
|
|
|
|
var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ |
130
|
|
|
|
|
|
|
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
/* |
133
|
|
|
|
|
|
|
* These are the functions you'll usually want to call. |
134
|
|
|
|
|
|
|
* They take string arguments and return either hex or base-64 encoded strings |
135
|
|
|
|
|
|
|
*/ |
136
|
|
|
|
|
|
|
function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));} |
137
|
|
|
|
|
|
|
function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));} |
138
|
|
|
|
|
|
|
function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));} |
139
|
|
|
|
|
|
|
function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));} |
140
|
|
|
|
|
|
|
function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));} |
141
|
|
|
|
|
|
|
function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
/* |
144
|
|
|
|
|
|
|
* Perform a simple self-test to see if the VM is working |
145
|
|
|
|
|
|
|
*/ |
146
|
|
|
|
|
|
|
function sha1_vm_test() |
147
|
|
|
|
|
|
|
{ |
148
|
|
|
|
|
|
|
return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d"; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
/* |
152
|
|
|
|
|
|
|
* Calculate the SHA-1 of an array of big-endian words, and a bit length |
153
|
|
|
|
|
|
|
*/ |
154
|
|
|
|
|
|
|
function core_sha1(x, len) |
155
|
|
|
|
|
|
|
{ |
156
|
|
|
|
|
|
|
/* append padding */ |
157
|
|
|
|
|
|
|
x[len >> 5] |= 0x80 << (24 - len % 32); |
158
|
|
|
|
|
|
|
x[((len + 64 >> 9) << 4) + 15] = len; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
var w = Array(80); |
161
|
|
|
|
|
|
|
var a = 1732584193; |
162
|
|
|
|
|
|
|
var b = -271733879; |
163
|
|
|
|
|
|
|
var c = -1732584194; |
164
|
|
|
|
|
|
|
var d = 271733878; |
165
|
|
|
|
|
|
|
var e = -1009589776; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
for(var i = 0; i < x.length; i += 16) |
168
|
|
|
|
|
|
|
{ |
169
|
|
|
|
|
|
|
var olda = a; |
170
|
|
|
|
|
|
|
var oldb = b; |
171
|
|
|
|
|
|
|
var oldc = c; |
172
|
|
|
|
|
|
|
var oldd = d; |
173
|
|
|
|
|
|
|
var olde = e; |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
for(var j = 0; j < 80; j++) |
176
|
|
|
|
|
|
|
{ |
177
|
|
|
|
|
|
|
if(j < 16) w[j] = x[i + j]; |
178
|
|
|
|
|
|
|
else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1); |
179
|
|
|
|
|
|
|
var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), |
180
|
|
|
|
|
|
|
safe_add(safe_add(e, w[j]), sha1_kt(j))); |
181
|
|
|
|
|
|
|
e = d; |
182
|
|
|
|
|
|
|
d = c; |
183
|
|
|
|
|
|
|
c = rol(b, 30); |
184
|
|
|
|
|
|
|
b = a; |
185
|
|
|
|
|
|
|
a = t; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
a = safe_add(a, olda); |
189
|
|
|
|
|
|
|
b = safe_add(b, oldb); |
190
|
|
|
|
|
|
|
c = safe_add(c, oldc); |
191
|
|
|
|
|
|
|
d = safe_add(d, oldd); |
192
|
|
|
|
|
|
|
e = safe_add(e, olde); |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
return Array(a, b, c, d, e); |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
/* |
199
|
|
|
|
|
|
|
* Perform the appropriate triplet combination function for the current |
200
|
|
|
|
|
|
|
* iteration |
201
|
|
|
|
|
|
|
*/ |
202
|
|
|
|
|
|
|
function sha1_ft(t, b, c, d) |
203
|
|
|
|
|
|
|
{ |
204
|
|
|
|
|
|
|
if(t < 20) return (b & c) | ((~b) & d); |
205
|
|
|
|
|
|
|
if(t < 40) return b ^ c ^ d; |
206
|
|
|
|
|
|
|
if(t < 60) return (b & c) | (b & d) | (c & d); |
207
|
|
|
|
|
|
|
return b ^ c ^ d; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
/* |
211
|
|
|
|
|
|
|
* Determine the appropriate additive constant for the current iteration |
212
|
|
|
|
|
|
|
*/ |
213
|
|
|
|
|
|
|
function sha1_kt(t) |
214
|
|
|
|
|
|
|
{ |
215
|
|
|
|
|
|
|
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : |
216
|
|
|
|
|
|
|
(t < 60) ? -1894007588 : -899497514; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
/* |
220
|
|
|
|
|
|
|
* Calculate the HMAC-SHA1 of a key and some data |
221
|
|
|
|
|
|
|
*/ |
222
|
|
|
|
|
|
|
function core_hmac_sha1(key, data) |
223
|
|
|
|
|
|
|
{ |
224
|
|
|
|
|
|
|
var bkey = str2binb(key); |
225
|
|
|
|
|
|
|
if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz); |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
var ipad = Array(16), opad = Array(16); |
228
|
|
|
|
|
|
|
for(var i = 0; i < 16; i++) |
229
|
|
|
|
|
|
|
{ |
230
|
|
|
|
|
|
|
ipad[i] = bkey[i] ^ 0x36363636; |
231
|
|
|
|
|
|
|
opad[i] = bkey[i] ^ 0x5C5C5C5C; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz); |
235
|
|
|
|
|
|
|
return core_sha1(opad.concat(hash), 512 + 160); |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
/* |
239
|
|
|
|
|
|
|
* Add integers, wrapping at 2^32. This uses 16-bit operations internally |
240
|
|
|
|
|
|
|
* to work around bugs in some JS interpreters. |
241
|
|
|
|
|
|
|
*/ |
242
|
|
|
|
|
|
|
function safe_add(x, y) |
243
|
|
|
|
|
|
|
{ |
244
|
|
|
|
|
|
|
var lsw = (x & 0xFFFF) + (y & 0xFFFF); |
245
|
|
|
|
|
|
|
var msw = (x >> 16) + (y >> 16) + (lsw >> 16); |
246
|
|
|
|
|
|
|
return (msw << 16) | (lsw & 0xFFFF); |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
/* |
250
|
|
|
|
|
|
|
* Bitwise rotate a 32-bit number to the left. |
251
|
|
|
|
|
|
|
*/ |
252
|
|
|
|
|
|
|
function rol(num, cnt) |
253
|
|
|
|
|
|
|
{ |
254
|
|
|
|
|
|
|
return (num << cnt) | (num >>> (32 - cnt)); |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
/* |
258
|
|
|
|
|
|
|
* Convert an 8-bit or 16-bit string to an array of big-endian words |
259
|
|
|
|
|
|
|
* In 8-bit function, characters >255 have their hi-byte silently ignored. |
260
|
|
|
|
|
|
|
*/ |
261
|
|
|
|
|
|
|
function str2binb(str) |
262
|
|
|
|
|
|
|
{ |
263
|
|
|
|
|
|
|
var bin = Array(); |
264
|
|
|
|
|
|
|
var mask = (1 << chrsz) - 1; |
265
|
|
|
|
|
|
|
for(var i = 0; i < str.length * chrsz; i += chrsz) |
266
|
|
|
|
|
|
|
bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i%32); |
267
|
|
|
|
|
|
|
return bin; |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
/* |
271
|
|
|
|
|
|
|
* Convert an array of big-endian words to a string |
272
|
|
|
|
|
|
|
*/ |
273
|
|
|
|
|
|
|
function binb2str(bin) |
274
|
|
|
|
|
|
|
{ |
275
|
|
|
|
|
|
|
var str = ""; |
276
|
|
|
|
|
|
|
var mask = (1 << chrsz) - 1; |
277
|
|
|
|
|
|
|
for(var i = 0; i < bin.length * 32; i += chrsz) |
278
|
|
|
|
|
|
|
str += String.fromCharCode((bin[i>>5] >>> (24 - i%32)) & mask); |
279
|
|
|
|
|
|
|
return str; |
280
|
|
|
|
|
|
|
} |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
/* |
283
|
|
|
|
|
|
|
* Convert an array of big-endian words to a hex string. |
284
|
|
|
|
|
|
|
*/ |
285
|
|
|
|
|
|
|
function binb2hex(binarray) |
286
|
|
|
|
|
|
|
{ |
287
|
|
|
|
|
|
|
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; |
288
|
|
|
|
|
|
|
var str = ""; |
289
|
|
|
|
|
|
|
for(var i = 0; i < binarray.length * 4; i++) |
290
|
|
|
|
|
|
|
{ |
291
|
|
|
|
|
|
|
str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) + |
292
|
|
|
|
|
|
|
hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF); |
293
|
|
|
|
|
|
|
} |
294
|
|
|
|
|
|
|
return str; |
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
/* |
298
|
|
|
|
|
|
|
* Convert an array of big-endian words to a base-64 string |
299
|
|
|
|
|
|
|
*/ |
300
|
|
|
|
|
|
|
function binb2b64(binarray) |
301
|
|
|
|
|
|
|
{ |
302
|
|
|
|
|
|
|
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
303
|
|
|
|
|
|
|
var str = ""; |
304
|
|
|
|
|
|
|
for(var i = 0; i < binarray.length * 4; i += 3) |
305
|
|
|
|
|
|
|
{ |
306
|
|
|
|
|
|
|
var triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16) |
307
|
|
|
|
|
|
|
| (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 ) |
308
|
|
|
|
|
|
|
| ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF); |
309
|
|
|
|
|
|
|
for(var j = 0; j < 4; j++) |
310
|
|
|
|
|
|
|
{ |
311
|
|
|
|
|
|
|
if(i * 8 + j * 6 > binarray.length * 32) str += b64pad; |
312
|
|
|
|
|
|
|
else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); |
313
|
|
|
|
|
|
|
} |
314
|
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
return str; |
316
|
|
|
|
|
|
|
} |
317
|
|
|
|
|
|
|
EOS |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
} # End of javascript. |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
# ----------------------------------------------- |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
sub new |
324
|
|
|
|
|
|
|
{ |
325
|
0
|
|
|
0
|
0
|
|
my($class, %arg) = @_; |
326
|
0
|
|
|
|
|
|
my($self) = bless({}, $class); |
327
|
|
|
|
|
|
|
|
328
|
0
|
|
|
|
|
|
for my $attr_name ($self -> _standard_keys() ) |
329
|
|
|
|
|
|
|
{ |
330
|
0
|
|
|
|
|
|
my($arg_name) = $attr_name =~ /^_(.*)/; |
331
|
|
|
|
|
|
|
|
332
|
0
|
0
|
|
|
|
|
if (exists($arg{$arg_name}) ) |
333
|
|
|
|
|
|
|
{ |
334
|
0
|
|
|
|
|
|
$$self{$attr_name} = $arg{$arg_name}; |
335
|
|
|
|
|
|
|
} |
336
|
|
|
|
|
|
|
else |
337
|
|
|
|
|
|
|
{ |
338
|
0
|
|
|
|
|
|
$$self{$attr_name} = $self -> _default_for($attr_name); |
339
|
|
|
|
|
|
|
} |
340
|
|
|
|
|
|
|
} |
341
|
|
|
|
|
|
|
|
342
|
0
|
|
|
|
|
|
$self; |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
} # End of new. |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
# ----------------------------------------------- |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
1; |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
__END__ |