码迷,mamicode.com
首页 > 编程语言 > 详细

本文实例讲述了JavaScript实现SHA-1加密算法的方法--------调用方法:hex_sha1即可

时间:2016-11-09 19:29:50      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:integer   append   let   output   message   round   加密算法   lower   bsp   

  1 /*
  2 
  3  *
  4 
  5  * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
  6 
  7  * in FIPS PUB 180-1
  8 
  9  *
 10 
 11  * By lizq
 12 
 13  *
 14 
 15  * 2006-11-11
 16 
 17  *
 18 
 19  */
 20 
 21 /*
 22 
 23  *
 24 
 25  * Configurable variables.
 26 
 27  *
 28 
 29  */
 30 
 31 var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
 32 
 33 var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
 34 
 35 /*
 36 
 37  *
 38 
 39  * The main function to calculate message digest
 40 
 41  *
 42 
 43  */
 44 
 45 function hex_sha1(s){
 46 
 47     return binb2hex(core_sha1(AlignSHA1(s)));
 48 
 49 }
 50 
 51 /*
 52 
 53  *
 54 
 55  * Perform a simple self-test to see if the VM is working
 56 
 57  *
 58 
 59  */
 60 
 61 function sha1_vm_test(){
 62 
 63     return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
 64 
 65 }
 66 
 67 /*
 68 
 69  *
 70 
 71  * Calculate the SHA-1 of an array of big-endian words, and a bit length
 72 
 73  *
 74 
 75  */
 76 
 77 function core_sha1(blockArray){
 78 
 79     var x = blockArray; // append padding
 80 
 81     var w = Array(80);
 82 
 83     var a = 1732584193;
 84 
 85     var b = -271733879;
 86 
 87     var c = -1732584194;
 88 
 89     var d = 271733878;
 90 
 91     var e = -1009589776;
 92 
 93     for (var i = 0; i < x.length; i += 16) // 每次处理512位 16*32
 94 
 95     {
 96 
 97         var olda = a;
 98 
 99         var oldb = b;
100 
101         var oldc = c;
102 
103         var oldd = d;
104 
105         var olde = e;
106 
107         for (var j = 0; j < 80; j++) // 对每个512位进行80步操作
108 
109         {
110 
111             if (j < 16) 
112 
113                 w[j] = x[i + j];
114 
115             else
116 
117                 w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
118 
119             var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j)));
120 
121             e = d;
122 
123             d = c;
124 
125             c = rol(b, 30);
126 
127             b = a;
128 
129             a = t;
130 
131         }
132 
133         a = safe_add(a, olda);
134 
135         b = safe_add(b, oldb);
136 
137         c = safe_add(c, oldc);
138 
139         d = safe_add(d, oldd);
140 
141         e = safe_add(e, olde);
142 
143     }
144 
145     return new Array(a, b, c, d, e);
146 
147 }
148 
149 /*
150 
151  *
152 
153  * Perform the appropriate triplet combination function for the current
154 
155  * iteration
156 
157  *
158 
159  * 返回对应F函数的值
160 
161  *
162 
163  */
164 
165 function sha1_ft(t, b, c, d){
166 
167     if (t < 20) 
168 
169         return (b & c) | ((~ b) & d);
170 
171     if (t < 40) 
172 
173         return b ^ c ^ d;
174 
175     if (t < 60) 
176 
177         return (b & c) | (b & d) | (c & d);
178 
179     return b ^ c ^ d; // t<80
180 
181 }
182 
183 /*
184 
185  *
186 
187  * Determine the appropriate additive constant for the current iteration
188 
189  *
190 
191  * 返回对应的Kt值
192 
193  *
194 
195  */
196 
197 function sha1_kt(t){
198 
199     return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : (t < 60) ? -1894007588 : -899497514;
200 
201 }
202 
203 /*
204 
205  *
206 
207  * Add integers, wrapping at 2^32. This uses 16-bit operations internally
208 
209  *
210 
211  * to work around bugs in some JS interpreters.
212 
213  *
214 
215  * 将32位数拆成高16位和低16位分别进行相加,从而实现 MOD 2^32 的加法
216 
217  *
218 
219  */
220 
221 function safe_add(x, y){
222 
223     var lsw = (x & 0xFFFF) + (y & 0xFFFF);
224 
225     var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
226 
227     return (msw << 16) | (lsw & 0xFFFF);
228 
229 }
230 
231 /*
232 
233  *
234 
235  * Bitwise rotate a 32-bit number to the left.
236 
237  *
238 
239  * 32位二进制数循环左移
240 
241  *
242 
243  */
244 
245 function rol(num, cnt){
246 
247     return (num << cnt) | (num >>> (32 - cnt));
248 
249 }
250 
251 /*
252 
253  *
254 
255  * The standard SHA1 needs the input string to fit into a block
256 
257  *
258 
259  * This function align the input string to meet the requirement
260 
261  *
262 
263  */
264 
265 function AlignSHA1(str){
266 
267     var nblk = ((str.length + 8) >> 6) + 1, blks = new Array(nblk * 16);
268 
269     for (var i = 0; i < nblk * 16; i++) 
270 
271         blks[i] = 0;
272 
273     for (i = 0; i < str.length; i++) 
274 
275         blks[i >> 2] |= str.charCodeAt(i) << (24 - (i & 3) * 8);
276 
277     blks[i >> 2] |= 0x80 << (24 - (i & 3) * 8);
278 
279     blks[nblk * 16 - 1] = str.length * 8;
280 
281     return blks;
282 
283 }
284 
285 /*
286 
287  *
288 
289  * Convert an array of big-endian words to a hex string.
290 
291  *
292 
293  */
294 
295 function binb2hex(binarray){
296 
297     var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
298 
299     var str = "";
300 
301     for (var i = 0; i < binarray.length * 4; i++) {
302 
303         str += hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) +
304 
305         hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xF);
306 
307     }
308 
309     return str;
310 
311 }
312 
313 /*
314 
315  *
316 
317  * calculate MessageDigest accord to source message that inputted
318 
319  *
320 
321  */
322 
323 function calcDigest(){
324 
325     var digestM = hex_sha1(document.SHAForm.SourceMessage.value);
326 
327     document.SHAForm.MessageDigest.value = digestM;
328 
329 }

 

本文实例讲述了JavaScript实现SHA-1加密算法的方法--------调用方法:hex_sha1即可

标签:integer   append   let   output   message   round   加密算法   lower   bsp   

原文地址:http://www.cnblogs.com/wwljy/p/6048094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!