Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
tapcode.ts
Go to the documentation of this file.
1
13import { BaseCipher } from "../base/baseCipher";
14
22export class TapCodeCipher extends BaseCipher {
26 readonly CipherName = "Tap Code";
27
31 static readonly ALPHABET: string = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
32
38 super();
39 }
40
48 encode(plaintext: string): string {
49 const letters = TapCodeCipher.ALPHABET;
50 const result: string[] = [];
51
52 for (const ch of plaintext.toUpperCase()) {
53 if (ch === 'J') {
54 // I/J share the same position (I)
55 result.push('.. ...');
56 }
57 else if (/[A-Z]/.test(ch)) {
58 const index = letters.indexOf(ch === 'J' ? 'I' : ch);
59 const row = Math.floor(index / 5) + 1;
60 const col = (index % 5) + 1;
61
62 result.push('.'.repeat(row) + ' ' + '.'.repeat(col));
63 }
64 else {
65 // Preserve non-alphabetic characters
66 result.push(ch);
67 }
68 }
69
70 return result.join(' / ');
71 }
72
80 decode(ciphertext: string): string {
81 const letters = TapCodeCipher.ALPHABET;
82
83 return ciphertext
84 .split(' / ')
85 .map(token => {
86 if (!token.trim()) {
87 return '';
88 }
89
90 const [rowDots, colDots] = token.split(' ');
91
92 // Handle malformed tokens gracefully
93 if (!rowDots || !colDots) {
94 return token;
95 }
96
97 const row = rowDots.length;
98 const col = colDots.length;
99 const index = (row - 1) * 5 + (col - 1);
100
101 return letters[index] ?? '?';
102 })
103 .join('');
104 }
105}
Abstract base class providing common functionality for cipher implementations.
Definition baseCipher.ts:18
Implementation of the Tap Code prisoner communication cipher.
Definition tapcode.ts:22
static readonly ALPHABET
Modified alphabet for 5×5 grid (excludes J, shares with I)
Definition tapcode.ts:31
constructor()
Constructor for Tap Code cipher.
Definition tapcode.ts:37
readonly CipherName
Identifier name for this cipher.
Definition tapcode.ts:26
export const Record< string,(...args:any[])=> string