Skip to content

Commit 8bfa6b9

Browse files
feat: add Caesar cipher algorithm
1 parent 5c39e87 commit 8bfa6b9

2 files changed

Lines changed: 77 additions & 47 deletions

File tree

‎Ciphers/CaesarCipher.js‎

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
11
/**
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
2+
* @function caesarCipher
3+
* @description Caesar cipher is one of the simplest encryption techniques.
4+
* It works by shifting each letter in the plaintext by a fixed number
5+
* of positions in the alphabet.
6+
* @param {string} str - The input string to encrypt/decrypt
7+
* @param {number} shift - The number of positions to shift (can be negative)
8+
* @returns {string} - The encrypted/decrypted string
9+
* @see https://en.wikipedia.org/wiki/Caesar_cipher
10+
* @example caesarCipher("ABC", 1) => "BCD"
11+
* @example caesarCipher("XYZ", 3) => "ABC"
12+
* @example caesarCipher("Hello World", 13) => "Uryyb Jbeyq"
813
*/
9-
const caesarCipher = (str, rotation) => {
10-
if (typeof str !== 'string' || !Number.isInteger(rotation) || rotation < 0) {
11-
throw new TypeError('Arguments are invalid')
14+
const caesarCipher = (str, shift) => {
15+
if (typeof str !== 'string') {
16+
throw new TypeError('Input must be a string')
1217
}
13-
14-
const alphabets = new Array(26)
15-
.fill()
16-
.map((_, index) => String.fromCharCode(97 + index)) // generate all lower alphabets array a-z
17-
18-
const cipherMap = alphabets.reduce(
19-
(map, char, index) => map.set(char, alphabets[(rotation + index) % 26]),
20-
new Map()
21-
)
22-
23-
return str.replace(/[a-z]/gi, (char) => {
24-
if (/[A-Z]/.test(char)) {
25-
return cipherMap.get(char.toLowerCase()).toUpperCase()
26-
}
27-
28-
return cipherMap.get(char)
29-
})
18+
if (!Number.isInteger(shift)) {
19+
throw new TypeError('Shift must be an integer')
20+
}
21+
const normalizedShift = ((shift % 26) + 26) % 26
22+
return str
23+
.split('')
24+
.map((char) => {
25+
if (char.match(/[a-z]/)) {
26+
return String.fromCharCode(
27+
((char.charCodeAt(0) - 97 + normalizedShift) % 26) + 97
28+
)
29+
}
30+
if (char.match(/[A-Z]/)) {
31+
return String.fromCharCode(
32+
((char.charCodeAt(0) - 65 + normalizedShift) % 26) + 65
33+
)
34+
}
35+
return char
36+
})
37+
.join('')
3038
}
3139

32-
export default caesarCipher
40+
export { caesarCipher }

‎Ciphers/test/CaesarCipher.test.js‎

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,44 @@
1-
import caesarCipher from '../CaesarCipher'
2-
3-
describe('Testing the caesarsCipher function', () => {
4-
it('Test - 1, Testing for invalid types', () => {
5-
expect(() => caesarCipher(false, 3)).toThrow()
6-
expect(() => caesarCipher('false', -1)).toThrow()
7-
expect(() => caesarCipher('true', null)).toThrow()
8-
})
9-
10-
it('Test - 2, Testing for valid string and rotation', () => {
11-
expect(caesarCipher('middle-Outz', 2)).toBe('okffng-Qwvb')
12-
expect(caesarCipher('abcdefghijklmnopqrstuvwxyz', 3)).toBe(
13-
'defghijklmnopqrstuvwxyzabc'
14-
)
15-
expect(caesarCipher('Always-Look-on-the-Bright-Side-of-Life', 5)).toBe(
16-
'Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj'
17-
)
18-
expect(
19-
caesarCipher('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG', 23)
20-
).toBe('QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD')
1+
import { caesarCipher } from '../CaesarCipher.js'
2+
3+
describe('caesarCipher', () => {
4+
test('shifts letters by 1', () => {
5+
expect(caesarCipher('ABC', 1)).toBe('BCD')
6+
})
7+
8+
test('wraps around Z to A', () => {
9+
expect(caesarCipher('XYZ', 3)).toBe('ABC')
10+
})
11+
12+
test('preserves case', () => {
13+
expect(caesarCipher('Hello', 13)).toBe('Uryyb')
14+
})
15+
16+
test('preserves spaces and punctuation', () => {
17+
expect(caesarCipher('Hello World!', 13)).toBe('Uryyb Jbeyq!')
18+
})
19+
20+
test('shift of 0 returns same string', () => {
21+
expect(caesarCipher('ABC', 0)).toBe('ABC')
22+
})
23+
24+
test('shift of 26 returns same string', () => {
25+
expect(caesarCipher('ABC', 26)).toBe('ABC')
26+
})
27+
28+
test('negative shift works', () => {
29+
expect(caesarCipher('BCD', -1)).toBe('ABC')
30+
})
31+
32+
test('decryption reverses encryption', () => {
33+
const original = 'Hello World'
34+
expect(caesarCipher(caesarCipher(original, 13), -13)).toBe(original)
35+
})
36+
37+
test('throws TypeError for non-string input', () => {
38+
expect(() => caesarCipher(123, 1)).toThrow(TypeError)
39+
})
40+
41+
test('throws TypeError for non-integer shift', () => {
42+
expect(() => caesarCipher('ABC', 1.5)).toThrow(TypeError)
2143
})
2244
})

0 commit comments

Comments
 (0)