remove js-sha256 dependency, use native hashing function

This commit is contained in:
Dainius Daukševičius 2020-10-15 20:15:11 +03:00
parent 4dc4160215
commit 5bc12e52f3
4 changed files with 15 additions and 19 deletions

5
package-lock.json generated
View file

@ -10494,11 +10494,6 @@
"traverse": "0.4.x"
}
},
"js-sha256": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.9.0.tgz",
"integrity": "sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA=="
},
"js-tokens": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",

View file

@ -11,7 +11,6 @@
"babel-loader": "^8.0.6",
"babel-preset-env": "^1.7.0",
"concurrently": "^5.1.0",
"js-sha256": "^0.9.0",
"react": "^16.12.0",
"react-dom": "^16.12.0"
},

View file

@ -544,7 +544,7 @@ function incorrectVideoCheck(videoID?: string, sponsorTime?: SponsorTime): boole
}
}
function sponsorsLookup(id: string) {
async function sponsorsLookup(id: string) {
video = document.querySelector('video') // Youtube video player
//there is no video here
if (video == null) {
@ -621,7 +621,8 @@ function sponsorsLookup(id: string) {
// Check for hashPrefix setting
let getRequest;
if (Config.config.hashPrefix) {
getRequest = utils.asyncRequestToServer('GET', "/api/skipSegments/" + utils.getHash(id, 1).substr(0,4), {
const hashPrefix = (await utils.getHash(id, 1)).substr(0, 4);
getRequest = utils.asyncRequestToServer('GET', "/api/skipSegments/" + hashPrefix, {
categories
});
} else {

View file

@ -1,6 +1,5 @@
import Config from "./config";
import { CategorySelection, SponsorTime, FetchResponse } from "./types";
import { sha256 } from 'js-sha256';
import * as CompileConfig from "../config.json";
@ -380,17 +379,19 @@ class Utils {
return typeof(browser) !== "undefined";
}
getHash(value: string, times=5000): string {
async getHash(value: string, times = 5000): Promise<string> {
if (times <= 0) return "";
let hashBuffer = new TextEncoder().encode(value).buffer;
for (let i = 0; i < times; i++) {
let hash = sha256.create();
hash.update(value);
hash.hex();
value = hash.toString();
hashBuffer = await crypto.subtle.digest('SHA-256', hashBuffer);
}
return value;
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
}
}