Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
caesar.ts
Go to the documentation of this file.
1
12import { BaseCipher } from "../base/baseCipher";
13
21export class CaesarCipher extends BaseCipher {
25 private shift: number;
26
30 readonly CipherName = "Caesar";
31
38 constructor(shift: number = 3) {
39 super();
40 this.shift = shift;
41 }
42
50 encode(plainText: string = ""): string {
51 const output: string[] = [];
52
53 for (const ch of plainText) {
54 const index = BaseCipher.ALPHABET.indexOf(ch.toUpperCase());
55 if (index === -1) {
56 output.push(ch);
57 continue;
58 }
59
60 const cipherIndex = this.mod(index + this.shift, 26);
61 output.push(BaseCipher.ALPHABET[cipherIndex]);
62 }
63
64 return output.join("");
65 }
66
75 decode(cipherText: string = ""): string {
76 const output: string[] = [];
77
78 for (const ch of cipherText) {
79 const index = BaseCipher.ALPHABET.indexOf(ch.toUpperCase());
80 if (index === -1) {
81 output.push(ch);
82 continue;
83 }
84
85 const plainIndex = this.mod(index - this.shift, 26);
86 output.push(BaseCipher.ALPHABET[plainIndex]);
87 }
88
89 return output.join("");
90 }
91}
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 Caesar substitution cipher.
Definition caesar.ts:21
constructor(shift:number=3)
Constructor for Caesar cipher.
Definition caesar.ts:38
readonly CipherName
Identifier name for this cipher.
Definition caesar.ts:30
export const Record< string,(...args:any[])=> string