微信小程序中使用MD5加密

本文介绍了如何在微信小程序中实现MD5加密。首先需要下载MD5的JS源文件,然后通过require引入到小程序模块中,或者全局引入到index.html。由于原始的md5.js没有导出模块,因此需要在md5.js内添加export语句以便在小程序中使用。
摘要由CSDN通过智能技术生成

1、下载MD5源文件(JS);

2、在小程序模块中使用require引入外部模块;也可以在index.html中直接全局引入md5.js文件。

因为源md5.js中没有队模块因为输出,如果使用require需要export,所以在md5.js中需要加入以下代码:

module.exports = {
hexMD5: hex_md5, //需要输出的加密算法,我这边只写了我需要得两种
b64Md5: b64_md5,
}

在js文件中使用require引入md5:

const md5 = require('../../assets/js/md5/md5.js');


使用:

let b64 = md5.b64Md5(code); //code需要加密的数据


下面是我的文件结构:

   

  md5.js 代码如下;

/*
* 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") ==
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值