hmac-sha256的纯js写法,无需引入外部模块,引入就可以直接用 (微信小程序先享后付,HMAC-SHA256加密算法)
在html里面的用法↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 具体用法
// var hmac256 = require("../../../utils/hmac256.js");
// // 先充后付
// var mch_id = '1514221471';
// var nonce_str = utils.randomString();
// var package1 = res.data.data;
// var sign_type = 'HMAC-SHA256';
// var timestamp = utils.getTimestamp();
// // 因为此时package为1
// var stringSign = 'mch_id=' + mch_id + '&nonce_str=' + nonce_str + '&package=' + package1 + + '&sign_type=' + sign_type + '×tamp=' + timestamp + '&key=186226E976CF3537BABFCCDFCA8B2D0D';
// -----真正用法
// var hash = hmac256.hex(hmac256.sign( "186226E976CF3537BABFCCDFCA8B2D0D",stringSign)).toUpperCase();
// console.log('stringSign...' , stringSign)
// console.log('hash...' , hash)
需要引入的hmac-sha256.js文件↓↓↓↓↓↓↓↓↓↓↓↓↓
// To ensure cross-browser support even without a proper SubtleCrypto
// impelmentation (or without access to the impelmentation, as is the case with
// Chrome loaded over HTTP instead of HTTPS), this library can create SHA-256
// HMAC signatures using nothing but raw JavaScript
/* eslint-disable no-magic-numbers, id-length, no-param-reassign, new-cap */
// By giving internal functions names that we can mangle, future calls to
// them are reduced to a single byte (minor space savings in minified file)
var uint8Array = Uint8Array;
var uint32Array = Uint32Array;
var pow = Math.pow;
// Will be initialized below
// Using a Uint32Array instead of a simple array makes the minified code
// a bit bigger (we lose our `unshift()` hack), but comes with huge
// performance gains
var DEFAULT_STATE = new uint32Array(8);
var ROUND_CONSTANTS = [];
// Reusable object for expanded message
// Using a Uint32Array instead of a simple array makes