refactor: move text encoder/decoder to util file
This commit is contained in:
parent
65b2474b43
commit
fe256218e6
@ -3,6 +3,7 @@ import type { CryptoBase } from '../CryptoBase';
|
|||||||
import type { DecryptCommandOptions } from '~/decrypt-worker/types.ts';
|
import type { DecryptCommandOptions } from '~/decrypt-worker/types.ts';
|
||||||
import { SEED, ENC_V2_KEY_1, ENC_V2_KEY_2 } from './qmc_v2.key.ts';
|
import { SEED, ENC_V2_KEY_1, ENC_V2_KEY_2 } from './qmc_v2.key.ts';
|
||||||
import { fetchParakeet } from '@jixun/libparakeet';
|
import { fetchParakeet } from '@jixun/libparakeet';
|
||||||
|
import { stringToUTF8Bytes } from '~/decrypt-worker/util/utf8Encoder.ts';
|
||||||
|
|
||||||
export class QMC2Crypto implements CryptoBase {
|
export class QMC2Crypto implements CryptoBase {
|
||||||
cryptoName = 'QMC/v2';
|
cryptoName = 'QMC/v2';
|
||||||
@ -36,8 +37,7 @@ export class QMC2CryptoWithKey implements CryptoBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const parakeet = await fetchParakeet();
|
const parakeet = await fetchParakeet();
|
||||||
const textEncoder = new TextEncoder();
|
const key = stringToUTF8Bytes(options.qmc2Key);
|
||||||
const key = textEncoder.encode(options.qmc2Key);
|
|
||||||
const keyCrypto = parakeet.make.QMCv2KeyCrypto(SEED, ENC_V2_KEY_1, ENC_V2_KEY_2);
|
const keyCrypto = parakeet.make.QMCv2KeyCrypto(SEED, ENC_V2_KEY_1, ENC_V2_KEY_2);
|
||||||
return transformBlob(buffer, (p) => p.make.QMCv2EKey(key, keyCrypto), {
|
return transformBlob(buffer, (p) => p.make.QMCv2EKey(key, keyCrypto), {
|
||||||
parakeet,
|
parakeet,
|
||||||
|
10
src/decrypt-worker/util/utf8Encoder.ts
Normal file
10
src/decrypt-worker/util/utf8Encoder.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
export const utf8Encoder = new TextEncoder();
|
||||||
|
export const utf8Decoder = new TextDecoder('utf-8');
|
||||||
|
|
||||||
|
export function stringToUTF8Bytes(str: string) {
|
||||||
|
return utf8Encoder.encode(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function bytesToUTF8String(str: BufferSource) {
|
||||||
|
return utf8Decoder.decode(str);
|
||||||
|
}
|
@ -1,8 +1,7 @@
|
|||||||
import type { StagingKWMv2Key } from '~/features/settings/keyFormats';
|
import type { StagingKWMv2Key } from '~/features/settings/keyFormats';
|
||||||
|
import { bytesToUTF8String } from '~/decrypt-worker/util/utf8Encoder';
|
||||||
import { formatHex } from './formatHex';
|
import { formatHex } from './formatHex';
|
||||||
|
|
||||||
const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
|
|
||||||
|
|
||||||
export class MMKVParser {
|
export class MMKVParser {
|
||||||
private offset = 4;
|
private offset = 4;
|
||||||
private length: number;
|
private length: number;
|
||||||
@ -67,7 +66,7 @@ export class MMKVParser {
|
|||||||
// ]
|
// ]
|
||||||
const strByteLen = this.readInt();
|
const strByteLen = this.readInt();
|
||||||
const data = this.readBytes(strByteLen);
|
const data = this.readBytes(strByteLen);
|
||||||
return textDecoder.decode(data).normalize();
|
return bytesToUTF8String(data).normalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
public readVariantString() {
|
public readVariantString() {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
const textEncoder = new TextEncoder();
|
|
||||||
|
|
||||||
// translation of pseudocode from Wikipedia:
|
// translation of pseudocode from Wikipedia:
|
||||||
|
import { stringToUTF8Bytes } from '~/decrypt-worker/util/utf8Encoder';
|
||||||
|
|
||||||
// https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_two_matrix_rows
|
// https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_two_matrix_rows
|
||||||
export function levenshtein(str1: string, str2: string) {
|
export function levenshtein(str1: string, str2: string) {
|
||||||
if (str1 === str2) {
|
if (str1 === str2) {
|
||||||
@ -14,8 +14,8 @@ export function levenshtein(str1: string, str2: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Convert them to Uint8Array to avoid expensive string APIs.
|
// Convert them to Uint8Array to avoid expensive string APIs.
|
||||||
const s = textEncoder.encode(str1.toLowerCase());
|
const s = stringToUTF8Bytes(str1.normalize().toLowerCase());
|
||||||
const t = textEncoder.encode(str2.toLowerCase());
|
const t = stringToUTF8Bytes(str2.normalize().toLowerCase());
|
||||||
const m = s.byteLength;
|
const m = s.byteLength;
|
||||||
const n = t.byteLength;
|
const n = t.byteLength;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user