微信小程序 MD5引用

Posted iblackly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信小程序 MD5引用相关的知识,希望对你有一定的参考价值。

模块化:https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/module.html

 

MD5.JS

  1 function md5(string) {
  2   function md5_RotateLeft(lValue, iShiftBits) {
  3     return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
  4   }
  5 
  6   function md5_AddUnsigned(lX, lY) {
  7     var lX4, lY4, lX8, lY8, lResult;
  8     lX8 = (lX & 0x80000000);
  9     lY8 = (lY & 0x80000000);
 10     lX4 = (lX & 0x40000000);
 11     lY4 = (lY & 0x40000000);
 12     lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
 13     if (lX4 & lY4) {
 14       return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
 15     }
 16     if (lX4 | lY4) {
 17       if (lResult & 0x40000000) {
 18         return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
 19       } else {
 20         return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
 21       }
 22     } else {
 23       return (lResult ^ lX8 ^ lY8);
 24     }
 25   }
 26 
 27   function md5_F(x, y, z) {
 28     return (x & y) | ((~x) & z);
 29   }
 30 
 31   function md5_G(x, y, z) {
 32     return (x & z) | (y & (~z));
 33   }
 34 
 35   function md5_H(x, y, z) {
 36     return (x ^ y ^ z);
 37   }
 38 
 39   function md5_I(x, y, z) {
 40     return (y ^ (x | (~z)));
 41   }
 42 
 43   function md5_FF(a, b, c, d, x, s, ac) {
 44     a = md5_AddUnsigned(a, md5_AddUnsigned(md5_AddUnsigned(md5_F(b, c, d), x), ac));
 45     return md5_AddUnsigned(md5_RotateLeft(a, s), b);
 46   };
 47 
 48   function md5_GG(a, b, c, d, x, s, ac) {
 49     a = md5_AddUnsigned(a, md5_AddUnsigned(md5_AddUnsigned(md5_G(b, c, d), x), ac));
 50     return md5_AddUnsigned(md5_RotateLeft(a, s), b);
 51   };
 52 
 53   function md5_HH(a, b, c, d, x, s, ac) {
 54     a = md5_AddUnsigned(a, md5_AddUnsigned(md5_AddUnsigned(md5_H(b, c, d), x), ac));
 55     return md5_AddUnsigned(md5_RotateLeft(a, s), b);
 56   };
 57 
 58   function md5_II(a, b, c, d, x, s, ac) {
 59     a = md5_AddUnsigned(a, md5_AddUnsigned(md5_AddUnsigned(md5_I(b, c, d), x), ac));
 60     return md5_AddUnsigned(md5_RotateLeft(a, s), b);
 61   };
 62 
 63   function md5_ConvertToWordArray(string) {
 64     var lWordCount;
 65     var lMessageLength = string.length;
 66     var lNumberOfWords_temp1 = lMessageLength + 8;
 67     var lNumberOfWords_temp2 = (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64;
 68     var lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16;
 69     var lWordArray = Array(lNumberOfWords - 1);
 70     var lBytePosition = 0;
 71     var lByteCount = 0;
 72     while (lByteCount < lMessageLength) {
 73       lWordCount = (lByteCount - (lByteCount % 4)) / 4;
 74       lBytePosition = (lByteCount % 4) * 8;
 75       lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount) << lBytePosition));
 76       lByteCount++;
 77     }
 78     lWordCount = (lByteCount - (lByteCount % 4)) / 4;
 79     lBytePosition = (lByteCount % 4) * 8;
 80     lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition);
 81     lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
 82     lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
 83     return lWordArray;
 84   };
 85 
 86   function md5_WordToHex(lValue) {
 87     var WordToHexValue = "",
 88       WordToHexValue_temp = "",
 89       lByte, lCount;
 90     for (lCount = 0; lCount <= 3; lCount++) {
 91       lByte = (lValue >>> (lCount * 8)) & 255;
 92       WordToHexValue_temp = "0" + lByte.toString(16);
 93       WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length - 2, 2);
 94     }
 95     return WordToHexValue;
 96   };
 97 
 98   function md5_Utf8Encode(string) {
 99     string = string.replace(/
/g, "
");
100     var utftext = "";
101     for (var n = 0; n < string.length; n++) {
102       var c = string.charCodeAt(n);
103       if (c < 128) {
104         utftext += String.fromCharCode(c);
105       } else if ((c > 127) && (c < 2048)) {
106         utftext += String.fromCharCode((c >> 6) | 192);
107         utftext += String.fromCharCode((c & 63) | 128);
108       } else {
109         utftext += String.fromCharCode((c >> 12) | 224);
110         utftext += String.fromCharCode(((c >> 6) & 63) | 128);
111         utftext += String.fromCharCode((c & 63) | 128);
112       }
113     }
114     return utftext;
115   };
116   var x = Array();
117   var k, AA, BB, CC, DD, a, b, c, d;
118   var S11 = 7,
119     S12 = 12,
120     S13 = 17,
121     S14 = 22;
122   var S21 = 5,
123     S22 = 9,
124     S23 = 14,
125     S24 = 20;
126   var S31 = 4,
127     S32 = 11,
128     S33 = 16,
129     S34 = 23;
130   var S41 = 6,
131     S42 = 10,
132     S43 = 15,
133     S44 = 21;
134   string = md5_Utf8Encode(string);
135   x = md5_ConvertToWordArray(string);
136   a = 0x67452301;
137   b = 0xEFCDAB89;
138   c = 0x98BADCFE;
139   d = 0x10325476;
140   for (k = 0; k < x.length; k += 16) {
141     AA = a;
142     BB = b;
143     CC = c;
144     DD = d;
145     a = md5_FF(a, b, c, d, x[k + 0], S11, 0xD76AA478);
146     d = md5_FF(d, a, b, c, x[k + 1], S12, 0xE8C7B756);
147     c = md5_FF(c, d, a, b, x[k + 2], S13, 0x242070DB);
148     b = md5_FF(b, c, d, a, x[k + 3], S14, 0xC1BDCEEE);
149     a = md5_FF(a, b, c, d, x[k + 4], S11, 0xF57C0FAF);
150     d = md5_FF(d, a, b, c, x[k + 5], S12, 0x4787C62A);
151     c = md5_FF(c, d, a, b, x[k + 6], S13, 0xA8304613);
152     b = md5_FF(b, c, d, a, x[k + 7], S14, 0xFD469501);
153     a = md5_FF(a, b, c, d, x[k + 8], S11, 0x698098D8);
154     d = md5_FF(d, a, b, c, x[k + 9], S12, 0x8B44F7AF);
155     c = md5_FF(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1);
156     b = md5_FF(b, c, d, a, x[k + 11], S14, 0x895CD7BE);
157     a = md5_FF(a, b, c, d, x[k + 12], S11, 0x6B901122);
158     d = md5_FF(d, a, b, c, x[k + 13], S12, 0xFD987193);
159     c = md5_FF(c, d, a, b, x[k + 14], S13, 0xA679438E);
160     b = md5_FF(b, c, d, a, x[k + 15], S14, 0x49B40821);
161     a = md5_GG(a, b, c, d, x[k + 1], S21, 0xF61E2562);
162     d = md5_GG(d, a, b, c, x[k + 6], S22, 0xC040B340);
163     c = md5_GG(c, d, a, b, x[k + 11], S23, 0x265E5A51);
164     b = md5_GG(b, c, d, a, x[k + 0], S24, 0xE9B6C7AA);
165     a = md5_GG(a, b, c, d, x[k + 5], S21, 0xD62F105D);
166     d = md5_GG(d, a, b, c, x[k + 10], S22, 0x2441453);
167     c = md5_GG(c, d, a, b, x[k + 15], S23, 0xD8A1E681);
168     b = md5_GG(b, c, d, a, x[k + 4], S24, 0xE7D3FBC8);
169     a = md5_GG(a, b, c, d, x[k + 9], S21, 0x21E1CDE6);
170     d = md5_GG(d, a, b, c, x[k + 14], S22, 0xC33707D6);
171     c = md5_GG(c, d, a, b, x[k + 3], S23, 0xF4D50D87);
172     b = md5_GG(b, c, d, a, x[k + 8], S24, 0x455A14ED);
173     a = md5_GG(a, b, c, d, x[k + 13], S21, 0xA9E3E905);
174     d = md5_GG(d, a, b, c, x[k + 2], S22, 0xFCEFA3F8);
175     c = md5_GG(c, d, a, b, x[k + 7], S23, 0x676F02D9);
176     b = md5_GG(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A);
177     a = md5_HH(a, b, c, d, x[k + 5], S31, 0xFFFA3942);
178     d = md5_HH(d, a, b, c, x[k + 8], S32, 0x8771F681);
179     c = md5_HH(c, d, a, b, x[k + 11], S33, 0x6D9D6122);
180     b = md5_HH(b, c, d, a, x[k + 14], S34, 0xFDE5380C);
181     a = md5_HH(a, b, c, d, x[k + 1], S31, 0xA4BEEA44);
182     d = md5_HH(d, a, b, c, x[k + 4], S32, 0x4BDECFA9);
183     c = md5_HH(c, d, a, b, x[k + 7], S33, 0xF6BB4B60);
184     b = md5_HH(b, c, d, a, x[k + 10], S34, 0xBEBFBC70);
185     a = md5_HH(a, b, c, d, x[k + 13], S31, 0x289B7EC6);
186     d = md5_HH(d, a, b, c, x[k + 0], S32, 0xEAA127FA);
187     c = md5_HH(c, d, a, b, x[k + 3], S33, 0xD4EF3085);
188     b = md5_HH(b, c, d, a, x[k + 6], S34, 0x4881D05);
189     a = md5_HH(a, b, c, d, x[k + 9], S31, 0xD9D4D039);
190     d = md5_HH(d, a, b, c, x[k + 12], S32, 0xE6DB99E5);
191     c = md5_HH(c, d, a, b, x[k + 15], S33, 0x1FA27CF8);
192     b = md5_HH(b, c, d, a, x[k + 2], S34, 0xC4AC5665);
193     a = md5_II(a, b, c, d, x[k + 0], S41, 0xF4292244);
194     d = md5_II(d, a, b, c, x[k + 7], S42, 0x432AFF97);
195     c = md5_II(c, d, a, b, x[k + 14], S43, 0xAB9423A7);
196     b = md5_II(b, c, d, a, x[k + 5], S44, 0xFC93A039);
197     a = md5_II(a, b, c, d, x[k + 12], S41, 0x655B59C3);
198     d = md5_II(d, a, b, c, x[k + 3], S42, 0x8F0CCC92);
199     c = md5_II(c, d, a, b, x[k + 10], S43, 0xFFEFF47D);
200     b = md5_II(b, c, d, a, x[k + 1], S44, 0x85845DD1);
201     a = md5_II(a, b, c, d, x[k + 8], S41, 0x6FA87E4F);
202     d = md5_II(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0);
203     c = md5_II(c, d, a, b, x[k + 6], S43, 0xA3014314);
204     b = md5_II(b, c, d, a, x[k + 13], S44, 0x4E0811A1);
205     a = md5_II(a, b, c, d, x[k + 4], S41, 0xF7537E82);
206     d = md5_II(d, a, b, c, x[k + 11], S42, 0xBD3AF235);
207     c = md5_II(c, d, a, b, x[k + 2], S43, 0x2AD7D2BB);
208     b = md5_II(b, c, d, a, x[k + 9], S44, 0xEB86D391);
209     a = md5_AddUnsigned(a, AA);
210     b = md5_AddUnsigned(b, BB);
211     c = md5_AddUnsigned(c, CC);
212     d = md5_AddUnsigned(d, DD);
213   }
214   return (md5_WordToHex(a) + md5_WordToHex(b) + md5_WordToHex(c) + md5_WordToHex(d)).toLowerCase();
215 }
216 module.exports.md5 = md5;

 

INDEX.JS

1     const cmd5 = require("md5.js");
2 
3     Page({
4       onLoad: function(){
5         console.log(cmd5.md5("xxx"));
6         //f561aaf6ef0bf14d4208bb46a4ccb3ad
7       }
8     })

 

以上是关于微信小程序 MD5引用的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序代码片段分享

怎样可以把微信的小程序加密

微信小程序json数据如何处理?

微信小程序下可以使用的MD5以及AES加密(通用)

微信小程序引入md5.js

微信小程序视图层WXML_模板