Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
monoAlphabetic.ts
Go to the documentation of this file.
1
12import { BaseCipher } from "../base/baseCipher";
13
21export class MonoalphabeticCipher extends BaseCipher {
25 readonly CipherName = "Monoalphabetic";
26
30 private mapping: Record<string, string>;
31
35 private inverse: Record<string, string>;
36
43 constructor(mapAlphabet: string = "") {
44 super();
45
46 const sanitizedMap = this.sanitize(mapAlphabet);
47
48 if (sanitizedMap.length !== 26) {
49 throw new Error("mapAlphabet must have 26 letters");
50 }
51
52 this.mapping = {};
53 this.inverse = {};
54
55 for (let i = 0; i < 26; i++) {
56 this.mapping[MonoalphabeticCipher.ALPHABET[i]] = sanitizedMap[i];
57 this.inverse[sanitizedMap[i]] = MonoalphabeticCipher.ALPHABET[i];
58 }
59 }
60
68 encode(plainText: string = ""): string {
69 return plainText
70 .split("")
71 .map(c => this.mapping[c.toUpperCase()] ?? c)
72 .join("");
73 }
74
82 decode(cipherText: string = ""): string {
83 return cipherText
84 .split("")
85 .map(c => this.inverse[c.toUpperCase()] ?? c)
86 .join("");
87 }
88}
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 a customizable monoalphabetic substitution cipher.
constructor(mapAlphabet:string="")
Constructor for Monoalphabetic cipher.
readonly CipherName
Identifier name for this cipher.
export const Record< string,(...args:any[])=> string