微信小程序中的MD5加密

本文轉載自 http://blog.csdn.net/qq_27626333/article/details/53634897

下面介紹微信小程序如何加密——模塊化

     我們可以將一些公共的代碼抽離成爲一個單獨的 js 文件,作爲一個模塊。模塊只有通過 module.exports 或者 exports 才能對外暴露接口。需要注意的是:
    (1)、 exports 是 module.exports 的一個引用,因此在模塊裏邊隨意更改 exports 的指向會造成未知的錯誤。所以我們更推薦開發者採用 module.exports 來暴露模塊接口,除非你已經清晰知道這兩者的關係。
     (2)、小程序目前不支持直接引入 node_modules , 開發者需要使用到 node_modules 時候建議拷貝出相關的代碼到小程序的目錄中。

[javascript] view plain copy
  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
  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
  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    
  220. }    
使用程序如下:

[javascript] view plain copy
  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. })    

utils目錄

utils目錄放的是我們工具腳本文件,我們在操作頁面或者解析數據的時候常常需要寫一些方法,通常是寫在我們各自頁面.js文件中或者是全局的app.js頁面中,我們通常將一些複雜的邏輯代碼抽取出來放在一個文件中封裝成一個個函數,這樣可以實現代碼的模塊化,也可以大量減少編寫重複功能代碼這類的工作。

把公共的方法封裝起來,然後使用時需要先引入這個工具js文件,例如在系統給我們創建的小實例中,logs頁面中的腳本文件logs.js需要引用utils目錄下util.js中的formatTime()方法,使用前他需要使用var util = require('../../utils/util.js')引入util.js文件:

[javascript] view plain copy
  1. //logs.js    
  2. var util = require('../../utils/util.js')    
  3. Page({    
  4.   data: {    
  5.     logs: []    
  6.   },    
  7.   onLoad: function () {    
  8.     this.setData({    
  9.       logs: (wx.getStorageSync('logs') || []).map(function (log) {    
  10.         return util.formatTime(new Date(log))    
  11.       })    
  12.     })    
  13.   }    
  14. })    

引入之後直接使用

[javascript] view plain copy
  1. util.formatTime(new Date(log))    
來使用這個方法並傳入參數即可。
發佈了98 篇原創文章 · 獲贊 70 · 訪問量 46萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章