Skip to content

Instantly share code, notes, and snippets.

@Kamilahsantos
Created May 26, 2020 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kamilahsantos/46802a2b6e73a1506d738bdaa7081cb4 to your computer and use it in GitHub Desktop.
Save Kamilahsantos/46802a2b6e73a1506d738bdaa7081cb4 to your computer and use it in GitHub Desktop.
const axios = require("axios");
const fs = require("fs");
const sha1 = require("js-sha1");
const request = require("request");
const MYTOKEN = "seu token";
const API_URL = "https://api.codenation.dev/v1/challenge/dev-ps";
const searchChallenge = async () => {
const { data } = await axios.get(`${API_URL}/generate-data?token=${MYTOKEN}`);
return data;
};
const decifraLetra = (alfabetArray, caracter) => {
const positionInAlfabet = alfabetArray.indexOf(caracter);
if (positionInAlfabet < 0) {
return caracter;
}
const posicaoDecodificada = positionInAlfabet - 8;
//verifica se sera necessario pegar de forma 'reversa'
if (posicaoDecodificada >= 0) {
return alfabetArray[posicaoDecodificada];
} else {
return alfabetArray[alfabetArray.length - posicaoDecodificada * -1];
}
};
const decifra = (string, numeroCasas) => {
//gerando alfabeto lowercase
const alfabetArray = new Array(26)
.fill(null)
.map((_, index) => String.fromCharCode(97 + index));
//gerando array da string convertido para lowercase
const stringArray = string.toLowerCase().split("");
const fraseDecifrada = stringArray.reduce((frase, caracter) => {
return (frase += decifraLetra(alfabetArray, caracter));
}, "");
return fraseDecifrada;
};
const geraResumoCripto = string => sha1(string);
const salvaResposta = (desafioCompleto, formatacao = null) => {
const desafioStringified = JSON.stringify(desafioCompleto, null, formatacao);
fs.writeFileSync("answer.json", desafioStringified);
};
const enviarResposta = async () => {
const options = {
method: "POST",
url: `${API_URL}/submit-solution?token=${MYTOKEN}`,
headers: {
"Content-Type": "multipart/form-data",
},
formData: {
answer: fs.createReadStream("./answer.json"),
},
};
request(options, function(err, res, body) {
if (err) console.log(err);
console.log(body);
});
};
searchChallenge().then(desafio => {
const { cifrado } = desafio;
//guarda decifrado
desafio.decifrado = decifra(cifrado, 8);
//gerar resumo
desafio.resumo_criptografico = geraResumoCripto(desafio.decifrado);
//salvar json
salvaResposta(desafio, 2);
//enviar json para api
enviarResposta();
});
package json
{
"name": "desafio-criptografia",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"axios": "^0.18.0",
"form-data": "^2.3.3",
"js-sha1": "^0.6.0",
"node-fetch": "^2.6.0",
"request": "^2.88.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment