Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
keyWord.ts
Go to the documentation of this file.
1
12import { BaseCipher } from "../base/baseCipher";
13
21export class KeywordCipher extends BaseCipher {
25 readonly CipherName = "Keyword";
26
30 private mapping: Record<string, string>;
31
35 private inverse: Record<string, string>;
36
43 constructor(keyword: string = "") {
44 super();
45
46 const sanitizedKeyword = this.sanitize(keyword);
47 const seen = new Set<string>();
48 const keyAlphabet: string[] = [];
49
50 // Add unique letters from the keyword first
51 for (const ch of sanitizedKeyword) {
52 if (!seen.has(ch)) {
53 seen.add(ch);
54 keyAlphabet.push(ch);
55 }
56 }
57
58 // Fill in the rest of the alphabet
59 for (const ch of KeywordCipher.ALPHABET) {
60 if (!seen.has(ch)) {
61 keyAlphabet.push(ch);
62 }
63 }
64
65 // Build mapping and inverse mapping
66 this.mapping = {};
67 this.inverse = {};
68 for (let i = 0; i < 26; i++) {
69 this.mapping[KeywordCipher.ALPHABET[i]] = keyAlphabet[i];
70 this.inverse[keyAlphabet[i]] = KeywordCipher.ALPHABET[i];
71 }
72 }
73
81 encode(plainText: string = ""): string {
82 return plainText
83 .split("")
84 .map(c => this.mapping[c.toUpperCase()] ?? c)
85 .join("");
86 }
87
95 decode(cipherText: string = ""): string {
96 return cipherText
97 .split("")
98 .map(c => this.inverse[c.toUpperCase()] ?? c)
99 .join("");
100 }
101}
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
Implementation of the Keyword monoalphabetic substitution cipher.
Definition keyWord.ts:21
readonly CipherName
Identifier name for this cipher.
Definition keyWord.ts:25
constructor(keyword:string="")
Constructor for Keyword cipher.
Definition keyWord.ts:43
export const Record< string,(...args:any[])=> string