/*  * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message  * Digest Algorithm, as defined in RFC 1321.  * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.  * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet  * Distributed under the BSD License  * See http://pajhome.org.uk/crypt/md5 for more info.  */

/*  * Configurable variables. You may need to tweak these to be compatible with  * the server-side, but the defaults work in most cases.  */ var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */ var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */ var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*  * These are the functions you'll usually want to call  * They take string arguments and return either hex or base-64 encoded strings  */ function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));} function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));} function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));} function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); } function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); } function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }

/*  * Perform a simple self-test to see if the VM is working  */ function md5_vm_test() {   return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72"; }

/*  * Calculate the MD5 of an array of little-endian words, and a bit length  */ function core_md5(x, len) {   /* append padding */   x[len >> 5] |= 0x80 << ((len) % 32);   x[(((len + 64) >>> 9) << 4) + 14] = len;

var a =  1732584193;   var b = -271733879;   var c = -1732584194;   var d =  271733878;

for(var i = 0; i < x.length; i += 16)   {     var olda = a;     var oldb = b;     var oldc = c;     var oldd = d;

a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);     d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);     c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);     b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);     a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);     d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);     c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);     b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);     a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);     d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);     c = md5_ff(c, d, a, b, x[i+10], 17, -42063);     b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);     a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);     d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);     c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);     b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);     d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);     c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);     b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);     a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);     d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);     c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);     b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);     a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);     d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);     c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);     b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);     a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);     d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);     c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);     b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);     d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);     c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);     b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);     a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);     d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);     c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);     b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);     a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);     d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);     c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);     b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);     a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);     d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);     c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);     b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);     d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);     c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);     b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);     a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);     d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);     c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);     b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);     a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);     d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);     c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);     b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);     a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);     d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);     c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);     b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

a = safe_add(a, olda);     b = safe_add(b, oldb);     c = safe_add(c, oldc);     d = safe_add(d, oldd);   }   return Array(a, b, c, d);

}

/*  * These functions implement the four basic operations the algorithm uses.  */ function md5_cmn(q, a, b, x, s, t) {   return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b); } function md5_ff(a, b, c, d, x, s, t) {   return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t); } function md5_gg(a, b, c, d, x, s, t) {   return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t); } function md5_hh(a, b, c, d, x, s, t) {   return md5_cmn(b ^ c ^ d, a, b, x, s, t); } function md5_ii(a, b, c, d, x, s, t) {   return md5_cmn(c ^ (b | (~d)), a, b, x, s, t); }

/*  * Calculate the HMAC-MD5, of a key and some data  */ function core_hmac_md5(key, data) {   var bkey = str2binl(key);   if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);

var ipad = Array(16), opad = Array(16);   for(var i = 0; i < 16; i++)   {     ipad[i] = bkey[i] ^ 0x36363636;     opad[i] = bkey[i] ^ 0x5C5C5C5C;   }

var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);   return core_md5(opad.concat(hash), 512 + 128); }

/*  * Add integers, wrapping at 2^32. This uses 16-bit operations internally  * to work around bugs in some JS interpreters.  */ function safe_add(x, y) {   var lsw = (x & 0xFFFF) + (y & 0xFFFF);   var msw = (x >> 16) + (y >> 16) + (lsw >> 16);   return (msw << 16) | (lsw & 0xFFFF); }

/*  * Bitwise rotate a 32-bit number to the left.  */ function bit_rol(num, cnt) {   return (num << cnt) | (num >>> (32 - cnt)); }

/*  * Convert a string to an array of little-endian words  * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.  */ function str2binl(str) {   var bin = Array();   var mask = (1 << chrsz) - 1;   for(var i = 0; i < str.length * chrsz; i += chrsz)     bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);   return bin; }

/*  * Convert an array of little-endian words to a string  */ function binl2str(bin) {   var str = "";   var mask = (1 << chrsz) - 1;   for(var i = 0; i < bin.length * 32; i += chrsz)     str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);   return str; }

/*  * Convert an array of little-endian words to a hex string.  */ function binl2hex(binarray) {   var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";   var str = "";   for(var i = 0; i < binarray.length * 4; i++)   {     str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +            hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);   }   return str; }

/*  * Convert an array of little-endian words to a base-64 string  */ function binl2b64(binarray) {   var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";   var str = "";   for(var i = 0; i < binarray.length * 4; i += 3)   {     var triplet = (((binarray[i   >> 2] >> 8 * ( i   %4)) & 0xFF) << 16)                 | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )                 |  ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);     for(var j = 0; j < 4; j++)     {       if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;       else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);     }   }   return str; }

MD5类库(hex_md5)的更多相关文章

  1. MD5:js,java,C#三种语言加密结果不同解决办法

    最近遇到前端js MD5加密与后端C#与Java MD5加密结果不一致的问题,所以写个关于此问题的解决办法 前端js引用的md5类库,类库地址:https://blueimp.github.io/Ja ...

  2. 基于js-spark-md5前端js类库,快速获取文件Md5值

    js-spark-md5是歪果仁开发的东西,有点多,但是我们只要一个js文件即可,具体类包我存在自己的oschina上,下载地址:https://git.oschina.net/jianqingwan ...

  3. 【javascript类库】zepto和jquery的md5加密插件

    [javascript类库]zepto和jquery的md5加密插件 相信很多人对jQuery并不陌生,这款封装良好的插件被很多开发者使用. zepto可以说是jQuery在移动端的替代产品,它比jQ ...

  4. Java常用类库API之MD5简单使用

    常用类库--MD5简单使用 MD5消息摘要算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash v ...

  5. 前端使用CryptoJs类库进行sha-256、MD5加密

    Google的加密库 CryptoJs(点此下载) 包含了很多常用的加解密方式,包括AES.DES.SHA-1.SHA-2.SHA256.MD5等. DES对称加密在之前的文章中也有介绍过,大传送门. ...

  6. C# js asp.net 字符串MD5加密GetMD5Hash

    赵小虎老师 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sy ...

  7. 经常使用MD5算法代码

    经常使用的MD5算法代码日期: 2014年8月4日作者: 铁锚 MD5,全称为 Message Digest Algorithm 5(消息摘要算法第五版).详情请參考 维基百科:MD5  MD5加密后 ...

  8. MD5的各种实现——也是醉了

    MD5即Message-Digest Algorithm 5(信息-摘要算法5).用于确保信息传输完整一致. 是计算机广泛使用的杂凑算法之中的一个(又译摘要算法.哈希算法),主流编程语言普遍已有MD5 ...

  9. JS中使用MD5加密

    下载 MD5 使用MD5加密的方法:下载md5.js文件,在网页中引用该文件: < script type="text/javascript" src="md5.j ...

随机推荐

  1. android 和主线程有关的小问题

    1.在android 中 HTTP请求被不允许在主线程中执行,否则会抛出异常.如果请求时间过长,阻塞UI线程是一个非常差的体验之前,刚开始开发遇到这个问题,每次app运行到最后,发送网络请求就自己崩溃 ...

  2. Python Qt5 Creator 使用创建项目教程

    1.下载Creator 4.2.1 2.点击文件-新建项目-QT-QT designer Form 然后 choose 3. 4.窗口组件选择 5.下一步,然后就可以自己设计了,,, 最后说一下,保存 ...

  3. SQL夯实基础(九)MySQL联接查询算法

    书接上文<SQL夯实基础(八):联接运算符算法归类>. 这里先解释下EXPLAIN 结果中,第一行出现的表就是驱动表(Important!). 对驱动表可以直接排序,对非驱动表(的字段排序 ...

  4. Python 三元条件判断表达式(and or/if else)

    参考: http://wangye.org/blog/archives/690/

  5. Mysql-Proxy 读写分离的各种坑,特别是复制延迟时

    延迟问题读写分离不能回避的问题之一就是延迟,可以考虑Google提供的SemiSyncReplicationDesign补丁. 端口问题MySQL-Proxy缺省使用的是4040端口,如果你想透明的把 ...

  6. systemtap 安装试用

    1. 安装 yum install -y systemtap systemtap-runtime 2. 环境准备    a. 自动安装依赖 stap-prep b. 手动安装依赖 kernel-deb ...

  7. Docker阿里云镜像加速器 for CentOS 7

    CentOS 7 CentOS使用配置方式略微复杂,需要先将默认的配置文件复制出来 /lib/systemd/system/docker.service -> /etc/systemd/syst ...

  8. Python函数 hash()

    hash(object)    hash() 用于获取取一个对象(字符串或者数值等)的哈希值.返回对象的哈希值.  实例: >>>hash('test') # 字符串 2314058 ...

  9. (转)Android短信的发送和接收监听

    /**发送与接收的广播**/     String SENT_SMS_ACTION = "SENT_SMS_ACTION";     String DELIVERED_SMS_AC ...

  10. foreach(PHP学习)

    先来看一个例子: $arr = array(0,1,2,3,4);让数组的每个值都变成原来的两倍,应该怎么来实现? 如果没有学习foreach之前,会想到用for循环 <?php $arr = ...