forked from hackademymx/future-developers-practica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaraiVentura.js
32 lines (24 loc) · 1 KB
/
SaraiVentura.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
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)