From dd5c68223b6b48344509132b411a166ebfbeedd4 Mon Sep 17 00:00:00 2001 From: absidue <48293849+absidue@users.noreply.github.com> Date: Tue, 31 May 2022 03:42:39 +0200 Subject: [PATCH] Use the builtin subtle crypto library instead of node-forge (#2276) --- package.json | 1 - src/renderer/store/modules/sponsorblock.js | 39 +++++++++++++--------- yarn.lock | 2 +- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 4b8f24ce..7b5bc878 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,6 @@ "marked": "^4.0.15", "material-design-icons": "^3.0.1", "nedb-promises": "^5.0.1", - "node-forge": "^1.3.1", "opml-to-json": "^1.0.1", "rss-parser": "^3.12.0", "socks-proxy-agent": "^6.0.0", diff --git a/src/renderer/store/modules/sponsorblock.js b/src/renderer/store/modules/sponsorblock.js index bfcbd224..24183078 100644 --- a/src/renderer/store/modules/sponsorblock.js +++ b/src/renderer/store/modules/sponsorblock.js @@ -1,5 +1,4 @@ import $ from 'jquery' -import forge from 'node-forge' const state = {} const getters = {} @@ -7,22 +6,30 @@ const getters = {} const actions = { sponsorBlockSkipSegments ({ rootState }, { videoId, categories }) { return new Promise((resolve, reject) => { - const messageDigestSha256 = forge.md.sha256.create() - messageDigestSha256.update(videoId) - const videoIdHashPrefix = messageDigestSha256.digest().toHex().substring(0, 4) - const requestUrl = `${rootState.settings.sponsorBlockUrl}/api/skipSegments/${videoIdHashPrefix}?categories=${JSON.stringify(categories)}` + const videoIdBuffer = new TextEncoder().encode(videoId) - $.getJSON(requestUrl, (response) => { - const segments = response - .filter((result) => result.videoID === videoId) - .flatMap((result) => result.segments) - resolve(segments) - }).fail((xhr, textStatus, error) => { - console.log(xhr) - console.log(textStatus) - console.log(requestUrl) - console.log(error) - reject(xhr) + crypto.subtle.digest('SHA-256', videoIdBuffer).then((hashBuffer) => { + const hashArray = Array.from(new Uint8Array(hashBuffer)) + + const videoIdHashPrefix = hashArray + .map(byte => byte.toString(16).padStart(2, '0')) + .slice(0, 4) + .join('') + + const requestUrl = `${rootState.settings.sponsorBlockUrl}/api/skipSegments/${videoIdHashPrefix}?categories=${JSON.stringify(categories)}` + + $.getJSON(requestUrl, (response) => { + const segments = response + .filter((result) => result.videoID === videoId) + .flatMap((result) => result.segments) + resolve(segments) + }).fail((xhr, textStatus, error) => { + console.log(xhr) + console.log(textStatus) + console.log(requestUrl) + console.log(error) + reject(xhr) + }) }) }) } diff --git a/yarn.lock b/yarn.lock index 921fccac..4104a3d0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6356,7 +6356,7 @@ node-fetch@^2.6.0: dependencies: whatwg-url "^5.0.0" -node-forge@^1, node-forge@^1.3.1: +node-forge@^1: version "1.3.1" resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-1.3.1.tgz#be8da2af243b2417d5f646a770663a92b7e9ded3" integrity sha512-dPEtOeMvF9VMcYV/1Wb8CPoVAXtp6MKMlcbAt4ddqmGqUJ6fQZFXkNZNkNlfevtNkGtaSoXf/vNNNSvgrdXwtA==