Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
baseCipher.ts
Go to the documentation of this file.
1
9import { CipherI } from "./cipherInterface";
10
18export abstract class BaseCipher implements CipherI {
25 abstract encode(plaintext: string, key?: any): string;
26
33 abstract decode(ciphertext: string, key?: any): string;
34
38 abstract readonly CipherName: string;
39
44 static readonly ALPHABET: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
45
52 protected sanitize(text: string): string {
53 return text.toUpperCase().replace(/[^A-Z]/g, '');
54 }
55
63 protected mod(n: number, m: number): number {
64 return ((n % m) + m) % m;
65 }
66
75 protected modInverse(a: number, m: number): number {
76 a = this.mod(a, m);
77 for (let x = 1; x < m; x++) {
78 if (this.mod(a * x, m) === 1) {
79 return x;
80 }
81 }
82 throw new Error("No modular inverse");
83 }
84
92 protected shiftChar(ch: string, shift: number): string {
93 const i = BaseCipher.ALPHABET.indexOf(ch.toUpperCase());
94 if (i === -1) {
95 return ch;
96 };
97 return BaseCipher.ALPHABET[this.mod(i + shift, 26)];
98 }
99}
Abstract base class providing common functionality for cipher implementations.
Definition baseCipher.ts:18
static readonly ALPHABET
Standard English alphabet for cipher operations.
Definition baseCipher.ts:44
abstract abstract encode(plaintext:string, key?:any) abstract decode(ciphertext:string, key?:any) readonly CipherName
Abstract method to encode plaintext.
Definition baseCipher.ts:33
Common interface that all cipher implementations must follow.
export const Record< string,(...args:any[])=> string