From 36585e5956afe756eab04f7988f4300dd5a5af33 Mon Sep 17 00:00:00 2001 From: lchh0412 Date: Sat, 25 May 2024 00:11:33 +0700 Subject: [PATCH 1/2] replace bigint with node-int64 --- lib/Decrypt.js | 119 +++++++++++++++++++++++++++++++++++++++++++------ package.json | 3 +- 2 files changed, 108 insertions(+), 14 deletions(-) diff --git a/lib/Decrypt.js b/lib/Decrypt.js index 447d8af..0bbe5ae 100644 --- a/lib/Decrypt.js +++ b/lib/Decrypt.js @@ -1,66 +1,159 @@ -const bigInt = require('big-integer'); -const Stream = require('stream'); +const Int64 = require('node-int64'); + +let Stream = require('stream'); + + +// Backwards compatibility for node versions < 8 + +if (!Stream.Writable || !Stream.Writable.prototype.destroy) + + Stream = require('readable-stream'); + let table; + function generateTable() { + const poly = 0xEDB88320; let c, n, k; + table = []; + for (n = 0; n < 256; n++) { + c = n; + for (k = 0; k < 8; k++) + c = (c & 1) ? poly ^ (c >>> 1) : c = c >>> 1; + table[n] = c >>> 0; + } + } + function crc(ch, crc) { + if (!table) + generateTable(); + if (ch.charCodeAt) + ch = ch.charCodeAt(0); - return (bigInt(crc).shiftRight(8).and(0xffffff)).xor(table[bigInt(crc).xor(ch).and(0xff)]).value; + + const l = (crc.readUInt32BE() >> 8) & 0xffffff; + + const r = table[(crc.readUInt32BE() ^ (ch >>> 0)) & 0xff]; + + + return (l ^ r) >>> 0; + } + +function multiply(a, b) { + + const ah = (a >> 16) & 0xffff; + + const al = a & 0xffff; + + const bh = (b >> 16) & 0xffff; + + const bl = b & 0xffff; + + const high = (ah * bl + al * bh) & 0xffff; + + return ((high << 16) >>> 0) + al * bl; + +}; + + function Decrypt() { + if (!(this instanceof Decrypt)) + return new Decrypt(); - this.key0 = 305419896; - this.key1 = 591751049; - this.key2 = 878082192; + + this.key0 = Buffer.allocUnsafe(4); + + this.key1 = Buffer.allocUnsafe(4); + + this.key2 = Buffer.allocUnsafe(4); + + + this.key0.writeUInt32BE(0x12345678, 0); + + this.key1.writeUInt32BE(0x23456789, 0); + + this.key2.writeUInt32BE(0x34567890, 0); + } + Decrypt.prototype.update = function(h) { - this.key0 = crc(h, this.key0); - this.key1 = bigInt(this.key0).and(255).and(4294967295).add(this.key1); - this.key1 = bigInt(this.key1).multiply(134775813).add(1).and(4294967295).value; - this.key2 = crc(bigInt(this.key1).shiftRight(24).and(255), this.key2); + + this.key0.writeUInt32BE(crc(h, this.key0)); + + this.key1.writeUInt32BE(((this.key0.readUInt32BE() & 0xff & 0xFFFFFFFF) + this.key1.readUInt32BE()) >>> 0); + + + const x = new Int64((multiply(this.key1.readUInt32BE(), 134775813) + 1) & 0xFFFFFFFF); + + const b = Buffer.alloc(8); + + x.copy(b, 0); + + b.copy(this.key1, 0, 4, 8); + + + this.key2.writeUInt32BE(crc(((this.key1.readUInt32BE() >> 24) & 0xff) >>> 0, this.key2)); + }; Decrypt.prototype.decryptByte = function(c) { - const k = bigInt(this.key2).or(2); - c = c ^ bigInt(k).multiply(bigInt(k^1)).shiftRight(8).and(255); + + const k = (this.key2.readUInt32BE() | 2) >>> 0; + + c = c ^ ((multiply(k, (k^1 >>> 0)) >> 8) & 0xff); + this.update(c); + return c; + }; + Decrypt.prototype.stream = function() { + const stream = Stream.Transform(), + self = this; + stream._transform = function(d, e, cb) { - for (let i = 0; i Date: Tue, 28 May 2024 08:29:16 +0700 Subject: [PATCH 2/2] remove new lines and big-integer --- lib/Decrypt.js | 114 +++++++++++-------------------------------------- package.json | 1 - 2 files changed, 24 insertions(+), 91 deletions(-) diff --git a/lib/Decrypt.js b/lib/Decrypt.js index 0bbe5ae..2c50a7b 100644 --- a/lib/Decrypt.js +++ b/lib/Decrypt.js @@ -1,160 +1,94 @@ -const Int64 = require('node-int64'); - -let Stream = require('stream'); - +const Int64 = require("node-int64"); +let Stream = require("stream"); // Backwards compatibility for node versions < 8 if (!Stream.Writable || !Stream.Writable.prototype.destroy) - - Stream = require('readable-stream'); - + Stream = require("readable-stream"); let table; - function generateTable() { - const poly = 0xEDB88320; let c, n, k; - table = []; - for (n = 0; n < 256; n++) { - c = n; - - for (k = 0; k < 8; k++) - - c = (c & 1) ? poly ^ (c >>> 1) : c = c >>> 1; - + for (k = 0; k < 8; k++) c = c & 1 ? poly ^ (c >>> 1) : (c = c >>> 1); table[n] = c >>> 0; - } - } - function crc(ch, crc) { + if (!table) generateTable(); - if (!table) - - generateTable(); - - - if (ch.charCodeAt) - - ch = ch.charCodeAt(0); - + if (ch.charCodeAt) ch = ch.charCodeAt(0); const l = (crc.readUInt32BE() >> 8) & 0xffffff; - const r = table[(crc.readUInt32BE() ^ (ch >>> 0)) & 0xff]; - return (l ^ r) >>> 0; - } - function multiply(a, b) { - const ah = (a >> 16) & 0xffff; - const al = a & 0xffff; - const bh = (b >> 16) & 0xffff; - const bl = b & 0xffff; - const high = (ah * bl + al * bh) & 0xffff; return ((high << 16) >>> 0) + al * bl; - -}; - +} function Decrypt() { - - if (!(this instanceof Decrypt)) - - return new Decrypt(); - + if (!(this instanceof Decrypt)) return new Decrypt(); this.key0 = Buffer.allocUnsafe(4); - this.key1 = Buffer.allocUnsafe(4); - this.key2 = Buffer.allocUnsafe(4); - this.key0.writeUInt32BE(0x12345678, 0); - this.key1.writeUInt32BE(0x23456789, 0); - this.key2.writeUInt32BE(0x34567890, 0); - } - -Decrypt.prototype.update = function(h) { - +Decrypt.prototype.update = function (h) { this.key0.writeUInt32BE(crc(h, this.key0)); - - this.key1.writeUInt32BE(((this.key0.readUInt32BE() & 0xff & 0xFFFFFFFF) + this.key1.readUInt32BE()) >>> 0); - - - const x = new Int64((multiply(this.key1.readUInt32BE(), 134775813) + 1) & 0xFFFFFFFF); - + this.key1.writeUInt32BE( + ((this.key0.readUInt32BE() & 0xff & 0xFFFFFFFF) + + this.key1.readUInt32BE()) >>> 0 + ); + const x = new Int64( + (multiply(this.key1.readUInt32BE(), 134775813) + 1) & 0xFFFFFFFF + ); const b = Buffer.alloc(8); - x.copy(b, 0); - b.copy(this.key1, 0, 4, 8); - - - this.key2.writeUInt32BE(crc(((this.key1.readUInt32BE() >> 24) & 0xff) >>> 0, this.key2)); - + this.key2.writeUInt32BE( + crc(((this.key1.readUInt32BE() >> 24) & 0xff) >>> 0, this.key2) + ); }; - -Decrypt.prototype.decryptByte = function(c) { - +Decrypt.prototype.decryptByte = function (c) { const k = (this.key2.readUInt32BE() | 2) >>> 0; - - c = c ^ ((multiply(k, (k^1 >>> 0)) >> 8) & 0xff); - + c = c ^ ((multiply(k, (k ^ 1 >>> 0)) >> 8) & 0xff); this.update(c); return c; - }; - -Decrypt.prototype.stream = function() { - +Decrypt.prototype.stream = function () { const stream = Stream.Transform(), - self = this; - - - stream._transform = function(d, e, cb) { - - for (let i = 0; i