JavaScript加密库jQuery.md5.js

JavaScript简单的MD5加密库jQuery.md5.js,简单用法如下:

//Create (hex-encoded) MD5 hash of a given string value:
var md5 = $.md5(‘value‘);
//Create (hex-encoded) HMAC-MD5 hash of a given string value and key:
var md5 = $.md5(‘value‘, ‘key‘);
//Create raw MD5 hash of a given string value:
var md5 = $.md5(‘value‘, null, true);
//Create raw HMAC-MD5 hash of a given string value and key:
var md5 = $.md5(‘value‘, ‘key‘, true);

源码查看地址 https://github.com/placemarker/jQuery-MD5

  1 /*
  2  * jQuery MD5 Plugin 1.2.1
  3  * https://github.com/blueimp/jQuery-MD5
  4  *
  5  * Copyright 2010, Sebastian Tschan
  6  * https://blueimp.net
  7  *
  8  * Licensed under the MIT license:
  9  * http://creativecommons.org/licenses/MIT/
 10  *
 11  * Based on
 12  * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 13  * Digest Algorithm, as defined in RFC 1321.
 14  * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
 15  * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 16  * Distributed under the BSD License
 17  * See http://pajhome.org.uk/crypt/md5 for more info.
 18  */
 19
 20 /*jslint bitwise: true */
 21 /*global unescape, jQuery */
 22
 23 (function ($) {
 24     ‘use strict‘;
 25
 26     /*
 27     * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 28     * to work around bugs in some JS interpreters.
 29     */
 30     function safe_add(x, y) {
 31         var lsw = (x & 0xFFFF) + (y & 0xFFFF),
 32             msw = (x >> 16) + (y >> 16) + (lsw >> 16);
 33         return (msw << 16) | (lsw & 0xFFFF);
 34     }
 35
 36     /*
 37     * Bitwise rotate a 32-bit number to the left.
 38     */
 39     function bit_rol(num, cnt) {
 40         return (num << cnt) | (num >>> (32 - cnt));
 41     }
 42
 43     /*
 44     * These functions implement the four basic operations the algorithm uses.
 45     */
 46     function md5_cmn(q, a, b, x, s, t) {
 47         return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
 48     }
 49     function md5_ff(a, b, c, d, x, s, t) {
 50         return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
 51     }
 52     function md5_gg(a, b, c, d, x, s, t) {
 53         return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
 54     }
 55     function md5_hh(a, b, c, d, x, s, t) {
 56         return md5_cmn(b ^ c ^ d, a, b, x, s, t);
 57     }
 58     function md5_ii(a, b, c, d, x, s, t) {
 59         return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
 60     }
 61
 62     /*
 63     * Calculate the MD5 of an array of little-endian words, and a bit length.
 64     */
 65     function binl_md5(x, len) {
 66         /* append padding */
 67         x[len >> 5] |= 0x80 << ((len) % 32);
 68         x[(((len + 64) >>> 9) << 4) + 14] = len;
 69
 70         var i, olda, oldb, oldc, oldd,
 71             a =  1732584193,
 72             b = -271733879,
 73             c = -1732584194,
 74             d =  271733878;
 75
 76         for (i = 0; i < x.length; i += 16) {
 77             olda = a;
 78             oldb = b;
 79             oldc = c;
 80             oldd = d;
 81
 82             a = md5_ff(a, b, c, d, x[i],       7, -680876936);
 83             d = md5_ff(d, a, b, c, x[i +  1], 12, -389564586);
 84             c = md5_ff(c, d, a, b, x[i +  2], 17,  606105819);
 85             b = md5_ff(b, c, d, a, x[i +  3], 22, -1044525330);
 86             a = md5_ff(a, b, c, d, x[i +  4],  7, -176418897);
 87             d = md5_ff(d, a, b, c, x[i +  5], 12,  1200080426);
 88             c = md5_ff(c, d, a, b, x[i +  6], 17, -1473231341);
 89             b = md5_ff(b, c, d, a, x[i +  7], 22, -45705983);
 90             a = md5_ff(a, b, c, d, x[i +  8],  7,  1770035416);
 91             d = md5_ff(d, a, b, c, x[i +  9], 12, -1958414417);
 92             c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
 93             b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
 94             a = md5_ff(a, b, c, d, x[i + 12],  7,  1804603682);
 95             d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
 96             c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
 97             b = md5_ff(b, c, d, a, x[i + 15], 22,  1236535329);
 98
 99             a = md5_gg(a, b, c, d, x[i +  1],  5, -165796510);
100             d = md5_gg(d, a, b, c, x[i +  6],  9, -1069501632);
101             c = md5_gg(c, d, a, b, x[i + 11], 14,  643717713);
102             b = md5_gg(b, c, d, a, x[i],      20, -373897302);
103             a = md5_gg(a, b, c, d, x[i +  5],  5, -701558691);
104             d = md5_gg(d, a, b, c, x[i + 10],  9,  38016083);
105             c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
106             b = md5_gg(b, c, d, a, x[i +  4], 20, -405537848);
107             a = md5_gg(a, b, c, d, x[i +  9],  5,  568446438);
108             d = md5_gg(d, a, b, c, x[i + 14],  9, -1019803690);
109             c = md5_gg(c, d, a, b, x[i +  3], 14, -187363961);
110             b = md5_gg(b, c, d, a, x[i +  8], 20,  1163531501);
111             a = md5_gg(a, b, c, d, x[i + 13],  5, -1444681467);
112             d = md5_gg(d, a, b, c, x[i +  2],  9, -51403784);
113             c = md5_gg(c, d, a, b, x[i +  7], 14,  1735328473);
114             b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
115
116             a = md5_hh(a, b, c, d, x[i +  5],  4, -378558);
117             d = md5_hh(d, a, b, c, x[i +  8], 11, -2022574463);
118             c = md5_hh(c, d, a, b, x[i + 11], 16,  1839030562);
119             b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
120             a = md5_hh(a, b, c, d, x[i +  1],  4, -1530992060);
121             d = md5_hh(d, a, b, c, x[i +  4], 11,  1272893353);
122             c = md5_hh(c, d, a, b, x[i +  7], 16, -155497632);
123             b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
124             a = md5_hh(a, b, c, d, x[i + 13],  4,  681279174);
125             d = md5_hh(d, a, b, c, x[i],      11, -358537222);
126             c = md5_hh(c, d, a, b, x[i +  3], 16, -722521979);
127             b = md5_hh(b, c, d, a, x[i +  6], 23,  76029189);
128             a = md5_hh(a, b, c, d, x[i +  9],  4, -640364487);
129             d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
130             c = md5_hh(c, d, a, b, x[i + 15], 16,  530742520);
131             b = md5_hh(b, c, d, a, x[i +  2], 23, -995338651);
132
133             a = md5_ii(a, b, c, d, x[i],       6, -198630844);
134             d = md5_ii(d, a, b, c, x[i +  7], 10,  1126891415);
135             c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
136             b = md5_ii(b, c, d, a, x[i +  5], 21, -57434055);
137             a = md5_ii(a, b, c, d, x[i + 12],  6,  1700485571);
138             d = md5_ii(d, a, b, c, x[i +  3], 10, -1894986606);
139             c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
140             b = md5_ii(b, c, d, a, x[i +  1], 21, -2054922799);
141             a = md5_ii(a, b, c, d, x[i +  8],  6,  1873313359);
142             d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
143             c = md5_ii(c, d, a, b, x[i +  6], 15, -1560198380);
144             b = md5_ii(b, c, d, a, x[i + 13], 21,  1309151649);
145             a = md5_ii(a, b, c, d, x[i +  4],  6, -145523070);
146             d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
147             c = md5_ii(c, d, a, b, x[i +  2], 15,  718787259);
148             b = md5_ii(b, c, d, a, x[i +  9], 21, -343485551);
149
150             a = safe_add(a, olda);
151             b = safe_add(b, oldb);
152             c = safe_add(c, oldc);
153             d = safe_add(d, oldd);
154         }
155         return [a, b, c, d];
156     }
157
158     /*
159     * Convert an array of little-endian words to a string
160     */
161     function binl2rstr(input) {
162         var i,
163             output = ‘‘;
164         for (i = 0; i < input.length * 32; i += 8) {
165             output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF);
166         }
167         return output;
168     }
169
170     /*
171     * Convert a raw string to an array of little-endian words
172     * Characters >255 have their high-byte silently ignored.
173     */
174     function rstr2binl(input) {
175         var i,
176             output = [];
177         output[(input.length >> 2) - 1] = undefined;
178         for (i = 0; i < output.length; i += 1) {
179             output[i] = 0;
180         }
181         for (i = 0; i < input.length * 8; i += 8) {
182             output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32);
183         }
184         return output;
185     }
186
187     /*
188     * Calculate the MD5 of a raw string
189     */
190     function rstr_md5(s) {
191         return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
192     }
193
194     /*
195     * Calculate the HMAC-MD5, of a key and some data (raw strings)
196     */
197     function rstr_hmac_md5(key, data) {
198         var i,
199             bkey = rstr2binl(key),
200             ipad = [],
201             opad = [],
202             hash;
203         ipad[15] = opad[15] = undefined;
204         if (bkey.length > 16) {
205             bkey = binl_md5(bkey, key.length * 8);
206         }
207         for (i = 0; i < 16; i += 1) {
208             ipad[i] = bkey[i] ^ 0x36363636;
209             opad[i] = bkey[i] ^ 0x5C5C5C5C;
210         }
211         hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
212         return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
213     }
214
215     /*
216     * Convert a raw string to a hex string
217     */
218     function rstr2hex(input) {
219         var hex_tab = ‘0123456789abcdef‘,
220             output = ‘‘,
221             x,
222             i;
223         for (i = 0; i < input.length; i += 1) {
224             x = input.charCodeAt(i);
225             output += hex_tab.charAt((x >>> 4) & 0x0F) +
226                 hex_tab.charAt(x & 0x0F);
227         }
228         return output;
229     }
230
231     /*
232     * Encode a string as utf-8
233     */
234     function str2rstr_utf8(input) {
235         return unescape(encodeURIComponent(input));
236     }
237
238     /*
239     * Take string arguments and return either raw or hex encoded strings
240     */
241     function raw_md5(s) {
242         return rstr_md5(str2rstr_utf8(s));
243     }
244     function hex_md5(s) {
245         return rstr2hex(raw_md5(s));
246     }
247     function raw_hmac_md5(k, d) {
248         return rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d));
249     }
250     function hex_hmac_md5(k, d) {
251         return rstr2hex(raw_hmac_md5(k, d));
252     }
253
254     $.md5 = function (string, key, raw) {
255         if (!key) {
256             if (!raw) {
257                 return hex_md5(string);
258             } else {
259                 return raw_md5(string);
260             }
261         }
262         if (!raw) {
263             return hex_hmac_md5(key, string);
264         } else {
265             return raw_hmac_md5(key, string);
266         }
267     };
268
269 }(typeof jQuery === ‘function‘ ? jQuery : this));

jQuery.md5.js

这里也附上源码,以方便

时间: 2024-10-16 21:08:33

JavaScript加密库jQuery.md5.js的相关文章

Jquery加密插件jquery.md5.js

使用jquery.md5.js加密插件可以实现在前端进行MD5加密 下载地址:https://github.com/placemarker/jQuery-MD5 使用方法 //Create (hex-encoded) MD5 hash of a given string value: var md5 = $.md5('value'); //Create (hex-encoded) HMAC-MD5 hash of a given string value and key: var md5 = $

JavaScript 框架库 - jQuery

jQuery 是一个 JavaScript 库. 引用 jQuery <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script> </head> <body> </body> </html> 一.jQu

JavaScript函数库.jQuery

jQuery使用方法 一.选择网页元素jQuery的基本设计和主要方法,就是"选择某个网页元素,然后对其进行某种操作",这是它区别与其他函数库的根本特点. 使用jQuery第一步,往往就是将一个选择表达式,放进构造函数jQuery()(简写为$),然后得到被选中的元素. 选择表达式可以是css选择器: $(document) //选择整个文档对象 $('#myId') //选择ID为myid的网页元素 $('div.myClass')    //选择class为myclass的div元

jQuery MD5加密实现代码

$(md("你想要加密的字符串")); md5插件下载地址:http://xiazai.jb51.net/201003/yuanma/jquery_md5.rar 下面是我的简单例子 : <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <

【JS加密库】SJCL :斯坦福大学JS加密库

斯坦福大学Javascript加密库简称SJCL,是一个由斯坦福大学计算机安全实验室创立的项目,旨在创建一个安全.快速.短小精悍.易使用.跨浏览器的JavaScript加密库. 斯坦福大学下载地址:http://crypto.stanford.edu/sjcl/sjcl.zip GitHub主页:http://github.com/bitwiseshiftleft/sjcl SJCL容易上手,简单地用下面的代码就可以加密.解密数据. sjcl.encrypt("password", &

javascript之 JavaScript 工具库

javascript之 JavaScript 工具库jQuery 目录: 一.查找标签和事件绑定以及操作标签的对比 二.DOM对象和jquery的转换 三.$(document).ready( )  四.链式操作 五.元素的操作:取值和赋值 六.元素的操作:移动 七.工具方法 八.特殊效果 一.查找标签和事件绑定以及操作标签的对比 1.查找标签的对比*** (1)原生javascript document.getElementsByClassName("classname");//根据

用jquery.qrcode.js生成高大尚的二维码

二维码的作用 1)  移动设备扫一扫,方便"带走"阅读(即把URL生成二维码) 2)  可以传递信息(单纯的把字符串生成二维码) 第三方开源库 项目源码:https://github.com/jeromeetienne/jquery-qrcode qrcode.js  实现二维码的核心函数库 jquery.qrcode.js  用jquery把核心函数库封装起来.用它来实现图形渲染,其实就是画图(支持canvas和table两种方式). 使用方法 1)  引入jquery库 2)  引

easyui中jquery.min.js引入要放在其他js之前

今天刚刚学习easyui 在引入easyui的js时,如果把自己的js放在easyui的js之前的话,easyui就不能成功引入例如: <script type="text/javascript" src="js/login.js"></script>        //自己的js <script type="text/javascript" src="easyui142/jquery.min.js&quo

jquery.form.js 让表单提交更优雅

jquery.form.js 让表单提交更优雅.可以页面不刷新提交表单,比jQuery的ajax提交要功能强大. 1.引入 <script src="/src/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="/src/jquery.form.js"></script> 2.使用 $(function () { va