-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·87 lines (81 loc) · 2.43 KB
/
cli.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const getStdin = require('get-stdin');
const cli = meow(`
Usage
~ ❯❯❯ caesar <text>
~ ❯❯❯ cat <file> | caesar
Options
-n, --shift Use specific shift number
-b, --break Bruteforce all possible shifts
Example
~ ❯❯❯ caesar 'havpbea'
unicorn
~ ❯❯❯ caesar 'omcha mbczn 6' -n 6
using shift 6
~ ❯❯❯ caesar xliwigvixtewwtlvewimwewtlonvlbuuihprubmdpcomvxkjxkd -b
ROT-1: ymjxjhwjyufxxumwfxjnxfxumpowmcvvjiqsvcneqdpnwylkyle
...
ROT-22: thesecretpassphraseisasphkjrhxqqedlnqxizlykirtgftgz
...
ROT-25: wkhvhfuhwsdvvskudvhlvdvsknmukatthgoqtalcobnluwjiwjc
`, {
flags: {
shift: {
type: 'integer',
alias: 'n',
default: 13
},
break: {
type: 'boolean',
alias: 'b',
default: false
}
}
});
const input = cli.input[0];
function substitute (text, shift) {
var result = [];
text.forEach(function(char) {
if (char.search(/[A-Za-z]/) !== -1) {
var upper = /[A-Z]/.test(char);
var char = char.toLowerCase();
var charNum = char.charCodeAt(0);
if (charNum + shift > 122) {
char = String.fromCharCode(96 + ((charNum + shift) - 122));
if (upper) result.push(char.toUpperCase());
else result.push(char);
} else {
if (upper) result.push((String.fromCharCode(charNum + shift)).toUpperCase());
else result.push(String.fromCharCode(charNum + shift));
}
} else {
result.push(char);
}
})
return result.join('')
}
function caesar (text) {
var splitted = text.split('');
var num = cli.flags["shift"];
if (!cli.flags["break"]) {
console.log(substitute(splitted, num));
}
else {
for (num = 1; num < 26; num++) {
console.log("ROT-" + num + ": " + substitute(splitted, num));
}
}
}
if (!input && process.stdin.isTTY) {
console.log('Specify ROT-ciphered string to break');
process.exit(1);
}
if (input) {
caesar(input);
} else {
getStdin().then(stdin => {
caesar(stdin);
});
}