小程序使用MD5加密

小程序开发会遇到权限控制问题,有时会使用MD5对openid等进行加密,现在简单介绍一下MD5的加密过程

1、将md5.js放入utils文件夹中

md5.js:

/*

* 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 http://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 (var i = 0; i < x.length; i += 16) {

    var olda = a

    va
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值