Jsencrypt
修改版本的 jsencrypt
使其兼容 uni-app 和 小程序端。同步 jsencrypt 更新。
修改描述
处理了只有浏览器环境下才能使用的变量
- window
- document
- navigator
module 编译类型输出目标改为 ES2015
,方便项目打包,去除 rollup 打包时出现的 this 警告
src/lib/jsbn/rng.ts
if (window.crypto && window.crypto.getRandomValues) {
// Extract entropy (2048 bits) from RNG if available
const z = new Uint32Array(256)
window.crypto.getRandomValues(z)
for (t = 0; t < z.length; ++t) {
rng_pool[rng_pptr++] = z[t] & 255
}
}
// Use mouse events for entropy, if we do not have enough entropy by the time
// we need it, entropy will be generated by Math.random.
var count = 0
const onMouseMoveListener = function (ev: Event & { x: number; y: number }) {
count = count || 0
if (count >= 256 || rng_pptr >= rng_psize) {
if (window.removeEventListener) {
window.removeEventListener('mousemove', onMouseMoveListener, false)
} else if ((window as any).detachEvent) {
;(window as any).detachEvent('onmousemove', onMouseMoveListener)
}
return
}
try {
const mouseCoordinates = ev.x + ev.y
rng_pool[rng_pptr++] = mouseCoordinates & 255
count += 1
} catch (e) {
// Sometimes Firefox will deny permission to access event properties for some reason. Ignore.
}
}
if (window.addEventListener) {
window.addEventListener('mousemove', onMouseMoveListener, false)
} else if ((window as any).attachEvent) {
;(window as any).attachEvent('onmousemove', onMouseMoveListener)
}
修改后
const getRandomValues = function (array: Uint32Array) {
for (let i = 0, l = array.length; i < l; i++) {
array[i] = Math.floor(Math.random() * 256)
}
return array
}
const z = new Uint32Array(256)
getRandomValues(z)
for (t = 0; t < z.length; ++t) {
rng_pool[rng_pptr++] = z[t] & 255
}
src/lib/jsbn/jsbn.ts
// am: Compute w_j += (x*this_i), propagate carries,
// c is initial carry, returns final carry.
// c < 3*dvalue, x < 2*dvalue, this_i < dvalue
// We need to select the fastest one that works in this environment.
const inBrowser = typeof navigator !== "undefined";
if (inBrowser && j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
// am2 avoids a big mult-and-extract completely.
// Max digit bits should be <= 30 because we do bitwise ops
// on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
BigInteger.prototype.am = function am2(i:number, x:number, w:BigInteger, j:number, c:number, n:number) {
const xl = x & 0x7fff;
const xh = x >> 15;
while (--n >= 0) {
let l = this[i] & 0x7fff;
const h = this[i++] >> 15;
const m = xh * l + h * xl;
l = xl * l + ((m & 0x7fff) << 15) + w[j] + (c & 0x3fffffff);
c = (l >>> 30) + (m >>> 15) + xh * h + (c >>> 30);
w[j++] = l & 0x3fffffff;
}
return c;
};
dbits = 30;
} else if (inBrowser && j_lm && (navigator.appName != "Netscape")) {
// am1: use a single mult and divide to get the high bits,
// max digit bits should be 26 because
// max internal value = 2*dvalue^2-2*dvalue (< 2^53)
BigInteger.prototype.am = function am1(i:number, x:number, w:BigInteger, j:number, c:number, n:number) {
while (--n >= 0) {
const v = x * this[i++] + w[j] + c;
c = Math.floor(v / 0x4000000);
w[j++] = v & 0x3ffffff;
}
return c;
};
dbits = 26;
} else { // Mozilla/Netscape seems to prefer am3
// Alternately, set max digit bits to 28 since some
// browsers slow down when dealing with 32-bit numbers.
BigInteger.prototype.am = function am3(i:number, x:number, w:BigInteger, j:number, c:number, n:number) {
const xl = x & 0x3fff;
const xh = x >> 14;
while (--n >= 0) {
let l = this[i] & 0x3fff;
const h = this[i++] >> 14;
const m = xh * l + h * xl;
l = xl * l + ((m & 0x3fff) << 14) + w[j] + c;
c = (l >> 28) + (m >> 14) + xh * h;
w[j++] = l & 0xfffffff;
}
return c;
};
dbits = 28;
}
修改后
BigInteger.prototype.am = function am3(i:number, x:number, w:BigInteger, j:number, c:number, n:number) {
const xl = x & 0x3fff;
const xh = x >> 14;
while (--n >= 0) {
let l = this[i] & 0x3fff;
const h = this[i++] >> 14;
const m = xh * l + h * xl;
l = xl * l + ((m & 0x3fff) << 14) + w[j] + c;
c = (l >> 28) + (m >> 14) + xh * h;
w[j++] = l & 0xfffffff;
}
return c;
};
dbits = 28;
tsconfig-def.json
{
"compilerOptions": {
"target": "es5",
}
修改后
{
"compilerOptions": {
"target": "ES2015",
}
tsconfig.json
{
"compilerOptions": {
"target": "es5",
}
修改后
{
"compilerOptions": {
"target": "ES2015",
}