Skip to content

Commit

Permalink
Update crc32.js
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Jul 22, 2020
1 parent 9c90799 commit 143600c
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 102 deletions.
185 changes: 87 additions & 98 deletions app/cdn/crc32.js
Original file line number Diff line number Diff line change
@@ -1,108 +1,97 @@
"use strict";
// https://github.com/bilibili-helper/bilibili-helper-o/blob/v1.2.21/src/js/libs/crc32.js

function _classCallCheck(a, b) {
if (!(a instanceof b)) throw new TypeError("Cannot call a class as a function")
}
var _createClass = function() {
function a(a, b) {
for (var c = 0; c < b.length; c++) {
var d = b[c];
d.enumerable = d.enumerable || !1, d.configurable = !0, "value" in d && (d.writable = !0), Object.defineProperty(a, d.key, d)
const CRC32_POLY = 0xEDB88320;

class Crc32Engine {
static initCrc32Table(table) {
for (let i = 0; i < 256; i++) {
let currCrc = i;
for (let j = 0; j < 8; j++) {
if (currCrc & 1) {
currCrc = (currCrc >>> 1) ^ CRC32_POLY;
} else {
currCrc >>>= 1;
}
}
table[i] = currCrc;
}
return function(b, c, d) {
return c && a(b.prototype, c), d && a(b, d), b
}

constructor() {
this.crc32Table = new Uint32Array(256);
Crc32Engine.initCrc32Table(this.crc32Table);
this.rainbowTableHash = new Uint32Array(100000);
this.rainbowTableValue = new Uint32Array(100000);
let fullHashCache = new Uint32Array(100000),
shortHashBuckets = new Uint32Array(65537);
// Initialize the rainbow Table
for (let i = 0; i < 100000; i++) {
let hash = this.compute(i) >>> 0;
fullHashCache[i] = hash;
shortHashBuckets[hash >>> 16]++;
}
}(),
CRC32_POLY = 3988292384,
Crc32Engine = function() {
function a() {
_classCallCheck(this, a), this.crc32Table = new Uint32Array(256), a.initCrc32Table(this.crc32Table), this.rainbowTableHash = new Uint32Array(1e5), this.rainbowTableValue = new Uint32Array(1e5);
for (var b = new Uint32Array(1e5), c = new Uint32Array(65537), d = 0; d < 1e5; d++) {
var e = this.compute(d) >>> 0;
b[d] = e, c[e >>> 16]++
}
var f = 0;
this.shortHashBucketStarts = c.map(function(a) {
return f += a
});
for (var g = 0; g < 1e5; g++) {
var h = --this.shortHashBucketStarts[b[g] >>> 16];
this.rainbowTableHash[h] = b[g], this.rainbowTableValue[h] = g
}
let runningSum = 0;
this.shortHashBucketStarts = shortHashBuckets.map((n) => runningSum += n);
for (let i = 0; i < 100000; i++) {
let idx = --this.shortHashBucketStarts[fullHashCache[i] >>> 16];
this.rainbowTableHash[idx] = fullHashCache[i];
this.rainbowTableValue[idx] = i;
}
return _createClass(a, null, [{
key: "initCrc32Table",
value: function(a) {
for (var b = 0; b < 256; b++) {
for (var c = b, d = 0; d < 8; d++) 1 & c ? c = c >>> 1 ^ CRC32_POLY : c >>>= 1;
a[b] = c
}
}

compute(input, addPadding = false) {
let currCrc = 0;
for (let digit of input.toString()) {
currCrc = this.crc32Update(currCrc, Number(digit));
}
if (addPadding) {
for (let i = 0; i < 5; i++) {
currCrc = this.crc32Update(currCrc, 0);
}
}]), _createClass(a, [{
key: "compute",
value: function(a) {
var b = arguments.length > 1 && void 0 !== arguments[1] && arguments[1],
c = 0,
d = !0,
e = !1,
f = void 0;
try {
for (var g, h = a.toString()[Symbol.iterator](); !(d = (g = h.next()).done); d = !0) {
var i = g.value;
c = this.crc32Update(c, Number(i))
}
} catch (j) {
e = !0, f = j
} finally {
try {
!d && h["return"] && h["return"]()
} finally {
if (e) throw f
}
return currCrc;
}

crack(hash) {
let candidates = [];
let hashVal = ~Number('0x' + hash) >>> 0;
let baseHash = 0xFFFFFFFF;

for (let digitCount = 1; digitCount < 10; digitCount++) {
baseHash = this.crc32Update(baseHash, 0x30); // 0x30: '0'
if (digitCount < 6) {
// Direct lookup
candidates = candidates.concat(this.lookup(hashVal ^ baseHash));
} else {
// Lookup with prefix
let startPrefix = Math.pow(10, digitCount - 6);
let endPrefix = Math.pow(10, digitCount - 5);

for (let prefix = startPrefix; prefix < endPrefix; prefix++) {
for (let postfix of this.lookup(hashVal ^ baseHash ^
this.compute(prefix, true))) {
candidates.push(prefix * 100000 + postfix);
}
}
if (b)
for (var k = 0; k < 5; k++) c = this.crc32Update(c, 0);
return c
}
}, {
key: "crack",
value: function(a) {
for (var b = [], c = ~Number("0x" + a) >>> 0, d = 4294967295, e = 1; e < 10; e++)
if (d = this.crc32Update(d, 48), e < 6) b = b.concat(this.lookup(c ^ d));
else
for (var f = Math.pow(10, e - 6), g = Math.pow(10, e - 5), h = f; h < g; h++) {
var i = !0,
j = !1,
k = void 0;
try {
for (var l, m = this.lookup(c ^ d ^ this.compute(h, !0))[Symbol.iterator](); !(i = (l = m.next()).done); i = !0) {
var n = l.value;
b.push(1e5 * h + n)
}
} catch (o) {
j = !0, k = o
} finally {
try {
!i && m["return"] && m["return"]()
} finally {
if (j) throw k
}
}
}
return b
}
}, {
key: "crc32Update",
value: function(a, b) {
return a >>> 8 ^ this.crc32Table[255 & (a ^ b)]
}
}, {
key: "lookup",
value: function(a) {
a >>>= 0;
for (var b = [], c = a >>> 16, d = this.shortHashBucketStarts[c]; d < this.shortHashBucketStarts[c + 1]; d++) this.rainbowTableHash[d] === a && b.push(this.rainbowTableValue[d]);
return b
}
return candidates;
}

crc32Update(currCrc, code) {
return (currCrc >>> 8) ^ this.crc32Table[(currCrc ^ code) & 0xFF];
}

lookup(hash) {
hash >>>= 0;
let candidates = [];
let shortHash = hash >>> 16;
for (let i = this.shortHashBucketStarts[shortHash];
i < this.shortHashBucketStarts[shortHash + 1]; i++) {
if (this.rainbowTableHash[i] === hash) {
candidates.push(this.rainbowTableValue[i]);
}
}]), a
}();
}
return candidates;
}
}
6 changes: 3 additions & 3 deletions app/js/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class Downloader {
}
}

download() {
downloadAll() {
let { cid } = this, flag = true;
document.querySelectorAll("tbody input[type=checkbox]").forEach((element, part) => {
if (!element.checked || this.downloading.includes(this.links[part])) return;
Expand Down Expand Up @@ -239,13 +239,13 @@ class Downloader {
};
let downloads = fs.createWriteStream(file, state ? {"flags": "a"} : {}),
index = this.downloading.indexOf(options.url);
this.generalDownload(index, options, downloads);
this.download(index, options, downloads);
state && $(".addon").eq(index).html(`从 ${Math.round(state.size / 1e6)}MB 处恢复的下载`);
//console.log(this.cid, file, options.url);
});
}

generalDownload(index, options, downloads) {
download(index, options, downloads) {
//https://blog.csdn.net/zhu_06/article/details/79772229
let proStream = progress({
time: 250 //单位ms
Expand Down
2 changes: 1 addition & 1 deletion app/panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ <h1>Mimi Downloader</h1>
</div>
<div class="row">
<div class="form-group">
<button class="btn btn-success" onclick="downloader.download()">下载选中片段</button>
<button class="btn btn-success" onclick="downloader.downloadAll()">下载选中片段</button>
<button class="btn btn-primary" onclick="openPath()">打开下载目录</button>
</div>
</div>
Expand Down

0 comments on commit 143600c

Please sign in to comment.