Asper Header  1.0.22
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
44 constructor(mapAlphabet: string = "QWERTYUIOPASDFGHJKLZXCVBNM") {
45 super();
46
47 const sanitizedMap = this.sanitize(mapAlphabet);
48
49 if (sanitizedMap.length !== 26) {
50 throw new Error("mapAlphabet must have 26 letters");
51 }
52
53 this.mapping = {};
54 this.inverse = {};
55
56 for (let i = 0; i < 26; i++) {
57 this.mapping[MonoalphabeticCipher.ALPHABET[i]] = sanitizedMap[i];
58 this.inverse[sanitizedMap[i]] = MonoalphabeticCipher.ALPHABET[i];
59 }
60 }
61
69 encode(plainText: string = ""): string {
70 return plainText
71 .split("")
72 .map(c => this.mapping[c.toUpperCase()] ?? c)
73 .join("");
74 }
75
83 decode(cipherText: string = ""): string {
84 return cipherText
85 .split("")
86 .map(c => this.inverse[c.toUpperCase()] ?? c)
87 .join("");
88 }
89}
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="QWERTYUIOPASDFGHJKLZXCVBNM")
Constructor for Monoalphabetic cipher.
readonly CipherName
Identifier name for this cipher.
export const export const string[]
Definition constants.ts:203