Current location - Quotes Website - Signature design - How to encrypt WeChat mini-programs
How to encrypt WeChat mini-programs

1. Download the MD5 source file (JS);

2. Use require in the mini program module to introduce external modules; you can also directly introduce md5.js globally in index.html document.

Because there is no team module in the source md5.js for output, if you use require, you need to export, so you need to add the following code to md5.js:

module.exports = {

p>

hexMD5: hex_md5, //The encryption algorithm that needs to be output, I only wrote the two I need here

b64Md5: b64_md5,

}

Use require in the js file to introduce md5:

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

Use:

let b64 = md5.b64Md5(code); //code needs to encrypt the data

The following is my file structure:

md5. js code is as follows;

/*

* 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 t)

{

return (num << cnt) | (num >>> ( 32 - cnt));

}

/*

* Convert a string to an array of little-endian words

* If chrsz is ASCII, characters >255 have their hi-byte silently ignored.

*/

function str2binl(str)

{

var bin = Array ();

var mask = ( 1 << chrsz) -

1;

for( var i =

0; i < str.length * chrsz; i += chrsz)

bin[i>> 5] |= (str.charCodeAt(i / chrsz) & mask) << (i% 32);< /p>

return bin;

}

/*

* Convert an array of little-endian words to a string

< p>*/

function binl2str(bin)

{

var str =

"";

var mask = ( 1 << chrsz) -

1;

for( var i =

0; i < bin.length *

32; i += chrsz)

str += String.fromCharCode((bin[i>> 5] >>> (i %

32)) & mask);

return str;

}

/*

* Convert an array of little-endian words to a hex string .

*/

function binl2hex(binarray)

{

var hex_tab = hexcase ?

" 0123456789ABCDEF" :

"0123456789abcdef";

var str =

"";

for( var i =

0; i < binarray.length *

4; i++)

{

str += hex_tab.charAt((binarray[i>> 2] >> ((i% 4)* 8+ 4)) &

0xF) +

hex_tab.charAt((binarray[i>> 2] >> (( i% 4)* 8 )) &

0xF);

}

return str;

}

< p>/*

* Convert an array of little-endian words to a base-64 string

*/

function binl2b64(binarray)

{

var tab =

"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

var str =

"";

for( var i =

0; i < binarray.length *

4; i +=

3)

< p>{

var triplet = (((binarray[i >>

2] >>

8 * ( i % 4)) &

0xFF) <<

16)

| (((binarray[i+ 1 >>

2] >>

8 * ((i+ 1)% 4)) &

0xFF) <<

8 )

| ((binarray[i+ 2 > >

2] >>

8 * ((i+ 2)% 4)) &

0xFF);

for( var j =

0; j <

4; j++)

{

if(i *

8 + j *

6 > binarray.length *

32) str += b64pad;

else str += tab.charAt((triplet > >

6*( 3-j)) &

0x3F);

}

}

return str;

}

module.exports = {

hexMD5: hex_md5,

b64Md5: b64_md5,

< p>}