Skip to content

Commit

Permalink
Merge pull request #316 from lechuhuuha/master
Browse files Browse the repository at this point in the history
replace bigint with node-int64
  • Loading branch information
ZJONSSON authored Jun 8, 2024
2 parents 343956a + 360b0f2 commit 760f100
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 30 deletions.
83 changes: 55 additions & 28 deletions lib/Decrypt.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
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;

Expand All @@ -9,59 +14,81 @@ function generateTable() {
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);

const l = (crc.readUInt32BE() >> 8) & 0xffffff;
const r = table[(crc.readUInt32BE() ^ (ch >>> 0)) & 0xff];

return (l ^ r) >>> 0;
}

if (ch.charCodeAt)
ch = ch.charCodeAt(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 (bigInt(crc).shiftRight(8).and(0xffffff)).xor(table[bigInt(crc).xor(ch).and(0xff)]).value;
return ((high << 16) >>> 0) + al * bl;
}

function Decrypt() {
if (!(this instanceof Decrypt))
return new 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);
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
);
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);
Decrypt.prototype.decryptByte = function (c) {
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() {
Decrypt.prototype.stream = function () {
const stream = Stream.Transform(),
self = this;

stream._transform = function(d, e, cb) {
for (let i = 0; i<d.length;i++) {
stream._transform = function (d, e, cb) {
for (let i = 0; i < d.length; i++) {
d[i] = self.decryptByte(d[i]);
}
this.push(d);
cb();
};

return stream;
};


module.exports = Decrypt;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
},
"license": "MIT",
"dependencies": {
"big-integer": "^1.6.17",
"bluebird": "~3.4.1",
"duplexer2": "~0.1.4",
"fs-extra": "^11.2.0",
"graceful-fs": "^4.2.2"
"graceful-fs": "^4.2.2",
"node-int64": "^0.4.0"
},
"devDependencies": {
"@eslint/js": "^9.2.0",
Expand Down

0 comments on commit 760f100

Please sign in to comment.