您当前的位置: 首页 > 知识百科 > 微信小程序md5加密怎么做?

微信小程序md5加密怎么做?

时间:2023-07-01 14:05 阅读数:75 人阅读 分类:知识百科

  微信小程序md5加密怎么做?微信小程序md5加密了以后更加的安全一些,微信小程序用户问小编微信小程序md5加密怎么做,以下就是小编整理的微信小程序md5加密怎么做的全部内容了哦。

  微信小程序md5加密怎么做?

  在小程序中,页面的脚本逻辑是在JsCore中运行,JsCore是一个没有窗口对象的环境,所以不能在脚本中使用window,也无法在脚本中操作组件。

  zepto/jquery 也无法使用,因为zepto/jquery 会使用到window对象和document对象。所以在微信小程序中不能使用jquery.md5.js对密码进行加密。

  下面我提供一种MD5.js加密实例,本实例是静态演示。

  微信小程序md5加密小程序js如下:

  [javascript] view plain copy/*

  * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message

  * Digest Algorithm, as defined in RFC 1321.

  * Version 1.1 Copyright (C) Paul Johnston 1999 - 2002.

  * Code also contributed by Greg Holt

  * See pajhome.org.uk/site/legal.html for details.

  * Add integers, wrapping at 2^32. This uses 16-bit operations internally

  * to work around bugs in some JS interpreters.

  function safe_add(x, y)

  var lsw = (x & 0xFFFF) + (y & 0xFFFF)

  var msw = (x >> 16) + (y >> 16) + (lsw >> 16)

  return (msw << 16) | (lsw & 0xFFFF)

  }

  /*

  * Bitwise rotate a 32-bit number to the left.

  */

  function rol(num, cnt)

  {

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

  }

  /*

  * These functions implement the four basic operations the algorithm uses.

  */

  function cmn(q, a, b, x, s, t)

  {

  return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b)

  }

  function ff(a, b, c, d, x, s, t)

  {

  return cmn((b & c) | ((~b) & d), a, b, x, s, t)

  }

  function gg(a, b, c, d, x, s, t)

  {

  return cmn((b & d) | (c & (~d)), a, b, x, s, t)

  }

  function hh(a, b, c, d, x, s, t)

  {

  return cmn(b ^ c ^ d, a, b, x, s, t)

  }

  function ii(a, b, c, d, x, s, t)

  {

  return cmn(c ^ (b | (~d)), a, b, x, s, t)

  }

  /*

  * Calculate the MD5 of an array of little-endian words, producing an array

  * of little-endian words.

  */

  function coreMD5(x)

  {

  var a = 1732584193

  var b = -271733879

  var c = -1732584194

  var d = 271733878

  for(i = 0; i < x.length; i += 16)

  {

  var olda = a

  var oldb = b

  var oldc = c

  var oldd = d

  }

  return [a, b, c, d]

  }

  /*

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

  */

  function binl2hex(binarray)

  {

  var hex_tab = "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

  }

  /*

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

  */

  function binl2b64(binarray)

  {

  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

  var str = ""

  for(var i = 0; i < binarray.length * 32; i += 6)

  {

  str += tab.charAt(((binarray[i>>5] << (i%32)) & 0x3F) |

  ((binarray[i>>5+1] >> (32-i%32)) & 0x3F))

  }

  return str

  }

  /*

  * Convert an 8-bit character string to a sequence of 16-word blocks, stored

  * as an array, and append appropriate padding for MD4/5 calculation.

  * If any of the characters are >255, the high byte is silently ignored.

  */

  function str2binl(str)

  {

  var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocks

  var blks = new Array(nblk * 16)

  for(var i = 0; i < nblk * 16; i++) blks[i] = 0

  for(var i = 0; i < str.length; i++)

  blks[i>>2] |= (str.charCodeAt(i) & 0xFF) << ((i%4) * 8)

  blks[i>>2] |= 0x80 << ((i%4) * 8)

  blks[nblk*16-2] = str.length * 8

  return blks

  * Convert a wide-character string to a sequence of 16-word blocks, stored as

  * an array, and append appropriate padding for MD4/5 calculation.

  var nblk = ((str.length + 4) >> 5) + 1 // number of 16-word blocks

  var blks = new Array(nblk * 16)

  for(var i = 0; i < nblk * 16; i++) blks[i] = 0

  for(var i = 0; i < str.length; i++)

  blks[i>>1] |= str.charCodeAt(i) << ((i%2) * 16)

  blks[i>>1] |= 0x80 << ((i%2) * 16)

  blks[nblk*16-2] = str.length * 16

  return blks

  md5.js加密效果如图:

  

  微信小程序md5加密怎么做?以上就是微信小程序md5加密怎么做的内容了。微信小程序md5加密实现以后的效果怎么样?大家应该都已经看到了吧!以上全部内容供大家参考,大家请多关注微小乔。

  

  微信小程序数据安全吗?关于小程序的几个秘密

  微信小程序怎么设置安全代理?

  微信小程序多选标签怎么开发