|
1 | 1 | /**
|
2 |
| - * Caesar's Cipher - also known as the ROT13 Cipher is when |
3 |
| - * a letter is replaced by the one that is 13 spaces away |
4 |
| - * from it in the alphabet. If the letter is in the first half |
5 |
| - * of the alphabet we add 13, if it's in the latter half we |
6 |
| - * subtract 13 from the character code value. |
| 2 | + * @function caesarsCipher |
| 3 | + * @description - In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence. |
| 4 | + * @see - [wiki](https://en.wikipedia.org/wiki/Caesar_cipher) |
| 5 | + * @param {string} str - string to be encrypted |
| 6 | + * @param {number} rotation - the number of rotation, expect real number ( > 0) |
| 7 | + * @return {string} - decrypted string |
7 | 8 | */
|
| 9 | +const caesarsCipher = (str, rotation) => { |
| 10 | + if (typeof str !== 'string' || !Number.isInteger(rotation) || rotation < 0) { |
| 11 | + throw new TypeError('Arguments are invalid') |
| 12 | + } |
8 | 13 |
|
9 |
| -/** |
10 |
| - * Decrypt a ROT13 cipher |
11 |
| - * @param {String} str - string to be decrypted |
12 |
| - * @return {String} decrypted string |
13 |
| - */ |
14 |
| -function rot13 (str) { |
15 |
| - const response = [] |
16 |
| - const strLength = str.length |
| 14 | + const alphabets = new Array(26) |
| 15 | + .fill() |
| 16 | + .map((_, index) => String.fromCharCode(97 + index)) // generate all lower alphabets array a-z |
17 | 17 |
|
18 |
| - for (let i = 0; i < strLength; i++) { |
19 |
| - const char = str.charCodeAt(i) |
| 18 | + const cipherMap = alphabets.reduce( |
| 19 | + (map, char, index) => map.set(char, alphabets[(rotation + index) % 26]), |
| 20 | + new Map() |
| 21 | + ) |
20 | 22 |
|
21 |
| - if (char < 65 || (char > 90 && char < 97) || char > 122) { |
22 |
| - response.push(str.charAt(i)) |
23 |
| - } else if ((char > 77 && char <= 90) || (char > 109 && char <= 122)) { |
24 |
| - response.push(String.fromCharCode(str.charCodeAt(i) - 13)) |
25 |
| - } else { |
26 |
| - response.push(String.fromCharCode(str.charCodeAt(i) + 13)) |
| 23 | + return str.replace(/[a-z]/gi, (char) => { |
| 24 | + if (/[A-Z]/.test(char)) { |
| 25 | + return cipherMap.get(char.toLowerCase()).toUpperCase() |
27 | 26 | }
|
28 |
| - } |
29 |
| - return response.join('') |
30 |
| -} |
31 | 27 |
|
32 |
| -export { rot13 } |
| 28 | + return cipherMap.get(char) |
| 29 | + }) |
| 30 | +} |
33 | 31 |
|
34 |
| -// > rot13('Uryyb Jbeyq') |
35 |
| -// 'Hello World' |
| 32 | +export default caesarsCipher |
0 commit comments