-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathalejandromiranda.js
41 lines (28 loc) · 1.08 KB
/
alejandromiranda.js
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
/* Cifrado de Cesár */
const englishAlphabet = 'abcdefghijklmnopqrstuvwxyz'.split('')
const getCipherMap = (alphabet, shift) => {
return alphabet.
reduce((charsMap, currentChar, charIndex) => {
const charsMapClone = { ...charsMap }
let encryptedCharIndex = (charIndex + shift) % alphabet.length
if(encryptedCharIndex < 0) {
encryptedCharIndex += alphabet.length
}
charsMapClone[currentChar] = alphabet[encryptedCharIndex]
return charsMapClone
}, {})
}
const encrypt = (str, shift, alphabet = englishAlphabet) => {
const cipherMap = getCipherMap(alphabet, shift)
return str.toLowerCase().split('').map(char => cipherMap[char] || char).join('')
}
const decrypt = (str, shift, alphabet = englishAlphabet) => {
const cipherMap = getCipherMap(alphabet, -shift)
return str.toLowerCase().split('').map(char => cipherMap[char] || char).join('')
}
const str = 'Hola Mundo'
const enc = encrypt(str, 2)
const dec = decrypt(enc, 2)
console.log(str)
console.log(enc)
console.log(dec)