crunchy
Version:
Crunchy is a fork of Crunchyroll.js, capable of downloading anime episodes from the popular CrunchyRoll streaming service.
74 lines • 2.06 kB
JavaScript
/* tslint:disable:no-bitwise false */
;
exports.__esModule = true;
var bigInt = require("big-integer");
var crypto = require("crypto");
var zlib = require("zlib");
/**
* Decodes the data.
*/
function default_1(id, iv, data, done) {
try {
decompress(decrypt(id, iv, data), done);
}
catch (e) {
done(e);
}
}
exports["default"] = default_1;
/**
* Decrypts the data.
*/
function decrypt(id, iv, data) {
var ivBuffer = typeof iv === 'string' ? Buffer.from(iv, 'base64') : iv;
var dataBuffer = typeof data === 'string' ? Buffer.from(data, 'base64') : data;
var decipher = crypto.createDecipheriv('aes-256-cbc', key(id), ivBuffer);
decipher.setAutoPadding(false);
return Buffer.concat([decipher.update(dataBuffer), decipher.final()]);
}
/**
* Decompresses the data.
*/
function decompress(data, done) {
try {
zlib.inflate(data, done);
}
catch (e) {
done(null, data);
}
}
/**
* Generates a key.
*/
function key(subtitleId) {
var hash = secret(20, 97, 1, 2) + magic(subtitleId);
var result = Buffer.allocUnsafe(32);
result.fill(0);
crypto.createHash('sha1').update(hash).digest().copy(result);
return result;
}
/**
* Generates a magic number.
*/
function magic(subtitleId) {
var base = Math.floor(Math.sqrt(6.9) * Math.pow(2, 25));
var hash = bigInt(base).xor(subtitleId).toJSNumber();
var multipliedHash = bigInt(hash).multiply(32).toJSNumber();
return bigInt(hash).xor(hash >> 3).xor(multipliedHash).toJSNumber();
}
/**
* Generates a secret string based on a Fibonacci sequence.
*/
function secret(size, modulo, firstSeed, secondSeed) {
var currentValue = firstSeed + secondSeed;
var previousValue = secondSeed;
var result = '';
for (var i = 0; i < size; i += 1) {
var oldValue = currentValue;
result += String.fromCharCode(currentValue % modulo + 33);
currentValue += previousValue;
previousValue = oldValue;
}
return result;
}
//# sourceMappingURL=decode.js.map