-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (45 loc) · 1008 Bytes
/
index.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
41
42
43
44
45
46
47
48
49
50
51
52
53
const Caesar = require('./src/Caesar');
const Atbash = require('./src/Atbash');
const Vigenere = require('./src/Vigenere');
const algorithms = {
Caesar,
Atbash,
Vigenere
};
function createCipher(algorithm = 'Caesar') {
checkCipher(algorithm);
return {
encrypt: algorithms[algorithm].encrypt
};
}
function createDecipher(algorithm = 'Caesar') {
checkCipher(algorithm);
return {
decrypt: algorithms[algorithm].decrypt
};
}
/**
* Check for cipher exitence
* @param {string} name Name of the cipher
* @returns {boolean} True when supplied cipher is known
* @throws Error
*/
function checkCipher(name) {
const availableCiphers = getCiphers();
if (!availableCiphers.includes(name)) {
throw new Error(`Unknown cipher: ${name}`);
}
return true;
}
/**
* Return list of ciphers
* @returns {Array.<string>} List of available ciphers
*/
function getCiphers() {
return Object.keys(algorithms);
}
module.exports = {
createCipher,
createDecipher,
getCiphers
};