-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (90 loc) · 2.89 KB
/
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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const fs = require('fs');
const crypto = require('crypto');
const axios = require('axios');
const FormData = require('form-data');
require('dotenv').config();
const ADDRESS_GET_FILE = 'https://api.codenation.dev/v1/challenge/dev-ps/generate-data?token=';
const ADDRESS_SEND_FILE = 'https://api.codenation.dev/v1/challenge/dev-ps/submit-solution?token=';
const FILE = 'answer.json';
const main = async () => {
try {
const data = await getCypher();
await writeToFile(FILE, data);
const answer = await readFromFile(FILE);
const { cifrado, numero_casas } = answer;
answer.decifrado = ceasarCypherSolver(cifrado, numero_casas);
answer.resumo_criptografico = createSHA1Hash(answer.decifrado);
await writeToFile(FILE, answer);
sendFile();
} catch (error) {
console.error(error);
}
}
main();
function sendFile() {
const formData = new FormData();
formData.append('answer', fs.createReadStream(FILE));
const formHeaders = formData.getHeaders();
axios.post(`${ADDRESS_SEND_FILE}${process.env.TOKEN}`, formData, {
headers: {
...formHeaders,
},
})
.then(response => console.log(response.data))
.catch(error => {
console.error(error)
console.log(error.data)
})
}
function getCypher() {
return new Promise((resolve, reject) => {
axios.get(`${ADDRESS_GET_FILE}${process.env.TOKEN}`)
.then(response => resolve(response.data))
.catch(error => reject(console.error(error)))
});
}
function readFromFile(file) {
return new Promise((resolve, reject) => {
fs.readFile(FILE, (error, data) => {
if (error) {
console.error('Something went wrong trying to read the file')
reject(error);
}
return resolve(JSON.parse(data));
});
});
}
function writeToFile(file, data) {
return new Promise((resolve, reject) => {
fs.writeFile(file, JSON.stringify(data, null, '\t'), (error) => {
console.error(error);
reject({ status: 'Error, could not write/create file' });
});
resolve({ status: `File created with :\n${data}` });
});
}
function ceasarCypherSolver(text, shift) {
const lowerCase = { start: 97, end: 122 };
const alphabetSize = lowerCase.end - lowerCase.start;
const arrOfCharCodes = [];
for (letter of text) {
const letterCode = letter.charCodeAt(0);
if (letterCode < lowerCase.start || letterCode > lowerCase.end) {
arrOfCharCodes.push(letterCode);
continue;
}
let letterCodePlusShift = letterCode - shift;
if (letterCodePlusShift > lowerCase.end) {
letterCodePlusShift -= alphabetSize;
} else if (letterCodePlusShift < lowerCase.start) {
letterCodePlusShift += alphabetSize;
}
arrOfCharCodes.push(letterCodePlusShift);
}
return String.fromCharCode(...arrOfCharCodes);
}
function createSHA1Hash(text) {
const sha1Creator = crypto.createHash('sha1');
sha1Creator.update(text);
return sha1Creator.digest('hex');
}