微信小程序 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(/\r\n/g, "\n");
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     })

原文地址:https://www.cnblogs.com/iblackly/p/12632211.html

时间: 2024-11-08 22:50:46

微信小程序 MD5引用的相关文章

微信小程序开发提升效率

http://www.ifanr.com/minapp/790017 微信小程序的 API 实现需要兼顾方方面面,所以仍然使用 callback 写法. 众所周知,Callback-Hell(回调地狱)是传统 JS 语法上的历史问题.但毕竟称手的工具是开发效率的源泉,因此笔者对当前版本的微信小程序 API 做了简单的封装--weapp. 同时,微信小程序框架本身专注于交互和 UI 的实现,并未提供内置的状态管理.如果众多的异步操作都直接在 App 或 Page 中一一实现,相信开发起来会很困难,

微信小程序 使用HMACSHA1和md5为登陆注册报文添加指纹验证签名

对接口请求报文作指纹验证签名相信在开发中经常碰到, 这次在与java后端一起开发小程序时,就碰到需求对登陆注册请求报文添加指纹验证签名来防止信息被修改 先来看下我们与后端定制签名规则 2.4. 签名规则 原文规则:采用标准的JSON格式,null值字段舍去,按照key值字符串升序排列 例如:{"appId":"1100310061380986","outTradeNo":"1515120685073","timest

微信小程序入门五: wxml文件引用、模版、生命周期

实例内容 wxml文件引用(include.import) 模版 小程序生命周期 实例一: include方式引用header.wxml文件 文件引用对于代码的重用非常重要,例如在web开发中我们可以将公用的header部分和footer等部分进行提取,然后在需要的地方进行引用. 微信小程序里面,是包含引用功能的--include.import.这两个引用文件的标签,使用基本差不多,这里先说一下include. 微信中的视图文件引用,引用过来的都是没有渲染的,基本类似于直接将引用过来的文件复制到

微信小程序视图层WXML_引用

微信小程序视图层WXML_小程序引用 微信小程序WXML提供两种文件引用方式import和include. import import可以在该文件中使用目标文件定义的template,如: 在微信小程序的item.wxml中定义了一个叫item的template: <!-- item.wxml --> <template name="item"> <text>{{text}}</text> </template> 在小程序的i

微信小程序实现支付功能

小程序支付,没有封装支付代码:直接上一段可用的流程代码吧:微信小程序支付官网文档有详细的说明,这里我就不再赘述啦:客户端js: wx.request({ url:'https://www.xxxx.com/api/payfee.php',//改成你自己的链接 header:{ 'Content-Type':'application/x-www-form-urlencoded' }, method:'POST', success:function(res){ console.log(res.dat

微信小程序学习指南

作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 微信小程序正式公测, 张小龙全面阐述小程序,定档1月9日上线(附90分钟演讲全文) ... 前言:新人第一坑,跳坑指南:修改后,必须保存:ctrl+S: 1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教

微信小程序 --- 完成小程序支付功能

最近开发小程序,一直在看小程序的支付.经过一天的努力,小程序支付功能最终实现了. 下面感谢 csdn 博主:千堆雪惹尘埃 发布的 " 小程序与php 实现微信支付 " 原文地址: http://blog.csdn.net/admin1008611/article/details/73240458 下面是我自己写的代码:使用的 thinkphp:其他的可以参考修改: 服务端: 获取用户的 openid: public function get_openid(){ $data = arra

php对接微信小程序支付

前言:这里我就假装你已经注册了微信小程序,并且基本的配置都已经好了.注: 个人注册小程序不支持微信支付,所以我还是假装你是企业或者个体工商户的微信小程序,其他的商户号注册,二者绑定,授权,支付开通,就阅读文档吧,这里我先负责实战. 微信小程序支付开发文档: https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_3&index=1 基本流程: 1. 申请商户平台账号 https://pay.weixin.qq.com/in

微信小程序&amp;PHP 微信支付接口调用

小程序端 /** * 微信支付接口 */ wxPaymoney:function (out_trade_no, true_money){ //out_trade_no 后台统一下单接口需要用 var that = this wx.hideToast() //隐藏toast wx.request({ method: 'POST', data: { openid: '************',    //调用人的openid out_trade_no: out_trade_no, body: '答