-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
112 lines (90 loc) · 3.09 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import type { CaesarInterface } from "./interface";
/**
* Encrypts a message using the Caesar cipher.
*
* @param message - The message to encrypt. (default: "")
* @param shift - The number of positions to shift each character (default: 3).
* @returns The encrypted message.
*/
export function caesarEncrypt(message: string, shift: number = 3): string {
if (message === undefined) {
throw new Error("A message to encrypt was not provided");
}
if (shift <= 0) {
throw new Error("The shift value cannot be negative");
}
// Alright firstly, we are gonna make a string so that we can move the alphabets
const alphabet: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Function to shift the characters
function shiftCharacter(char: string, shift: number): string {
if (char === " ") {
// return spaces if there are only spaces.
return " ";
}
const isUpperCase: Boolean = char === char.toUpperCase();
const charIndex: number = alphabet.indexOf(char.toUpperCase());
// If character is not in the alphabet string, then we don't do anything to it
if (charIndex === -1) {
return char;
}
const shiftedIndex = (charIndex + shift) % 26;
const shiftedChar: string = isUpperCase
? alphabet[shiftedIndex]
: alphabet[shiftedIndex].toLowerCase();
return shiftedChar;
}
let encryptedMessage: string = "";
for (let i = 0; i < message.length; i++) {
const char: string = message[i];
const shiftedChar: string = shiftCharacter(char, shift);
encryptedMessage += shiftedChar;
}
return encryptedMessage;
}
/**
* Decrypts an encrypted message using the Caesar cipher.
*
* @param encryptedMessage - The encrypted message to decrypt. (default: "")
* @param shift - The number of positions to shift each character (default: 3).
* @returns The decrypted message.
*/
export function caesarDecrypt(
encryptedMessage: string,
shift: number = 3
): string {
if (encryptedMessage === undefined) {
throw new Error("A message to decrypt was not provided");
}
if (shift <= 0) {
throw new Error("The shift value cannot be negative");
}
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
function shiftCharacter(char: string, shift: number) {
if (char === " ") {
return " ";
}
const isUpperCase: boolean = char === char.toUpperCase();
const charIndex: number = alphabet.indexOf(char.toUpperCase());
if (charIndex === -1) {
return char;
}
// Here we use +26 so that the value is always a positive integer. cuz sometimes it may be negative.
const shiftedIndex: number = (charIndex - shift + 26) % 26;
const shiftedChar: string = isUpperCase
? alphabet[shiftedIndex]
: alphabet[shiftedIndex].toLowerCase();
return shiftedChar;
}
let decryptedMessage = "";
for (let i = 0; i < encryptedMessage.length; i++) {
const char: string = encryptedMessage[i];
const shiftedChar: string = shiftCharacter(char, shift);
decryptedMessage += shiftedChar;
}
return decryptedMessage;
}
const caesar: CaesarInterface = {
caesarEncrypt,
caesarDecrypt,
};
export default caesar;