微信小程序登录密码MD5加密

来源:互联网 发布:平安普惠软件打不开啊 编辑:程序博客网 时间:2024/05/21 08:41

 在小程序中,页面的脚本逻辑是在JsCore中运行,JsCore是一个没有窗口对象的环境,所以不能在脚本中使用window,也无法在脚本中操作组件。zepto/jQuery 也无法使用,因为zepto/jquery 会使用到window对象和document对象。所以在微信小程序中不能使用jquery.md5.js对密码进行加密。下面我提供一种MD5.js加密实例,本实例先静态演示,后面再到小程序中演示。

    md5.js程序如下:

[javascript] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message 
  3.  * Digest Algorithm, as defined in RFC 1321. 
  4.  * Version 1.1 Copyright (C) Paul Johnston 1999 - 2002. 
  5.  * Code also contributed by Greg Holt 
  6.  * See http://pajhome.org.uk/site/legal.html for details. 
  7.  */  
  8.   
  9. /* 
  10.  * Add integers, wrapping at 2^32. This uses 16-bit operations internally 
  11.  * to work around bugs in some JS interpreters. 
  12.  */  
  13. function safe_add(x, y)  
  14. {  
  15.   var lsw = (x & 0xFFFF) + (y & 0xFFFF)  
  16.   var msw = (x >> 16) + (y >> 16) + (lsw >> 16)  
  17.   return (msw << 16) | (lsw & 0xFFFF)  
  18. }  
  19.   
  20. /* 
  21.  * Bitwise rotate a 32-bit number to the left. 
  22.  */  
  23. function rol(num, cnt)  
  24. {  
  25.   return (num << cnt) | (num >>> (32 - cnt))  
  26. }  
  27.   
  28. /* 
  29.  * These functions implement the four basic operations the algorithm uses. 
  30.  */  
  31. function cmn(q, a, b, x, s, t)  
  32. {  
  33.   return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b)  
  34. }  
  35. function ff(a, b, c, d, x, s, t)  
  36. {  
  37.   return cmn((b & c) | ((~b) & d), a, b, x, s, t)  
  38. }  
  39. function gg(a, b, c, d, x, s, t)  
  40. {  
  41.   return cmn((b & d) | (c & (~d)), a, b, x, s, t)  
  42. }  
  43. function hh(a, b, c, d, x, s, t)  
  44. {  
  45.   return cmn(b ^ c ^ d, a, b, x, s, t)  
  46. }  
  47. function ii(a, b, c, d, x, s, t)  
  48. {  
  49.   return cmn(c ^ (b | (~d)), a, b, x, s, t)  
  50. }  
  51.   
  52. /* 
  53.  * Calculate the MD5 of an array of little-endian words, producing an array 
  54.  * of little-endian words. 
  55.  */  
  56. function coreMD5(x)  
  57. {  
  58.   var a =  1732584193  
  59.   var b = -271733879  
  60.   var c = -1732584194  
  61.   var d =  271733878  
  62.   
  63.   for(i = 0; i < x.length; i += 16)  
  64.   {  
  65.     var olda = a  
  66.     var oldb = b  
  67.     var oldc = c  
  68.     var oldd = d  
  69.     a = ff(a, b, c, d, x[i+ 0], 7 , -680876936)  
  70.     d = ff(d, a, b, c, x[i+ 1], 12, -389564586)  
  71.     c = ff(c, d, a, b, x[i+ 2], 17,  606105819)  
  72.     b = ff(b, c, d, a, x[i+ 3], 22, -1044525330)  
  73.     a = ff(a, b, c, d, x[i+ 4], 7 , -176418897)  
  74.     d = ff(d, a, b, c, x[i+ 5], 12,  1200080426)  
  75.     c = ff(c, d, a, b, x[i+ 6], 17, -1473231341)  
  76.     b = ff(b, c, d, a, x[i+ 7], 22, -45705983)  
  77.     a = ff(a, b, c, d, x[i+ 8], 7 ,  1770035416)  
  78.     d = ff(d, a, b, c, x[i+ 9], 12, -1958414417)  
  79.     c = ff(c, d, a, b, x[i+10], 17, -42063)  
  80.     b = ff(b, c, d, a, x[i+11], 22, -1990404162)  
  81.     a = ff(a, b, c, d, x[i+12], 7 ,  1804603682)  
  82.     d = ff(d, a, b, c, x[i+13], 12, -40341101)  
  83.     c = ff(c, d, a, b, x[i+14], 17, -1502002290)  
  84.     b = ff(b, c, d, a, x[i+15], 22,  1236535329)  
  85.     a = gg(a, b, c, d, x[i+ 1], 5 , -165796510)  
  86.     d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632)  
  87.     c = gg(c, d, a, b, x[i+11], 14,  643717713)  
  88.     b = gg(b, c, d, a, x[i+ 0], 20, -373897302)  
  89.     a = gg(a, b, c, d, x[i+ 5], 5 , -701558691)  
  90.     d = gg(d, a, b, c, x[i+10], 9 ,  38016083)  
  91.     c = gg(c, d, a, b, x[i+15], 14, -660478335)  
  92.     b = gg(b, c, d, a, x[i+ 4], 20, -405537848)  
  93.     a = gg(a, b, c, d, x[i+ 9], 5 ,  568446438)  
  94.     d = gg(d, a, b, c, x[i+14], 9 , -1019803690)  
  95.     c = gg(c, d, a, b, x[i+ 3], 14, -187363961)  
  96.     b = gg(b, c, d, a, x[i+ 8], 20,  1163531501)  
  97.     a = gg(a, b, c, d, x[i+13], 5 , -1444681467)  
  98.     d = gg(d, a, b, c, x[i+ 2], 9 , -51403784)  
  99.     c = gg(c, d, a, b, x[i+ 7], 14,  1735328473)  
  100.     b = gg(b, c, d, a, x[i+12], 20, -1926607734)  
  101.     a = hh(a, b, c, d, x[i+ 5], 4 , -378558)  
  102.     d = hh(d, a, b, c, x[i+ 8], 11, -2022574463)  
  103.     c = hh(c, d, a, b, x[i+11], 16,  1839030562)  
  104.     b = hh(b, c, d, a, x[i+14], 23, -35309556)  
  105.     a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060)  
  106.     d = hh(d, a, b, c, x[i+ 4], 11,  1272893353)  
  107.     c = hh(c, d, a, b, x[i+ 7], 16, -155497632)  
  108.     b = hh(b, c, d, a, x[i+10], 23, -1094730640)  
  109.     a = hh(a, b, c, d, x[i+13], 4 ,  681279174)  
  110.     d = hh(d, a, b, c, x[i+ 0], 11, -358537222)  
  111.     c = hh(c, d, a, b, x[i+ 3], 16, -722521979)  
  112.     b = hh(b, c, d, a, x[i+ 6], 23,  76029189)  
  113.     a = hh(a, b, c, d, x[i+ 9], 4 , -640364487)  
  114.     d = hh(d, a, b, c, x[i+12], 11, -421815835)  
  115.     c = hh(c, d, a, b, x[i+15], 16,  530742520)  
  116.     b = hh(b, c, d, a, x[i+ 2], 23, -995338651)  
  117.     a = ii(a, b, c, d, x[i+ 0], 6 , -198630844)  
  118.     d = ii(d, a, b, c, x[i+ 7], 10,  1126891415)  
  119.     c = ii(c, d, a, b, x[i+14], 15, -1416354905)  
  120.     b = ii(b, c, d, a, x[i+ 5], 21, -57434055)  
  121.     a = ii(a, b, c, d, x[i+12], 6 ,  1700485571)  
  122.     d = ii(d, a, b, c, x[i+ 3], 10, -1894986606)  
  123.     c = ii(c, d, a, b, x[i+10], 15, -1051523)  
  124.     b = ii(b, c, d, a, x[i+ 1], 21, -2054922799)  
  125.     a = ii(a, b, c, d, x[i+ 8], 6 ,  1873313359)  
  126.     d = ii(d, a, b, c, x[i+15], 10, -30611744)  
  127.     c = ii(c, d, a, b, x[i+ 6], 15, -1560198380)  
  128.     b = ii(b, c, d, a, x[i+13], 21,  1309151649)  
  129.     a = ii(a, b, c, d, x[i+ 4], 6 , -145523070)  
  130.     d = ii(d, a, b, c, x[i+11], 10, -1120210379)  
  131.     c = ii(c, d, a, b, x[i+ 2], 15,  718787259)  
  132.     b = ii(b, c, d, a, x[i+ 9], 21, -343485551)  
  133.     a = safe_add(a, olda)  
  134.     b = safe_add(b, oldb)  
  135.     c = safe_add(c, oldc)  
  136.     d = safe_add(d, oldd)  
  137.   }  
  138.   return [a, b, c, d]  
  139. }  
  140.   
  141. /* 
  142.  * Convert an array of little-endian words to a hex string. 
  143.  */  
  144. function binl2hex(binarray)  
  145. {  
  146.   var hex_tab = "0123456789abcdef"  
  147.   var str = ""  
  148.   for(var i = 0; i < binarray.length * 4; i++)  
  149.   {  
  150.     str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +  
  151.            hex_tab.charAt((binarray[i>>2] >> ((i%4)*8)) & 0xF)  
  152.   }  
  153.   return str  
  154. }  
  155.   
  156. /* 
  157.  * Convert an array of little-endian words to a base64 encoded string. 
  158.  */  
  159. function binl2b64(binarray)  
  160. {  
  161.   var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"  
  162.   var str = ""  
  163.   for(var i = 0; i < binarray.length * 32; i += 6)  
  164.   {  
  165.     str += tab.charAt(((binarray[i>>5] << (i%32)) & 0x3F) |  
  166.                       ((binarray[i>>5+1] >> (32-i%32)) & 0x3F))  
  167.   }  
  168.   return str  
  169. }  
  170.   
  171. /* 
  172.  * Convert an 8-bit character string to a sequence of 16-word blocks, stored 
  173.  * as an array, and append appropriate padding for MD4/5 calculation. 
  174.  * If any of the characters are >255, the high byte is silently ignored. 
  175.  */  
  176. function str2binl(str)  
  177. {  
  178.   var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocks  
  179.   var blks = new Array(nblk * 16)  
  180.   for(var i = 0; i < nblk * 16; i++) blks[i] = 0  
  181.   for(var i = 0; i < str.length; i++)  
  182.     blks[i>>2] |= (str.charCodeAt(i) & 0xFF) << ((i%4) * 8)  
  183.   blks[i>>2] |= 0x80 << ((i%4) * 8)  
  184.   blks[nblk*16-2] = str.length * 8  
  185.   return blks  
  186. }  
  187.   
  188. /* 
  189.  * Convert a wide-character string to a sequence of 16-word blocks, stored as 
  190.  * an array, and append appropriate padding for MD4/5 calculation. 
  191.  */  
  192. function strw2binl(str)  
  193. {  
  194.   var nblk = ((str.length + 4) >> 5) + 1 // number of 16-word blocks  
  195.   var blks = new Array(nblk * 16)  
  196.   for(var i = 0; i < nblk * 16; i++) blks[i] = 0  
  197.   for(var i = 0; i < str.length; i++)  
  198.     blks[i>>1] |= str.charCodeAt(i) << ((i%2) * 16)  
  199.   blks[i>>1] |= 0x80 << ((i%2) * 16)  
  200.   blks[nblk*16-2] = str.length * 16  
  201.   return blks  
  202. }  
  203.   
  204. /* 
  205.  * External interface 
  206.  */  
  207. function hexMD5 (str) { return binl2hex(coreMD5( str2binl(str))) }  
  208. function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) }  
  209. function b64MD5 (str) { return binl2b64(coreMD5( str2binl(str))) }  
  210. function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) }  
  211. /* Backward compatibility */  
  212. function calcMD5(str) { return binl2hex(coreMD5( str2binl(str))) }  

md5.html页面程序如下:

[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <html>  
  2. <head>  
  3. <script language='javascript' src='md5.js'></script><!--引入MD5加密-->  
  4. <script language="javascript">  
  5. <!--  
  6. function my_do()  
  7. //通过js对数据进行加密  
  8.  {  
  9.   //var URL="http://127.0.0.1/index.jsp?";  
  10.   //var dqz_out="";  
  11.   var dqz_in=document.my_form1.my_in_1.value;  
  12.  document.my_form1.my_out_1.value=hexMD5(dqz_in);  
  13.  document.my_form1.my_out_2.value=hexMD5w(dqz_in);  
  14.  document.my_form1.my_out_3.value=b64MD5(dqz_in);  
  15.  document.my_form1.my_out_4.value=b64MD5w(dqz_in);  
  16.  //URL=URL+"in="+dqz_in+"&out="+dqz_out;  
  17.  //my_form1.action = URL;  
  18.  //window.open(URL,'','');  
  19.  //my_form1.submit();  
  20.   }  
  21. //-->  
  22. </script>  
  23. </head>  
  24. <body>  
  25. <form name="my_form1" method="get" action="">  
  26.   <p>计算MD5加密结果的例子<br>  
  27.     请输入加密内容:  
  28.       <textarea name="my_in_1" cols="70" rows="10">1234567890</textarea>  
  29.     <br>  
  30.     点击右边的文本框显示结果:<br><!--mouseup某个鼠标按键被松开,mouseout鼠标从某个元素移开-->  
  31. hexMD5()= <input name="my_out_1" type="text" onChange="my_do()" onMouseUp="my_do()" onMouseOut="my_do()" size="50" maxlength="50"><br>   
  32. hexMD5w()=<input name="my_out_2" type="text" onChange="my_do()" onMouseUp="my_do()" onMouseOut="my_do()" size="50" maxlength="50"><br>  
  33. b64MD5()= <input name="my_out_3" type="text" onChange="my_do()" onMouseUp="my_do()" onMouseOut="my_do()" size="50" maxlength="50"><br>  
  34. b64MD5w()=<input name="my_out_4" type="text" onChange="my_do()" onMouseUp="my_do()" onMouseOut="my_do()" size="50" maxlength="50"><br>  
  35.     <br>  
  36.   </p>  
  37. </form>  
  38. </body>  
  39. </html>  
md5.js加密效果如图:


下面介绍微信小程序如何加密——模块化

     我们可以将一些公共的代码抽离成为一个单独的 js 文件,作为一个模块。模块只有通过 module.exports 或者 exports 才能对外暴露接口。需要注意的是:
    (1)、 exports 是 module.exports 的一个引用,因此在模块里边随意更改 exports 的指向会造成未知的错误。所以我们更推荐开发者采用 module.exports 来暴露模块接口,除非你已经清晰知道这两者的关系。
     (2)、小程序目前不支持直接引入 node_modules , 开发者需要使用到 node_modules 时候建议拷贝出相关的代码到小程序的目录中。

[javascript] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. // common.js  
  2. function sayHello(name) {  
  3.   console.log(`Hello ${name} !`)  
  4. }  
  5. function sayGoodbye(name) {  
  6.   console.log(`Goodbye ${name} !`)  
  7. }  
  8. module.exports.sayHello = sayHello  
  9. exports.sayGoodbye = sayGoodbye  
​在需要使用这些模块的文件中,使用 require(path) 将公共代码引入
[javascript] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. var common = require('common.js')  
  2. Page({  
  3.   helloMINA: function() {  
  4.     common.sayHello('MINA')  
  5.   },  
  6.   goodbyeMINA: function() {  
  7.     common.sayGoodbye('MINA')  
  8.   }  
  9. })  
 仿照模块化方法我们可以加入MD5.js加密:

md5.js程序如下:

[javascript] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message 
  3.  * Digest Algorithm, as defined in RFC 1321. 
  4.  * Version 1.1 Copyright (C) Paul Johnston 1999 - 2002. 
  5.  * Code also contributed by Greg Holt 
  6.  * See http://pajhome.org.uk/site/legal.html for details. 
  7.  */  
  8.   
  9. /* 
  10.  * Add integers, wrapping at 2^32. This uses 16-bit operations internally 
  11.  * to work around bugs in some JS interpreters. 
  12.  */  
  13. function safe_add(x, y)  
  14. {  
  15.   var lsw = (x & 0xFFFF) + (y & 0xFFFF)  
  16.   var msw = (x >> 16) + (y >> 16) + (lsw >> 16)  
  17.   return (msw << 16) | (lsw & 0xFFFF)  
  18. }  
  19.   
  20. /* 
  21.  * Bitwise rotate a 32-bit number to the left. 
  22.  */  
  23. function rol(num, cnt)  
  24. {  
  25.   return (num << cnt) | (num >>> (32 - cnt))  
  26. }  
  27.   
  28. /* 
  29.  * These functions implement the four basic operations the algorithm uses. 
  30.  */  
  31. function cmn(q, a, b, x, s, t)  
  32. {  
  33.   return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b)  
  34. }  
  35. function ff(a, b, c, d, x, s, t)  
  36. {  
  37.   return cmn((b & c) | ((~b) & d), a, b, x, s, t)  
  38. }  
  39. function gg(a, b, c, d, x, s, t)  
  40. {  
  41.   return cmn((b & d) | (c & (~d)), a, b, x, s, t)  
  42. }  
  43. function hh(a, b, c, d, x, s, t)  
  44. {  
  45.   return cmn(b ^ c ^ d, a, b, x, s, t)  
  46. }  
  47. function ii(a, b, c, d, x, s, t)  
  48. {  
  49.   return cmn(c ^ (b | (~d)), a, b, x, s, t)  
  50. }  
  51.   
  52. /* 
  53.  * Calculate the MD5 of an array of little-endian words, producing an array 
  54.  * of little-endian words. 
  55.  */  
  56. function coreMD5(x)  
  57. {  
  58.   var a =  1732584193  
  59.   var b = -271733879  
  60.   var c = -1732584194  
  61.   var d =  271733878  
  62.   
  63.   for(var i = 0; i < x.length; i += 16)  
  64.   {  
  65.     var olda = a  
  66.     var oldb = b  
  67.     var oldc = c  
  68.     var oldd = d  
  69.   
  70.     a = ff(a, b, c, d, x[i+ 0], 7 , -680876936)  
  71.     d = ff(d, a, b, c, x[i+ 1], 12, -389564586)  
  72.     c = ff(c, d, a, b, x[i+ 2], 17,  606105819)  
  73.     b = ff(b, c, d, a, x[i+ 3], 22, -1044525330)  
  74.     a = ff(a, b, c, d, x[i+ 4], 7 , -176418897)  
  75.     d = ff(d, a, b, c, x[i+ 5], 12,  1200080426)  
  76.     c = ff(c, d, a, b, x[i+ 6], 17, -1473231341)  
  77.     b = ff(b, c, d, a, x[i+ 7], 22, -45705983)  
  78.     a = ff(a, b, c, d, x[i+ 8], 7 ,  1770035416)  
  79.     d = ff(d, a, b, c, x[i+ 9], 12, -1958414417)  
  80.     c = ff(c, d, a, b, x[i+10], 17, -42063)  
  81.     b = ff(b, c, d, a, x[i+11], 22, -1990404162)  
  82.     a = ff(a, b, c, d, x[i+12], 7 ,  1804603682)  
  83.     d = ff(d, a, b, c, x[i+13], 12, -40341101)  
  84.     c = ff(c, d, a, b, x[i+14], 17, -1502002290)  
  85.     b = ff(b, c, d, a, x[i+15], 22,  1236535329)  
  86.   
  87.     a = gg(a, b, c, d, x[i+ 1], 5 , -165796510)  
  88.     d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632)  
  89.     c = gg(c, d, a, b, x[i+11], 14,  643717713)  
  90.     b = gg(b, c, d, a, x[i+ 0], 20, -373897302)  
  91.     a = gg(a, b, c, d, x[i+ 5], 5 , -701558691)  
  92.     d = gg(d, a, b, c, x[i+10], 9 ,  38016083)  
  93.     c = gg(c, d, a, b, x[i+15], 14, -660478335)  
  94.     b = gg(b, c, d, a, x[i+ 4], 20, -405537848)  
  95.     a = gg(a, b, c, d, x[i+ 9], 5 ,  568446438)  
  96.     d = gg(d, a, b, c, x[i+14], 9 , -1019803690)  
  97.     c = gg(c, d, a, b, x[i+ 3], 14, -187363961)  
  98.     b = gg(b, c, d, a, x[i+ 8], 20,  1163531501)  
  99.     a = gg(a, b, c, d, x[i+13], 5 , -1444681467)  
  100.     d = gg(d, a, b, c, x[i+ 2], 9 , -51403784)  
  101.     c = gg(c, d, a, b, x[i+ 7], 14,  1735328473)  
  102.     b = gg(b, c, d, a, x[i+12], 20, -1926607734)  
  103.   
  104.     a = hh(a, b, c, d, x[i+ 5], 4 , -378558)  
  105.     d = hh(d, a, b, c, x[i+ 8], 11, -2022574463)  
  106.     c = hh(c, d, a, b, x[i+11], 16,  1839030562)  
  107.     b = hh(b, c, d, a, x[i+14], 23, -35309556)  
  108.     a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060)  
  109.     d = hh(d, a, b, c, x[i+ 4], 11,  1272893353)  
  110.     c = hh(c, d, a, b, x[i+ 7], 16, -155497632)  
  111.     b = hh(b, c, d, a, x[i+10], 23, -1094730640)  
  112.     a = hh(a, b, c, d, x[i+13], 4 ,  681279174)  
  113.     d = hh(d, a, b, c, x[i+ 0], 11, -358537222)  
  114.     c = hh(c, d, a, b, x[i+ 3], 16, -722521979)  
  115.     b = hh(b, c, d, a, x[i+ 6], 23,  76029189)  
  116.     a = hh(a, b, c, d, x[i+ 9], 4 , -640364487)  
  117.     d = hh(d, a, b, c, x[i+12], 11, -421815835)  
  118.     c = hh(c, d, a, b, x[i+15], 16,  530742520)  
  119.     b = hh(b, c, d, a, x[i+ 2], 23, -995338651)  
  120.   
  121.     a = ii(a, b, c, d, x[i+ 0], 6 , -198630844)  
  122.     d = ii(d, a, b, c, x[i+ 7], 10,  1126891415)  
  123.     c = ii(c, d, a, b, x[i+14], 15, -1416354905)  
  124.     b = ii(b, c, d, a, x[i+ 5], 21, -57434055)  
  125.     a = ii(a, b, c, d, x[i+12], 6 ,  1700485571)  
  126.     d = ii(d, a, b, c, x[i+ 3], 10, -1894986606)  
  127.     c = ii(c, d, a, b, x[i+10], 15, -1051523)  
  128.     b = ii(b, c, d, a, x[i+ 1], 21, -2054922799)  
  129.     a = ii(a, b, c, d, x[i+ 8], 6 ,  1873313359)  
  130.     d = ii(d, a, b, c, x[i+15], 10, -30611744)  
  131.     c = ii(c, d, a, b, x[i+ 6], 15, -1560198380)  
  132.     b = ii(b, c, d, a, x[i+13], 21,  1309151649)  
  133.     a = ii(a, b, c, d, x[i+ 4], 6 , -145523070)  
  134.     d = ii(d, a, b, c, x[i+11], 10, -1120210379)  
  135.     c = ii(c, d, a, b, x[i+ 2], 15,  718787259)  
  136.     b = ii(b, c, d, a, x[i+ 9], 21, -343485551)  
  137.   
  138.     a = safe_add(a, olda)  
  139.     b = safe_add(b, oldb)  
  140.     c = safe_add(c, oldc)  
  141.     d = safe_add(d, oldd)  
  142.   }  
  143.   return [a, b, c, d]  
  144. }  
  145.   
  146. /* 
  147.  * Convert an array of little-endian words to a hex string. 
  148.  */  
  149. function binl2hex(binarray)  
  150. {  
  151.   var hex_tab = "0123456789abcdef"  
  152.   var str = ""  
  153.   for(var i = 0; i < binarray.length * 4; i++)  
  154.   {  
  155.     str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +  
  156.            hex_tab.charAt((binarray[i>>2] >> ((i%4)*8)) & 0xF)  
  157.   }  
  158.   return str  
  159. }  
  160.   
  161. /* 
  162.  * Convert an array of little-endian words to a base64 encoded string. 
  163.  */  
  164. function binl2b64(binarray)  
  165. {  
  166.   var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"  
  167.   var str = ""  
  168.   for(var i = 0; i < binarray.length * 32; i += 6)  
  169.   {  
  170.     str += tab.charAt(((binarray[i>>5] << (i%32)) & 0x3F) |  
  171.                       ((binarray[i>>5+1] >> (32-i%32)) & 0x3F))  
  172.   }  
  173.   return str  
  174. }  
  175.   
  176. /* 
  177.  * Convert an 8-bit character string to a sequence of 16-word blocks, stored 
  178.  * as an array, and append appropriate padding for MD4/5 calculation. 
  179.  * If any of the characters are >255, the high byte is silently ignored. 
  180.  */  
  181. function str2binl(str)  
  182. {  
  183.   var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocks  
  184.   var blks = new Array(nblk * 16)  
  185.   for(var i = 0; i < nblk * 16; i++) blks[i] = 0  
  186.   for(var i = 0; i < str.length; i++)  
  187.     blks[i>>2] |= (str.charCodeAt(i) & 0xFF) << ((i%4) * 8)  
  188.   blks[i>>2] |= 0x80 << ((i%4) * 8)  
  189.   blks[nblk*16-2] = str.length * 8  
  190.   return blks  
  191. }  
  192.   
  193. /* 
  194.  * Convert a wide-character string to a sequence of 16-word blocks, stored as 
  195.  * an array, and append appropriate padding for MD4/5 calculation. 
  196.  */  
  197. function strw2binl(str)  
  198. {  
  199.   var nblk = ((str.length + 4) >> 5) + 1 // number of 16-word blocks  
  200.   var blks = new Array(nblk * 16)  
  201.   for(var i = 0; i < nblk * 16; i++) blks[i] = 0  
  202.   for(var i = 0; i < str.length; i++)  
  203.     blks[i>>1] |= str.charCodeAt(i) << ((i%2) * 16)  
  204.   blks[i>>1] |= 0x80 << ((i%2) * 16)  
  205.   blks[nblk*16-2] = str.length * 16  
  206.   return blks  
  207. }  
  208.   
  209. /* 
  210.  * External interface 
  211.  */  
  212. function hexMD5 (str) { return binl2hex(coreMD5( str2binl(str))) }  
  213. function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) }  
  214. function b64MD5 (str) { return binl2b64(coreMD5( str2binl(str))) }  
  215. function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) }  
  216. /* Backward compatibility */  
  217. function calcMD5(str) { return binl2hex(coreMD5( str2binl(str))) }  
  218. module.exports = {  
  219.   hexMD5: hexMD5  

使用程序如下:

[javascript] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. //index.js  
  2. var util = require('../../utils/md5.js')  
  3. //获取应用实例  
  4. var app = getApp()  
  5. Page({  
  6.   data: {  
  7.     page:'1',  
  8.   },  
  9.    var password=value.password;  
  10.    if(password===""||password===null){  
  11.        wx.showModal({  
  12.             title:'提示',  
  13.             content: '密码不能为空',  
  14.             confirmColor:'#118EDE',  
  15.             showCancel: false,  
  16.             success: function (res) {  
  17.                 if (res.confirm) {  
  18.                     //console.log('用户点击确定')  
  19.                 }  
  20.             }  
  21.         });  
  22.         return false;  
  23.     }else{  
  24.         password=util.hexMD5(password);  
  25.     }  
  26. })  


0 0
原创粉丝点击