Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 1x 1x 26x 23x 23x 23x 90x 23x 23x 23x 107x 23x 23x 7x 7x 7x 7x 7x 7x | import { BytesCoder } from '../bytesCoder';
import type { IPadding } from './padding';
export abstract class ICrypter {
protected readonly abstract blockSize: number;
public constructor(
private readonly padding: IPadding
) {}
protected abstract encryptBlocks(blocks: Uint8Array[]): Uint8Array;
protected abstract decryptBlocks(blocks: Uint8Array[]): Uint8Array;
public encrypt(data: Uint8Array): Uint8Array {
const paddedData = this.padding.addTo(data, this.blockSize);
const blocks = [];
for (let i = 0; i < paddedData.length; i += this.blockSize)
blocks.push(paddedData.slice(i, i + this.blockSize));
return this.encryptBlocks(blocks);
}
public decrypt(data: Uint8Array): Uint8Array {
const blocks = [];
for (let i = 0; i < data.length; i += this.blockSize)
blocks.push(data.slice(i, i + this.blockSize));
const decryptedData = this.decryptBlocks(blocks);
return this.padding.removeFrom(decryptedData);
}
public encodeAndEncrypt(value: unknown): Uint8Array {
const jsonString = value === undefined ? '' : JSON.stringify(value);
const data = BytesCoder.fromUtf8(jsonString);
return this.encrypt(data);
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
public decryptAndDecode<T = unknown>(data: Uint8Array): T {
const decryptedData = this.decrypt(data);
const value = BytesCoder.toUtf8(decryptedData);
return (value === '' ? undefined : JSON.parse(value)) as T;
}
}
|