From ba4b8cc29edaec094f225966f21455ce7b840441 Mon Sep 17 00:00:00 2001 From: Savely Savianok <1986developer@gmail.com> Date: Sat, 1 Mar 2025 22:58:48 +0300 Subject: [PATCH] feat(staging): adding protobuf decoder (part 2) --- lib/protobuf-decoder/protobufDecoder.js | 2 +- lib/totp-quickjs/base32decoder.js | 38 +++++++++++++++---------- setting/utils/queryParser.js | 7 +++-- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/protobuf-decoder/protobufDecoder.js b/lib/protobuf-decoder/protobufDecoder.js index f9a11af..558f1cb 100644 --- a/lib/protobuf-decoder/protobufDecoder.js +++ b/lib/protobuf-decoder/protobufDecoder.js @@ -1,6 +1,6 @@ import { decodeVarint } from "./varintUtils"; -class BufferReader { +export class BufferReader { constructor(buffer) { this.buffer = buffer; this.offset = 0; diff --git a/lib/totp-quickjs/base32decoder.js b/lib/totp-quickjs/base32decoder.js index 7c0e514..821b33c 100644 --- a/lib/totp-quickjs/base32decoder.js +++ b/lib/totp-quickjs/base32decoder.js @@ -25,22 +25,30 @@ function leftpad(str, len, pad) { ); } -export function base64decode(base64String) { - var sliceSize = 1024; - var byteCharacters = window.atob(base64String); - var bytesLength = byteCharacters.length; - var slicesCount = Math.ceil(bytesLength / sliceSize); - var byteArrays = new Array(slicesCount); +export function base64decode(base64) { + const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + let result = []; + let i = 0, j = 0; + let b1, b2, b3, b4; - for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) { - var begin = sliceIndex * sliceSize; - var end = Math.min(begin + sliceSize, bytesLength); + while (i < base64.length) { + b1 = chars.indexOf(base64.charAt(i++)); + b2 = chars.indexOf(base64.charAt(i++)); + b3 = chars.indexOf(base64.charAt(i++)); + b4 = chars.indexOf(base64.charAt(i++)); - var bytes = new Array(end - begin); - for (var offset = begin, i = 0; offset < end; ++i, ++offset) { - bytes[i] = byteCharacters[offset].charCodeAt(0); - } - byteArrays[sliceIndex] = new Uint8Array(bytes); + if (b1 === -1 || b2 === -1) break; + + result[j++] = (b1 << 2) | (b2 >> 4); + + if (b3 !== -1) { + result[j++] = ((b2 & 15) << 4) | (b3 >> 2); } - return byteArrays + + if (b4 !== -1) { + result[j++] = ((b3 & 3) << 6) | b4; + } + } + + return result.slice(0, j); } \ No newline at end of file diff --git a/setting/utils/queryParser.js b/setting/utils/queryParser.js index 8bb2365..2bd7953 100644 --- a/setting/utils/queryParser.js +++ b/setting/utils/queryParser.js @@ -64,6 +64,9 @@ function getByOtpauthScheme(link){ function getByGoogleMigrationScheme(link){ console.log("Hello") let data = link.split("data=")[1]; //Returns secret - let decodedProto = decodeProto(base64decode(data)); - console.log(decodedProto) + data = decodeURIComponent(data); + console.log(data) + let decode = base64decode(data); + console.log(decode) + console.log(decodeProto(decode)); } \ No newline at end of file