OLD | NEW |
| (Empty) |
1 /** | |
2 * | |
3 * Secure Hash Algorithm (SHA1) | |
4 * http://www.webtoolkit.info/ | |
5 * | |
6 **/ | |
7 | |
8 function SHA1(msg) { | |
9 | |
10 function rotate_left(n,s) { | |
11 var t4 = ( n<<s ) | (n>>>(32-s)); | |
12 return t4; | |
13 }; | |
14 | |
15 function lsb_hex(val) { | |
16 var str=""; | |
17 var i; | |
18 var vh; | |
19 var vl; | |
20 | |
21 for( i=0; i<=6; i+=2 ) { | |
22 vh = (val>>>(i*4+4))&0x0f; | |
23 vl = (val>>>(i*4))&0x0f; | |
24 str += vh.toString(16) + vl.toString(16); | |
25 } | |
26 return str; | |
27 }; | |
28 | |
29 function cvt_hex(val) { | |
30 var str=""; | |
31 var i; | |
32 var v; | |
33 | |
34 for( i=7; i>=0; i-- ) { | |
35 v = (val>>>(i*4))&0x0f; | |
36 str += v.toString(16); | |
37 } | |
38 return str; | |
39 }; | |
40 | |
41 | |
42 function Utf8Encode(string) { | |
43 string = string.replace(/\r\n/g,"\n"); | |
44 var utftext = ""; | |
45 | |
46 for (var n = 0; n < string.length; n++) { | |
47 | |
48 var c = string.charCodeAt(n); | |
49 | |
50 if (c < 128) { | |
51 utftext += String.fromCharCode(c); | |
52 } | |
53 else if((c > 127) && (c < 2048)) { | |
54 utftext += String.fromCharCode((c >> 6) | 192); | |
55 utftext += String.fromCharCode((c & 63) | 128); | |
56 } | |
57 else { | |
58 utftext += String.fromCharCode((c >> 12) | 224); | |
59 utftext += String.fromCharCode(((c >> 6) & 63) |
128); | |
60 utftext += String.fromCharCode((c & 63) | 128); | |
61 } | |
62 | |
63 } | |
64 | |
65 return utftext; | |
66 }; | |
67 | |
68 var blockstart; | |
69 var i, j; | |
70 var W = new Array(80); | |
71 var H0 = 0x67452301; | |
72 var H1 = 0xEFCDAB89; | |
73 var H2 = 0x98BADCFE; | |
74 var H3 = 0x10325476; | |
75 var H4 = 0xC3D2E1F0; | |
76 var A, B, C, D, E; | |
77 var temp; | |
78 | |
79 msg = Utf8Encode(msg); | |
80 | |
81 var msg_len = msg.length; | |
82 | |
83 var word_array = new Array(); | |
84 for( i=0; i<msg_len-3; i+=4 ) { | |
85 j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i+1)<<16 | | |
86 msg.charCodeAt(i+2)<<8 | msg.charCodeAt(i+3); | |
87 word_array.push( j ); | |
88 } | |
89 | |
90 switch( msg_len % 4 ) { | |
91 case 0: | |
92 i = 0x080000000; | |
93 break; | |
94 case 1: | |
95 i = msg.charCodeAt(msg_len-1)<<24 | 0x0800000; | |
96 break; | |
97 | |
98 case 2: | |
99 i = msg.charCodeAt(msg_len-2)<<24 | msg.charCodeAt(msg_l
en-1)<<16 | 0x08000; | |
100 break; | |
101 | |
102 case 3: | |
103 i = msg.charCodeAt(msg_len-3)<<24 | msg.charCodeAt(msg_l
en-2)<<16 | msg.charCodeAt(msg_len-1)<<8 | 0x80; | |
104 break; | |
105 } | |
106 | |
107 word_array.push( i ); | |
108 | |
109 while( (word_array.length % 16) != 14 ) word_array.push( 0 ); | |
110 | |
111 word_array.push( msg_len>>>29 ); | |
112 word_array.push( (msg_len<<3)&0x0ffffffff ); | |
113 | |
114 | |
115 for ( blockstart=0; blockstart<word_array.length; blockstart+=16 ) { | |
116 | |
117 for( i=0; i<16; i++ ) W[i] = word_array[blockstart+i]; | |
118 for( i=16; i<=79; i++ ) W[i] = rotate_left(W[i-3] ^ W[i-8] ^ W[i
-14] ^ W[i-16], 1); | |
119 | |
120 A = H0; | |
121 B = H1; | |
122 C = H2; | |
123 D = H3; | |
124 E = H4; | |
125 | |
126 for( i= 0; i<=19; i++ ) { | |
127 temp = (rotate_left(A,5) + ((B&C) | (~B&D)) + E + W[i] +
0x5A827999) & 0x0ffffffff; | |
128 E = D; | |
129 D = C; | |
130 C = rotate_left(B,30); | |
131 B = A; | |
132 A = temp; | |
133 } | |
134 | |
135 for( i=20; i<=39; i++ ) { | |
136 temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0x6E
D9EBA1) & 0x0ffffffff; | |
137 E = D; | |
138 D = C; | |
139 C = rotate_left(B,30); | |
140 B = A; | |
141 A = temp; | |
142 } | |
143 | |
144 for( i=40; i<=59; i++ ) { | |
145 temp = (rotate_left(A,5) + ((B&C) | (B&D) | (C&D)) + E +
W[i] + 0x8F1BBCDC) & 0x0ffffffff; | |
146 E = D; | |
147 D = C; | |
148 C = rotate_left(B,30); | |
149 B = A; | |
150 A = temp; | |
151 } | |
152 | |
153 for( i=60; i<=79; i++ ) { | |
154 temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0xCA
62C1D6) & 0x0ffffffff; | |
155 E = D; | |
156 D = C; | |
157 C = rotate_left(B,30); | |
158 B = A; | |
159 A = temp; | |
160 } | |
161 | |
162 H0 = (H0 + A) & 0x0ffffffff; | |
163 H1 = (H1 + B) & 0x0ffffffff; | |
164 H2 = (H2 + C) & 0x0ffffffff; | |
165 H3 = (H3 + D) & 0x0ffffffff; | |
166 H4 = (H4 + E) & 0x0ffffffff; | |
167 | |
168 } | |
169 | |
170 var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_h
ex(H4); | |
171 | |
172 return temp.toLowerCase(); | |
173 } | |
OLD | NEW |